blob: 342bbd661d3b0d2f55e0de3f91b721fa9bb4b33e [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
Carolyn Wyborny66999382012-12-05 02:46:05 +000063#define MIN 1
64#define BUILD 2
Carolyn Wyborny0d1fe822011-03-11 20:58:19 -080065#define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) "." \
Carolyn Wyborny929dd042011-05-26 03:02:26 +000066__stringify(BUILD) "-k"
Auke Kok9d5c8242008-01-24 02:22:38 -080067char igb_driver_name[] = "igb";
68char igb_driver_version[] = DRV_VERSION;
69static const char igb_driver_string[] =
70 "Intel(R) Gigabit Ethernet Network Driver";
Carolyn Wyborny6e861322012-01-18 22:13:27 +000071static const char igb_copyright[] = "Copyright (c) 2007-2012 Intel Corporation.";
Auke Kok9d5c8242008-01-24 02:22:38 -080072
Auke Kok9d5c8242008-01-24 02:22:38 -080073static const struct e1000_info *igb_info_tbl[] = {
74 [board_82575] = &e1000_82575_info,
75};
76
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000077static DEFINE_PCI_DEVICE_TABLE(igb_pci_tbl) = {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +000078 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I211_COPPER), board_82575 },
79 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER), board_82575 },
80 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_FIBER), board_82575 },
81 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES), board_82575 },
82 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SGMII), board_82575 },
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +000083 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_COPPER), board_82575 },
84 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_FIBER), board_82575 },
85 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SERDES), board_82575 },
86 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SGMII), board_82575 },
Alexander Duyck55cac242009-11-19 12:42:21 +000087 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER), board_82575 },
88 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_FIBER), board_82575 },
Carolyn Wyborny6493d242011-01-14 05:33:46 +000089 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_QUAD_FIBER), board_82575 },
Alexander Duyck55cac242009-11-19 12:42:21 +000090 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SERDES), board_82575 },
91 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SGMII), board_82575 },
92 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER_DUAL), board_82575 },
Joseph Gasparakis308fb392010-09-22 17:56:44 +000093 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SGMII), board_82575 },
94 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SERDES), board_82575 },
Gasparakis, Joseph1b5dda32010-12-09 01:41:01 +000095 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_BACKPLANE), board_82575 },
96 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SFP), board_82575 },
Alexander Duyck2d064c02008-07-08 15:10:12 -070097 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
Alexander Duyck9eb23412009-03-13 20:42:15 +000098 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS), board_82575 },
Alexander Duyck747d49b2009-10-05 06:33:27 +000099 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS_SERDES), board_82575 },
Alexander Duyck2d064c02008-07-08 15:10:12 -0700100 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
101 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
Alexander Duyck4703bf72009-07-23 18:09:48 +0000102 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES_QUAD), board_82575 },
Carolyn Wybornyb894fa22010-03-19 06:07:48 +0000103 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER_ET2), board_82575 },
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +0000104 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
Auke Kok9d5c8242008-01-24 02:22:38 -0800105 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
106 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
107 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
108 /* required last entry */
109 {0, }
110};
111
112MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
113
114void igb_reset(struct igb_adapter *);
115static int igb_setup_all_tx_resources(struct igb_adapter *);
116static int igb_setup_all_rx_resources(struct igb_adapter *);
117static void igb_free_all_tx_resources(struct igb_adapter *);
118static void igb_free_all_rx_resources(struct igb_adapter *);
Alexander Duyck06cf2662009-10-27 15:53:25 +0000119static void igb_setup_mrqc(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800120static int igb_probe(struct pci_dev *, const struct pci_device_id *);
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500121static void igb_remove(struct pci_dev *pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -0800122static int igb_sw_init(struct igb_adapter *);
123static int igb_open(struct net_device *);
124static int igb_close(struct net_device *);
Stefan Assmann53c7d062012-12-04 06:00:12 +0000125static void igb_configure(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800126static void igb_configure_tx(struct igb_adapter *);
127static void igb_configure_rx(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800128static void igb_clean_all_tx_rings(struct igb_adapter *);
129static void igb_clean_all_rx_rings(struct igb_adapter *);
Mitch Williams3b644cf2008-06-27 10:59:48 -0700130static void igb_clean_tx_ring(struct igb_ring *);
131static void igb_clean_rx_ring(struct igb_ring *);
Alexander Duyckff41f8d2009-09-03 14:48:56 +0000132static void igb_set_rx_mode(struct net_device *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800133static void igb_update_phy_info(unsigned long);
134static void igb_watchdog(unsigned long);
135static void igb_watchdog_task(struct work_struct *);
Alexander Duyckcd392f52011-08-26 07:43:59 +0000136static netdev_tx_t igb_xmit_frame(struct sk_buff *skb, struct net_device *);
Eric Dumazet12dcd862010-10-15 17:27:10 +0000137static struct rtnl_link_stats64 *igb_get_stats64(struct net_device *dev,
138 struct rtnl_link_stats64 *stats);
Auke Kok9d5c8242008-01-24 02:22:38 -0800139static int igb_change_mtu(struct net_device *, int);
140static int igb_set_mac(struct net_device *, void *);
Alexander Duyck68d480c2009-10-05 06:33:08 +0000141static void igb_set_uta(struct igb_adapter *adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800142static irqreturn_t igb_intr(int irq, void *);
143static irqreturn_t igb_intr_msi(int irq, void *);
144static irqreturn_t igb_msix_other(int irq, void *);
Alexander Duyck047e0032009-10-27 15:49:27 +0000145static irqreturn_t igb_msix_ring(int irq, void *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700146#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +0000147static void igb_update_dca(struct igb_q_vector *);
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700148static void igb_setup_dca(struct igb_adapter *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700149#endif /* CONFIG_IGB_DCA */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700150static int igb_poll(struct napi_struct *, int);
Alexander Duyck13fde972011-10-05 13:35:24 +0000151static bool igb_clean_tx_irq(struct igb_q_vector *);
Alexander Duyckcd392f52011-08-26 07:43:59 +0000152static bool igb_clean_rx_irq(struct igb_q_vector *, int);
Auke Kok9d5c8242008-01-24 02:22:38 -0800153static int igb_ioctl(struct net_device *, struct ifreq *, int cmd);
154static void igb_tx_timeout(struct net_device *);
155static void igb_reset_task(struct work_struct *);
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000156static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features);
Jiri Pirko8e586132011-12-08 19:52:37 -0500157static int igb_vlan_rx_add_vid(struct net_device *, u16);
158static int igb_vlan_rx_kill_vid(struct net_device *, u16);
Auke Kok9d5c8242008-01-24 02:22:38 -0800159static void igb_restore_vlan(struct igb_adapter *);
Alexander Duyck26ad9172009-10-05 06:32:49 +0000160static void igb_rar_set_qsel(struct igb_adapter *, u8 *, u32 , u8);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800161static void igb_ping_all_vfs(struct igb_adapter *);
162static void igb_msg_task(struct igb_adapter *);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800163static void igb_vmm_control(struct igb_adapter *);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +0000164static int igb_set_vf_mac(struct igb_adapter *, int, unsigned char *);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800165static void igb_restore_vf_multicasts(struct igb_adapter *adapter);
Williams, Mitch A8151d292010-02-10 01:44:24 +0000166static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac);
167static int igb_ndo_set_vf_vlan(struct net_device *netdev,
168 int vf, u16 vlan, u8 qos);
169static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate);
170static int igb_ndo_get_vf_config(struct net_device *netdev, int vf,
171 struct ifla_vf_info *ivi);
Lior Levy17dc5662011-02-08 02:28:46 +0000172static void igb_check_vf_rate_limit(struct igb_adapter *);
RongQing Li46a01692011-10-18 22:52:35 +0000173
174#ifdef CONFIG_PCI_IOV
Greg Rose0224d662011-10-14 02:57:14 +0000175static int igb_vf_configure(struct igb_adapter *adapter, int vf);
Stefan Assmannf5571472012-08-18 04:06:11 +0000176static bool igb_vfs_are_assigned(struct igb_adapter *adapter);
RongQing Li46a01692011-10-18 22:52:35 +0000177#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800178
Auke Kok9d5c8242008-01-24 02:22:38 -0800179#ifdef CONFIG_PM
Emil Tantilovd9dd9662012-01-28 08:10:35 +0000180#ifdef CONFIG_PM_SLEEP
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000181static int igb_suspend(struct device *);
Emil Tantilovd9dd9662012-01-28 08:10:35 +0000182#endif
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000183static int igb_resume(struct device *);
184#ifdef CONFIG_PM_RUNTIME
185static int igb_runtime_suspend(struct device *dev);
186static int igb_runtime_resume(struct device *dev);
187static int igb_runtime_idle(struct device *dev);
188#endif
189static const struct dev_pm_ops igb_pm_ops = {
190 SET_SYSTEM_SLEEP_PM_OPS(igb_suspend, igb_resume)
191 SET_RUNTIME_PM_OPS(igb_runtime_suspend, igb_runtime_resume,
192 igb_runtime_idle)
193};
Auke Kok9d5c8242008-01-24 02:22:38 -0800194#endif
195static void igb_shutdown(struct pci_dev *);
Greg Rosefa44f2f2013-01-17 01:03:06 -0800196static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700197#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700198static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
199static struct notifier_block dca_notifier = {
200 .notifier_call = igb_notify_dca,
201 .next = NULL,
202 .priority = 0
203};
204#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800205#ifdef CONFIG_NET_POLL_CONTROLLER
206/* for netdump / net console */
207static void igb_netpoll(struct net_device *);
208#endif
Alexander Duyck37680112009-02-19 20:40:30 -0800209#ifdef CONFIG_PCI_IOV
Alexander Duyck2a3abf62009-04-07 14:37:52 +0000210static unsigned int max_vfs = 0;
211module_param(max_vfs, uint, 0);
212MODULE_PARM_DESC(max_vfs, "Maximum number of virtual functions to allocate "
213 "per physical function");
214#endif /* CONFIG_PCI_IOV */
215
Auke Kok9d5c8242008-01-24 02:22:38 -0800216static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
217 pci_channel_state_t);
218static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
219static void igb_io_resume(struct pci_dev *);
220
Stephen Hemminger3646f0e2012-09-07 09:33:15 -0700221static const struct pci_error_handlers igb_err_handler = {
Auke Kok9d5c8242008-01-24 02:22:38 -0800222 .error_detected = igb_io_error_detected,
223 .slot_reset = igb_io_slot_reset,
224 .resume = igb_io_resume,
225};
226
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +0000227static void igb_init_dmac(struct igb_adapter *adapter, u32 pba);
Auke Kok9d5c8242008-01-24 02:22:38 -0800228
229static struct pci_driver igb_driver = {
230 .name = igb_driver_name,
231 .id_table = igb_pci_tbl,
232 .probe = igb_probe,
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500233 .remove = igb_remove,
Auke Kok9d5c8242008-01-24 02:22:38 -0800234#ifdef CONFIG_PM
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000235 .driver.pm = &igb_pm_ops,
Auke Kok9d5c8242008-01-24 02:22:38 -0800236#endif
237 .shutdown = igb_shutdown,
Greg Rosefa44f2f2013-01-17 01:03:06 -0800238 .sriov_configure = igb_pci_sriov_configure,
Auke Kok9d5c8242008-01-24 02:22:38 -0800239 .err_handler = &igb_err_handler
240};
241
242MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
243MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
244MODULE_LICENSE("GPL");
245MODULE_VERSION(DRV_VERSION);
246
stephen hemmingerb3f4d592012-03-13 06:04:20 +0000247#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
248static int debug = -1;
249module_param(debug, int, 0);
250MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
251
Taku Izumic97ec422010-04-27 14:39:30 +0000252struct igb_reg_info {
253 u32 ofs;
254 char *name;
255};
256
257static const struct igb_reg_info igb_reg_info_tbl[] = {
258
259 /* General Registers */
260 {E1000_CTRL, "CTRL"},
261 {E1000_STATUS, "STATUS"},
262 {E1000_CTRL_EXT, "CTRL_EXT"},
263
264 /* Interrupt Registers */
265 {E1000_ICR, "ICR"},
266
267 /* RX Registers */
268 {E1000_RCTL, "RCTL"},
269 {E1000_RDLEN(0), "RDLEN"},
270 {E1000_RDH(0), "RDH"},
271 {E1000_RDT(0), "RDT"},
272 {E1000_RXDCTL(0), "RXDCTL"},
273 {E1000_RDBAL(0), "RDBAL"},
274 {E1000_RDBAH(0), "RDBAH"},
275
276 /* TX Registers */
277 {E1000_TCTL, "TCTL"},
278 {E1000_TDBAL(0), "TDBAL"},
279 {E1000_TDBAH(0), "TDBAH"},
280 {E1000_TDLEN(0), "TDLEN"},
281 {E1000_TDH(0), "TDH"},
282 {E1000_TDT(0), "TDT"},
283 {E1000_TXDCTL(0), "TXDCTL"},
284 {E1000_TDFH, "TDFH"},
285 {E1000_TDFT, "TDFT"},
286 {E1000_TDFHS, "TDFHS"},
287 {E1000_TDFPC, "TDFPC"},
288
289 /* List Terminator */
290 {}
291};
292
293/*
294 * igb_regdump - register printout routine
295 */
296static void igb_regdump(struct e1000_hw *hw, struct igb_reg_info *reginfo)
297{
298 int n = 0;
299 char rname[16];
300 u32 regs[8];
301
302 switch (reginfo->ofs) {
303 case E1000_RDLEN(0):
304 for (n = 0; n < 4; n++)
305 regs[n] = rd32(E1000_RDLEN(n));
306 break;
307 case E1000_RDH(0):
308 for (n = 0; n < 4; n++)
309 regs[n] = rd32(E1000_RDH(n));
310 break;
311 case E1000_RDT(0):
312 for (n = 0; n < 4; n++)
313 regs[n] = rd32(E1000_RDT(n));
314 break;
315 case E1000_RXDCTL(0):
316 for (n = 0; n < 4; n++)
317 regs[n] = rd32(E1000_RXDCTL(n));
318 break;
319 case E1000_RDBAL(0):
320 for (n = 0; n < 4; n++)
321 regs[n] = rd32(E1000_RDBAL(n));
322 break;
323 case E1000_RDBAH(0):
324 for (n = 0; n < 4; n++)
325 regs[n] = rd32(E1000_RDBAH(n));
326 break;
327 case E1000_TDBAL(0):
328 for (n = 0; n < 4; n++)
329 regs[n] = rd32(E1000_RDBAL(n));
330 break;
331 case E1000_TDBAH(0):
332 for (n = 0; n < 4; n++)
333 regs[n] = rd32(E1000_TDBAH(n));
334 break;
335 case E1000_TDLEN(0):
336 for (n = 0; n < 4; n++)
337 regs[n] = rd32(E1000_TDLEN(n));
338 break;
339 case E1000_TDH(0):
340 for (n = 0; n < 4; n++)
341 regs[n] = rd32(E1000_TDH(n));
342 break;
343 case E1000_TDT(0):
344 for (n = 0; n < 4; n++)
345 regs[n] = rd32(E1000_TDT(n));
346 break;
347 case E1000_TXDCTL(0):
348 for (n = 0; n < 4; n++)
349 regs[n] = rd32(E1000_TXDCTL(n));
350 break;
351 default:
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000352 pr_info("%-15s %08x\n", reginfo->name, rd32(reginfo->ofs));
Taku Izumic97ec422010-04-27 14:39:30 +0000353 return;
354 }
355
356 snprintf(rname, 16, "%s%s", reginfo->name, "[0-3]");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000357 pr_info("%-15s %08x %08x %08x %08x\n", rname, regs[0], regs[1],
358 regs[2], regs[3]);
Taku Izumic97ec422010-04-27 14:39:30 +0000359}
360
361/*
362 * igb_dump - Print registers, tx-rings and rx-rings
363 */
364static void igb_dump(struct igb_adapter *adapter)
365{
366 struct net_device *netdev = adapter->netdev;
367 struct e1000_hw *hw = &adapter->hw;
368 struct igb_reg_info *reginfo;
Taku Izumic97ec422010-04-27 14:39:30 +0000369 struct igb_ring *tx_ring;
370 union e1000_adv_tx_desc *tx_desc;
371 struct my_u0 { u64 a; u64 b; } *u0;
Taku Izumic97ec422010-04-27 14:39:30 +0000372 struct igb_ring *rx_ring;
373 union e1000_adv_rx_desc *rx_desc;
374 u32 staterr;
Alexander Duyck6ad4edf2011-08-26 07:45:26 +0000375 u16 i, n;
Taku Izumic97ec422010-04-27 14:39:30 +0000376
377 if (!netif_msg_hw(adapter))
378 return;
379
380 /* Print netdevice Info */
381 if (netdev) {
382 dev_info(&adapter->pdev->dev, "Net device Info\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000383 pr_info("Device Name state trans_start "
384 "last_rx\n");
385 pr_info("%-15s %016lX %016lX %016lX\n", netdev->name,
386 netdev->state, netdev->trans_start, netdev->last_rx);
Taku Izumic97ec422010-04-27 14:39:30 +0000387 }
388
389 /* Print Registers */
390 dev_info(&adapter->pdev->dev, "Register Dump\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000391 pr_info(" Register Name Value\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000392 for (reginfo = (struct igb_reg_info *)igb_reg_info_tbl;
393 reginfo->name; reginfo++) {
394 igb_regdump(hw, reginfo);
395 }
396
397 /* Print TX Ring Summary */
398 if (!netdev || !netif_running(netdev))
399 goto exit;
400
401 dev_info(&adapter->pdev->dev, "TX Rings Summary\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000402 pr_info("Queue [NTU] [NTC] [bi(ntc)->dma ] leng ntw timestamp\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000403 for (n = 0; n < adapter->num_tx_queues; n++) {
Alexander Duyck06034642011-08-26 07:44:22 +0000404 struct igb_tx_buffer *buffer_info;
Taku Izumic97ec422010-04-27 14:39:30 +0000405 tx_ring = adapter->tx_ring[n];
Alexander Duyck06034642011-08-26 07:44:22 +0000406 buffer_info = &tx_ring->tx_buffer_info[tx_ring->next_to_clean];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000407 pr_info(" %5d %5X %5X %016llX %04X %p %016llX\n",
408 n, tx_ring->next_to_use, tx_ring->next_to_clean,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000409 (u64)dma_unmap_addr(buffer_info, dma),
410 dma_unmap_len(buffer_info, len),
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000411 buffer_info->next_to_watch,
412 (u64)buffer_info->time_stamp);
Taku Izumic97ec422010-04-27 14:39:30 +0000413 }
414
415 /* Print TX Rings */
416 if (!netif_msg_tx_done(adapter))
417 goto rx_ring_summary;
418
419 dev_info(&adapter->pdev->dev, "TX Rings Dump\n");
420
421 /* Transmit Descriptor Formats
422 *
423 * Advanced Transmit Descriptor
424 * +--------------------------------------------------------------+
425 * 0 | Buffer Address [63:0] |
426 * +--------------------------------------------------------------+
427 * 8 | PAYLEN | PORTS |CC|IDX | STA | DCMD |DTYP|MAC|RSV| DTALEN |
428 * +--------------------------------------------------------------+
429 * 63 46 45 40 39 38 36 35 32 31 24 15 0
430 */
431
432 for (n = 0; n < adapter->num_tx_queues; n++) {
433 tx_ring = adapter->tx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000434 pr_info("------------------------------------\n");
435 pr_info("TX QUEUE INDEX = %d\n", tx_ring->queue_index);
436 pr_info("------------------------------------\n");
437 pr_info("T [desc] [address 63:0 ] [PlPOCIStDDM Ln] "
438 "[bi->dma ] leng ntw timestamp "
439 "bi->skb\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000440
441 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000442 const char *next_desc;
Alexander Duyck06034642011-08-26 07:44:22 +0000443 struct igb_tx_buffer *buffer_info;
Alexander Duyck601369062011-08-26 07:44:05 +0000444 tx_desc = IGB_TX_DESC(tx_ring, i);
Alexander Duyck06034642011-08-26 07:44:22 +0000445 buffer_info = &tx_ring->tx_buffer_info[i];
Taku Izumic97ec422010-04-27 14:39:30 +0000446 u0 = (struct my_u0 *)tx_desc;
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000447 if (i == tx_ring->next_to_use &&
448 i == tx_ring->next_to_clean)
449 next_desc = " NTC/U";
450 else if (i == tx_ring->next_to_use)
451 next_desc = " NTU";
452 else if (i == tx_ring->next_to_clean)
453 next_desc = " NTC";
454 else
455 next_desc = "";
456
457 pr_info("T [0x%03X] %016llX %016llX %016llX"
458 " %04X %p %016llX %p%s\n", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000459 le64_to_cpu(u0->a),
460 le64_to_cpu(u0->b),
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000461 (u64)dma_unmap_addr(buffer_info, dma),
462 dma_unmap_len(buffer_info, len),
Taku Izumic97ec422010-04-27 14:39:30 +0000463 buffer_info->next_to_watch,
464 (u64)buffer_info->time_stamp,
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000465 buffer_info->skb, next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000466
Emil Tantilovb6695882012-07-28 05:07:48 +0000467 if (netif_msg_pktdata(adapter) && buffer_info->skb)
Taku Izumic97ec422010-04-27 14:39:30 +0000468 print_hex_dump(KERN_INFO, "",
469 DUMP_PREFIX_ADDRESS,
Emil Tantilovb6695882012-07-28 05:07:48 +0000470 16, 1, buffer_info->skb->data,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000471 dma_unmap_len(buffer_info, len),
472 true);
Taku Izumic97ec422010-04-27 14:39:30 +0000473 }
474 }
475
476 /* Print RX Rings Summary */
477rx_ring_summary:
478 dev_info(&adapter->pdev->dev, "RX Rings Summary\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000479 pr_info("Queue [NTU] [NTC]\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000480 for (n = 0; n < adapter->num_rx_queues; n++) {
481 rx_ring = adapter->rx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000482 pr_info(" %5d %5X %5X\n",
483 n, rx_ring->next_to_use, rx_ring->next_to_clean);
Taku Izumic97ec422010-04-27 14:39:30 +0000484 }
485
486 /* Print RX Rings */
487 if (!netif_msg_rx_status(adapter))
488 goto exit;
489
490 dev_info(&adapter->pdev->dev, "RX Rings Dump\n");
491
492 /* Advanced Receive Descriptor (Read) Format
493 * 63 1 0
494 * +-----------------------------------------------------+
495 * 0 | Packet Buffer Address [63:1] |A0/NSE|
496 * +----------------------------------------------+------+
497 * 8 | Header Buffer Address [63:1] | DD |
498 * +-----------------------------------------------------+
499 *
500 *
501 * Advanced Receive Descriptor (Write-Back) Format
502 *
503 * 63 48 47 32 31 30 21 20 17 16 4 3 0
504 * +------------------------------------------------------+
505 * 0 | Packet IP |SPH| HDR_LEN | RSV|Packet| RSS |
506 * | Checksum Ident | | | | Type | Type |
507 * +------------------------------------------------------+
508 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
509 * +------------------------------------------------------+
510 * 63 48 47 32 31 20 19 0
511 */
512
513 for (n = 0; n < adapter->num_rx_queues; n++) {
514 rx_ring = adapter->rx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000515 pr_info("------------------------------------\n");
516 pr_info("RX QUEUE INDEX = %d\n", rx_ring->queue_index);
517 pr_info("------------------------------------\n");
518 pr_info("R [desc] [ PktBuf A0] [ HeadBuf DD] "
519 "[bi->dma ] [bi->skb] <-- Adv Rx Read format\n");
520 pr_info("RWB[desc] [PcsmIpSHl PtRs] [vl er S cks ln] -----"
521 "----------- [bi->skb] <-- Adv Rx Write-Back format\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000522
523 for (i = 0; i < rx_ring->count; i++) {
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000524 const char *next_desc;
Alexander Duyck06034642011-08-26 07:44:22 +0000525 struct igb_rx_buffer *buffer_info;
526 buffer_info = &rx_ring->rx_buffer_info[i];
Alexander Duyck601369062011-08-26 07:44:05 +0000527 rx_desc = IGB_RX_DESC(rx_ring, i);
Taku Izumic97ec422010-04-27 14:39:30 +0000528 u0 = (struct my_u0 *)rx_desc;
529 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000530
531 if (i == rx_ring->next_to_use)
532 next_desc = " NTU";
533 else if (i == rx_ring->next_to_clean)
534 next_desc = " NTC";
535 else
536 next_desc = "";
537
Taku Izumic97ec422010-04-27 14:39:30 +0000538 if (staterr & E1000_RXD_STAT_DD) {
539 /* Descriptor Done */
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000540 pr_info("%s[0x%03X] %016llX %016llX ---------------- %s\n",
541 "RWB", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000542 le64_to_cpu(u0->a),
543 le64_to_cpu(u0->b),
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000544 next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000545 } else {
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000546 pr_info("%s[0x%03X] %016llX %016llX %016llX %s\n",
547 "R ", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000548 le64_to_cpu(u0->a),
549 le64_to_cpu(u0->b),
550 (u64)buffer_info->dma,
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000551 next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000552
Emil Tantilovb6695882012-07-28 05:07:48 +0000553 if (netif_msg_pktdata(adapter) &&
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000554 buffer_info->dma && buffer_info->page) {
Alexander Duyck44390ca2011-08-26 07:43:38 +0000555 print_hex_dump(KERN_INFO, "",
556 DUMP_PREFIX_ADDRESS,
557 16, 1,
Emil Tantilovb6695882012-07-28 05:07:48 +0000558 page_address(buffer_info->page) +
559 buffer_info->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +0000560 IGB_RX_BUFSZ, true);
Taku Izumic97ec422010-04-27 14:39:30 +0000561 }
562 }
Taku Izumic97ec422010-04-27 14:39:30 +0000563 }
564 }
565
566exit:
567 return;
568}
569
Auke Kok9d5c8242008-01-24 02:22:38 -0800570/**
Alexander Duyckc0410762010-03-25 13:10:08 +0000571 * igb_get_hw_dev - return device
Auke Kok9d5c8242008-01-24 02:22:38 -0800572 * used by hardware layer to print debugging information
573 **/
Alexander Duyckc0410762010-03-25 13:10:08 +0000574struct net_device *igb_get_hw_dev(struct e1000_hw *hw)
Auke Kok9d5c8242008-01-24 02:22:38 -0800575{
576 struct igb_adapter *adapter = hw->back;
Alexander Duyckc0410762010-03-25 13:10:08 +0000577 return adapter->netdev;
Auke Kok9d5c8242008-01-24 02:22:38 -0800578}
Patrick Ohly38c845c2009-02-12 05:03:41 +0000579
580/**
Auke Kok9d5c8242008-01-24 02:22:38 -0800581 * igb_init_module - Driver Registration Routine
582 *
583 * igb_init_module is the first routine called when the driver is
584 * loaded. All it does is register with the PCI subsystem.
585 **/
586static int __init igb_init_module(void)
587{
588 int ret;
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000589 pr_info("%s - version %s\n",
Auke Kok9d5c8242008-01-24 02:22:38 -0800590 igb_driver_string, igb_driver_version);
591
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000592 pr_info("%s\n", igb_copyright);
Auke Kok9d5c8242008-01-24 02:22:38 -0800593
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700594#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700595 dca_register_notify(&dca_notifier);
596#endif
Alexander Duyckbbd98fe2009-01-31 00:52:30 -0800597 ret = pci_register_driver(&igb_driver);
Auke Kok9d5c8242008-01-24 02:22:38 -0800598 return ret;
599}
600
601module_init(igb_init_module);
602
603/**
604 * igb_exit_module - Driver Exit Cleanup Routine
605 *
606 * igb_exit_module is called just before the driver is removed
607 * from memory.
608 **/
609static void __exit igb_exit_module(void)
610{
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700611#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700612 dca_unregister_notify(&dca_notifier);
613#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800614 pci_unregister_driver(&igb_driver);
615}
616
617module_exit(igb_exit_module);
618
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800619#define Q_IDX_82576(i) (((i & 0x1) << 3) + (i >> 1))
620/**
621 * igb_cache_ring_register - Descriptor ring to register mapping
622 * @adapter: board private structure to initialize
623 *
624 * Once we know the feature-set enabled for the device, we'll cache
625 * the register offset the descriptor ring is assigned to.
626 **/
627static void igb_cache_ring_register(struct igb_adapter *adapter)
628{
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000629 int i = 0, j = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000630 u32 rbase_offset = adapter->vfs_allocated_count;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800631
632 switch (adapter->hw.mac.type) {
633 case e1000_82576:
634 /* The queues are allocated for virtualization such that VF 0
635 * is allocated queues 0 and 8, VF 1 queues 1 and 9, etc.
636 * In order to avoid collision we start at the first free queue
637 * and continue consuming queues in the same sequence
638 */
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000639 if (adapter->vfs_allocated_count) {
Alexander Duycka99955f2009-11-12 18:37:19 +0000640 for (; i < adapter->rss_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000641 adapter->rx_ring[i]->reg_idx = rbase_offset +
642 Q_IDX_82576(i);
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000643 }
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800644 case e1000_82575:
Alexander Duyck55cac242009-11-19 12:42:21 +0000645 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000646 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000647 case e1000_i210:
648 case e1000_i211:
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800649 default:
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000650 for (; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000651 adapter->rx_ring[i]->reg_idx = rbase_offset + i;
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000652 for (; j < adapter->num_tx_queues; j++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000653 adapter->tx_ring[j]->reg_idx = rbase_offset + j;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800654 break;
655 }
656}
657
Alexander Duyck4be000c2011-08-26 07:45:52 +0000658/**
659 * igb_write_ivar - configure ivar for given MSI-X vector
660 * @hw: pointer to the HW structure
661 * @msix_vector: vector number we are allocating to a given ring
662 * @index: row index of IVAR register to write within IVAR table
663 * @offset: column offset of in IVAR, should be multiple of 8
664 *
665 * This function is intended to handle the writing of the IVAR register
666 * for adapters 82576 and newer. The IVAR table consists of 2 columns,
667 * each containing an cause allocation for an Rx and Tx ring, and a
668 * variable number of rows depending on the number of queues supported.
669 **/
670static void igb_write_ivar(struct e1000_hw *hw, int msix_vector,
671 int index, int offset)
672{
673 u32 ivar = array_rd32(E1000_IVAR0, index);
674
675 /* clear any bits that are currently set */
676 ivar &= ~((u32)0xFF << offset);
677
678 /* write vector and valid bit */
679 ivar |= (msix_vector | E1000_IVAR_VALID) << offset;
680
681 array_wr32(E1000_IVAR0, index, ivar);
682}
683
Auke Kok9d5c8242008-01-24 02:22:38 -0800684#define IGB_N0_QUEUE -1
Alexander Duyck047e0032009-10-27 15:49:27 +0000685static void igb_assign_vector(struct igb_q_vector *q_vector, int msix_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -0800686{
Alexander Duyck047e0032009-10-27 15:49:27 +0000687 struct igb_adapter *adapter = q_vector->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -0800688 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck047e0032009-10-27 15:49:27 +0000689 int rx_queue = IGB_N0_QUEUE;
690 int tx_queue = IGB_N0_QUEUE;
Alexander Duyck4be000c2011-08-26 07:45:52 +0000691 u32 msixbm = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000692
Alexander Duyck0ba82992011-08-26 07:45:47 +0000693 if (q_vector->rx.ring)
694 rx_queue = q_vector->rx.ring->reg_idx;
695 if (q_vector->tx.ring)
696 tx_queue = q_vector->tx.ring->reg_idx;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700697
698 switch (hw->mac.type) {
699 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800700 /* The 82575 assigns vectors using a bitmask, which matches the
701 bitmask for the EICR/EIMS/EIMC registers. To assign one
702 or more queues to a vector, we write the appropriate bits
703 into the MSIXBM register for that vector. */
Alexander Duyck047e0032009-10-27 15:49:27 +0000704 if (rx_queue > IGB_N0_QUEUE)
Auke Kok9d5c8242008-01-24 02:22:38 -0800705 msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
Alexander Duyck047e0032009-10-27 15:49:27 +0000706 if (tx_queue > IGB_N0_QUEUE)
Auke Kok9d5c8242008-01-24 02:22:38 -0800707 msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
Alexander Duyckfeeb2722010-02-03 21:59:51 +0000708 if (!adapter->msix_entries && msix_vector == 0)
709 msixbm |= E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800710 array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
Alexander Duyck047e0032009-10-27 15:49:27 +0000711 q_vector->eims_value = msixbm;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700712 break;
713 case e1000_82576:
Alexander Duyck4be000c2011-08-26 07:45:52 +0000714 /*
715 * 82576 uses a table that essentially consists of 2 columns
716 * with 8 rows. The ordering is column-major so we use the
717 * lower 3 bits as the row index, and the 4th bit as the
718 * column offset.
719 */
720 if (rx_queue > IGB_N0_QUEUE)
721 igb_write_ivar(hw, msix_vector,
722 rx_queue & 0x7,
723 (rx_queue & 0x8) << 1);
724 if (tx_queue > IGB_N0_QUEUE)
725 igb_write_ivar(hw, msix_vector,
726 tx_queue & 0x7,
727 ((tx_queue & 0x8) << 1) + 8);
Alexander Duyck047e0032009-10-27 15:49:27 +0000728 q_vector->eims_value = 1 << msix_vector;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700729 break;
Alexander Duyck55cac242009-11-19 12:42:21 +0000730 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000731 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000732 case e1000_i210:
733 case e1000_i211:
Alexander Duyck4be000c2011-08-26 07:45:52 +0000734 /*
735 * On 82580 and newer adapters the scheme is similar to 82576
736 * however instead of ordering column-major we have things
737 * ordered row-major. So we traverse the table by using
738 * bit 0 as the column offset, and the remaining bits as the
739 * row index.
740 */
741 if (rx_queue > IGB_N0_QUEUE)
742 igb_write_ivar(hw, msix_vector,
743 rx_queue >> 1,
744 (rx_queue & 0x1) << 4);
745 if (tx_queue > IGB_N0_QUEUE)
746 igb_write_ivar(hw, msix_vector,
747 tx_queue >> 1,
748 ((tx_queue & 0x1) << 4) + 8);
Alexander Duyck55cac242009-11-19 12:42:21 +0000749 q_vector->eims_value = 1 << msix_vector;
750 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700751 default:
752 BUG();
753 break;
754 }
Alexander Duyck26b39272010-02-17 01:00:41 +0000755
756 /* add q_vector eims value to global eims_enable_mask */
757 adapter->eims_enable_mask |= q_vector->eims_value;
758
759 /* configure q_vector to set itr on first interrupt */
760 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -0800761}
762
763/**
764 * igb_configure_msix - Configure MSI-X hardware
765 *
766 * igb_configure_msix sets up the hardware to properly
767 * generate MSI-X interrupts.
768 **/
769static void igb_configure_msix(struct igb_adapter *adapter)
770{
771 u32 tmp;
772 int i, vector = 0;
773 struct e1000_hw *hw = &adapter->hw;
774
775 adapter->eims_enable_mask = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -0800776
777 /* set vector for other causes, i.e. link changes */
Alexander Duyck2d064c02008-07-08 15:10:12 -0700778 switch (hw->mac.type) {
779 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800780 tmp = rd32(E1000_CTRL_EXT);
781 /* enable MSI-X PBA support*/
782 tmp |= E1000_CTRL_EXT_PBA_CLR;
783
784 /* Auto-Mask interrupts upon ICR read. */
785 tmp |= E1000_CTRL_EXT_EIAME;
786 tmp |= E1000_CTRL_EXT_IRCA;
787
788 wr32(E1000_CTRL_EXT, tmp);
Alexander Duyck047e0032009-10-27 15:49:27 +0000789
790 /* enable msix_other interrupt */
791 array_wr32(E1000_MSIXBM(0), vector++,
792 E1000_EIMS_OTHER);
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700793 adapter->eims_other = E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800794
Alexander Duyck2d064c02008-07-08 15:10:12 -0700795 break;
796
797 case e1000_82576:
Alexander Duyck55cac242009-11-19 12:42:21 +0000798 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000799 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000800 case e1000_i210:
801 case e1000_i211:
Alexander Duyck047e0032009-10-27 15:49:27 +0000802 /* Turn on MSI-X capability first, or our settings
803 * won't stick. And it will take days to debug. */
804 wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
805 E1000_GPIE_PBA | E1000_GPIE_EIAME |
806 E1000_GPIE_NSICR);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700807
Alexander Duyck047e0032009-10-27 15:49:27 +0000808 /* enable msix_other interrupt */
809 adapter->eims_other = 1 << vector;
810 tmp = (vector++ | E1000_IVAR_VALID) << 8;
811
812 wr32(E1000_IVAR_MISC, tmp);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700813 break;
814 default:
815 /* do nothing, since nothing else supports MSI-X */
816 break;
817 } /* switch (hw->mac.type) */
Alexander Duyck047e0032009-10-27 15:49:27 +0000818
819 adapter->eims_enable_mask |= adapter->eims_other;
820
Alexander Duyck26b39272010-02-17 01:00:41 +0000821 for (i = 0; i < adapter->num_q_vectors; i++)
822 igb_assign_vector(adapter->q_vector[i], vector++);
Alexander Duyck047e0032009-10-27 15:49:27 +0000823
Auke Kok9d5c8242008-01-24 02:22:38 -0800824 wrfl();
825}
826
827/**
828 * igb_request_msix - Initialize MSI-X interrupts
829 *
830 * igb_request_msix allocates MSI-X vectors and requests interrupts from the
831 * kernel.
832 **/
833static int igb_request_msix(struct igb_adapter *adapter)
834{
835 struct net_device *netdev = adapter->netdev;
Alexander Duyck047e0032009-10-27 15:49:27 +0000836 struct e1000_hw *hw = &adapter->hw;
Stefan Assmann52285b72012-12-04 06:00:17 +0000837 int i, err = 0, vector = 0, free_vector = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -0800838
Auke Kok9d5c8242008-01-24 02:22:38 -0800839 err = request_irq(adapter->msix_entries[vector].vector,
Joe Perchesa0607fd2009-11-18 23:29:17 -0800840 igb_msix_other, 0, netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800841 if (err)
Stefan Assmann52285b72012-12-04 06:00:17 +0000842 goto err_out;
Alexander Duyck047e0032009-10-27 15:49:27 +0000843
844 for (i = 0; i < adapter->num_q_vectors; i++) {
845 struct igb_q_vector *q_vector = adapter->q_vector[i];
846
Stefan Assmann52285b72012-12-04 06:00:17 +0000847 vector++;
848
Alexander Duyck047e0032009-10-27 15:49:27 +0000849 q_vector->itr_register = hw->hw_addr + E1000_EITR(vector);
850
Alexander Duyck0ba82992011-08-26 07:45:47 +0000851 if (q_vector->rx.ring && q_vector->tx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000852 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000853 q_vector->rx.ring->queue_index);
854 else if (q_vector->tx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000855 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000856 q_vector->tx.ring->queue_index);
857 else if (q_vector->rx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000858 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000859 q_vector->rx.ring->queue_index);
Alexander Duyck047e0032009-10-27 15:49:27 +0000860 else
861 sprintf(q_vector->name, "%s-unused", netdev->name);
862
863 err = request_irq(adapter->msix_entries[vector].vector,
Joe Perchesa0607fd2009-11-18 23:29:17 -0800864 igb_msix_ring, 0, q_vector->name,
Alexander Duyck047e0032009-10-27 15:49:27 +0000865 q_vector);
866 if (err)
Stefan Assmann52285b72012-12-04 06:00:17 +0000867 goto err_free;
Alexander Duyck047e0032009-10-27 15:49:27 +0000868 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800869
Auke Kok9d5c8242008-01-24 02:22:38 -0800870 igb_configure_msix(adapter);
871 return 0;
Stefan Assmann52285b72012-12-04 06:00:17 +0000872
873err_free:
874 /* free already assigned IRQs */
875 free_irq(adapter->msix_entries[free_vector++].vector, adapter);
876
877 vector--;
878 for (i = 0; i < vector; i++) {
879 free_irq(adapter->msix_entries[free_vector++].vector,
880 adapter->q_vector[i]);
881 }
882err_out:
Auke Kok9d5c8242008-01-24 02:22:38 -0800883 return err;
884}
885
886static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
887{
888 if (adapter->msix_entries) {
889 pci_disable_msix(adapter->pdev);
890 kfree(adapter->msix_entries);
891 adapter->msix_entries = NULL;
Alexander Duyck047e0032009-10-27 15:49:27 +0000892 } else if (adapter->flags & IGB_FLAG_HAS_MSI) {
Auke Kok9d5c8242008-01-24 02:22:38 -0800893 pci_disable_msi(adapter->pdev);
Alexander Duyck047e0032009-10-27 15:49:27 +0000894 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800895}
896
Alexander Duyck047e0032009-10-27 15:49:27 +0000897/**
Alexander Duyck5536d212012-09-25 00:31:17 +0000898 * igb_free_q_vector - Free memory allocated for specific interrupt vector
899 * @adapter: board private structure to initialize
900 * @v_idx: Index of vector to be freed
901 *
902 * This function frees the memory allocated to the q_vector. In addition if
903 * NAPI is enabled it will delete any references to the NAPI struct prior
904 * to freeing the q_vector.
905 **/
906static void igb_free_q_vector(struct igb_adapter *adapter, int v_idx)
907{
908 struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
909
910 if (q_vector->tx.ring)
911 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
912
913 if (q_vector->rx.ring)
914 adapter->tx_ring[q_vector->rx.ring->queue_index] = NULL;
915
916 adapter->q_vector[v_idx] = NULL;
917 netif_napi_del(&q_vector->napi);
918
919 /*
920 * ixgbe_get_stats64() might access the rings on this vector,
921 * we must wait a grace period before freeing it.
922 */
923 kfree_rcu(q_vector, rcu);
924}
925
926/**
Alexander Duyck047e0032009-10-27 15:49:27 +0000927 * igb_free_q_vectors - Free memory allocated for interrupt vectors
928 * @adapter: board private structure to initialize
929 *
930 * This function frees the memory allocated to the q_vectors. In addition if
931 * NAPI is enabled it will delete any references to the NAPI struct prior
932 * to freeing the q_vector.
933 **/
934static void igb_free_q_vectors(struct igb_adapter *adapter)
935{
Alexander Duyck5536d212012-09-25 00:31:17 +0000936 int v_idx = adapter->num_q_vectors;
Alexander Duyck047e0032009-10-27 15:49:27 +0000937
Alexander Duyck5536d212012-09-25 00:31:17 +0000938 adapter->num_tx_queues = 0;
939 adapter->num_rx_queues = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000940 adapter->num_q_vectors = 0;
Alexander Duyck5536d212012-09-25 00:31:17 +0000941
942 while (v_idx--)
943 igb_free_q_vector(adapter, v_idx);
Alexander Duyck047e0032009-10-27 15:49:27 +0000944}
945
946/**
947 * igb_clear_interrupt_scheme - reset the device to a state of no interrupts
948 *
949 * This function resets the device so that it has 0 rx queues, tx queues, and
950 * MSI-X interrupts allocated.
951 */
952static void igb_clear_interrupt_scheme(struct igb_adapter *adapter)
953{
Alexander Duyck047e0032009-10-27 15:49:27 +0000954 igb_free_q_vectors(adapter);
955 igb_reset_interrupt_capability(adapter);
956}
Auke Kok9d5c8242008-01-24 02:22:38 -0800957
958/**
959 * igb_set_interrupt_capability - set MSI or MSI-X if supported
960 *
961 * Attempt to configure interrupts using the best available
962 * capabilities of the hardware and kernel.
963 **/
Stefan Assmann53c7d062012-12-04 06:00:12 +0000964static void igb_set_interrupt_capability(struct igb_adapter *adapter, bool msix)
Auke Kok9d5c8242008-01-24 02:22:38 -0800965{
966 int err;
967 int numvecs, i;
968
Stefan Assmann53c7d062012-12-04 06:00:12 +0000969 if (!msix)
970 goto msi_only;
971
Alexander Duyck83b71802009-02-06 23:15:45 +0000972 /* Number of supported queues. */
Alexander Duycka99955f2009-11-12 18:37:19 +0000973 adapter->num_rx_queues = adapter->rss_queues;
Greg Rose5fa85172010-07-01 13:38:16 +0000974 if (adapter->vfs_allocated_count)
975 adapter->num_tx_queues = 1;
976 else
977 adapter->num_tx_queues = adapter->rss_queues;
Alexander Duyck83b71802009-02-06 23:15:45 +0000978
Alexander Duyck047e0032009-10-27 15:49:27 +0000979 /* start with one vector for every rx queue */
980 numvecs = adapter->num_rx_queues;
981
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800982 /* if tx handler is separate add 1 for every tx queue */
Alexander Duycka99955f2009-11-12 18:37:19 +0000983 if (!(adapter->flags & IGB_FLAG_QUEUE_PAIRS))
984 numvecs += adapter->num_tx_queues;
Alexander Duyck047e0032009-10-27 15:49:27 +0000985
986 /* store the number of vectors reserved for queues */
987 adapter->num_q_vectors = numvecs;
988
989 /* add 1 vector for link status interrupts */
990 numvecs++;
Auke Kok9d5c8242008-01-24 02:22:38 -0800991 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
992 GFP_KERNEL);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000993
Auke Kok9d5c8242008-01-24 02:22:38 -0800994 if (!adapter->msix_entries)
995 goto msi_only;
996
997 for (i = 0; i < numvecs; i++)
998 adapter->msix_entries[i].entry = i;
999
1000 err = pci_enable_msix(adapter->pdev,
1001 adapter->msix_entries,
1002 numvecs);
1003 if (err == 0)
Alexander Duyck0c2cc022012-09-25 00:31:22 +00001004 return;
Auke Kok9d5c8242008-01-24 02:22:38 -08001005
1006 igb_reset_interrupt_capability(adapter);
1007
1008 /* If we can't do MSI-X, try MSI */
1009msi_only:
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001010#ifdef CONFIG_PCI_IOV
1011 /* disable SR-IOV for non MSI-X configurations */
1012 if (adapter->vf_data) {
1013 struct e1000_hw *hw = &adapter->hw;
1014 /* disable iov and allow time for transactions to clear */
1015 pci_disable_sriov(adapter->pdev);
1016 msleep(500);
1017
1018 kfree(adapter->vf_data);
1019 adapter->vf_data = NULL;
1020 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001021 wrfl();
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001022 msleep(100);
1023 dev_info(&adapter->pdev->dev, "IOV Disabled\n");
1024 }
1025#endif
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00001026 adapter->vfs_allocated_count = 0;
Alexander Duycka99955f2009-11-12 18:37:19 +00001027 adapter->rss_queues = 1;
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00001028 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
Auke Kok9d5c8242008-01-24 02:22:38 -08001029 adapter->num_rx_queues = 1;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001030 adapter->num_tx_queues = 1;
Alexander Duyck047e0032009-10-27 15:49:27 +00001031 adapter->num_q_vectors = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08001032 if (!pci_enable_msi(adapter->pdev))
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001033 adapter->flags |= IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -08001034}
1035
Alexander Duyck5536d212012-09-25 00:31:17 +00001036static void igb_add_ring(struct igb_ring *ring,
1037 struct igb_ring_container *head)
1038{
1039 head->ring = ring;
1040 head->count++;
1041}
1042
1043/**
1044 * igb_alloc_q_vector - Allocate memory for a single interrupt vector
1045 * @adapter: board private structure to initialize
1046 * @v_count: q_vectors allocated on adapter, used for ring interleaving
1047 * @v_idx: index of vector in adapter struct
1048 * @txr_count: total number of Tx rings to allocate
1049 * @txr_idx: index of first Tx ring to allocate
1050 * @rxr_count: total number of Rx rings to allocate
1051 * @rxr_idx: index of first Rx ring to allocate
1052 *
1053 * We allocate one q_vector. If allocation fails we return -ENOMEM.
1054 **/
1055static int igb_alloc_q_vector(struct igb_adapter *adapter,
1056 int v_count, int v_idx,
1057 int txr_count, int txr_idx,
1058 int rxr_count, int rxr_idx)
1059{
1060 struct igb_q_vector *q_vector;
1061 struct igb_ring *ring;
1062 int ring_count, size;
1063
1064 /* igb only supports 1 Tx and/or 1 Rx queue per vector */
1065 if (txr_count > 1 || rxr_count > 1)
1066 return -ENOMEM;
1067
1068 ring_count = txr_count + rxr_count;
1069 size = sizeof(struct igb_q_vector) +
1070 (sizeof(struct igb_ring) * ring_count);
1071
1072 /* allocate q_vector and rings */
1073 q_vector = kzalloc(size, GFP_KERNEL);
1074 if (!q_vector)
1075 return -ENOMEM;
1076
1077 /* initialize NAPI */
1078 netif_napi_add(adapter->netdev, &q_vector->napi,
1079 igb_poll, 64);
1080
1081 /* tie q_vector and adapter together */
1082 adapter->q_vector[v_idx] = q_vector;
1083 q_vector->adapter = adapter;
1084
1085 /* initialize work limits */
1086 q_vector->tx.work_limit = adapter->tx_work_limit;
1087
1088 /* initialize ITR configuration */
1089 q_vector->itr_register = adapter->hw.hw_addr + E1000_EITR(0);
1090 q_vector->itr_val = IGB_START_ITR;
1091
1092 /* initialize pointer to rings */
1093 ring = q_vector->ring;
1094
1095 if (txr_count) {
1096 /* assign generic ring traits */
1097 ring->dev = &adapter->pdev->dev;
1098 ring->netdev = adapter->netdev;
1099
1100 /* configure backlink on ring */
1101 ring->q_vector = q_vector;
1102
1103 /* update q_vector Tx values */
1104 igb_add_ring(ring, &q_vector->tx);
1105
1106 /* For 82575, context index must be unique per ring. */
1107 if (adapter->hw.mac.type == e1000_82575)
1108 set_bit(IGB_RING_FLAG_TX_CTX_IDX, &ring->flags);
1109
1110 /* apply Tx specific ring traits */
1111 ring->count = adapter->tx_ring_count;
1112 ring->queue_index = txr_idx;
1113
1114 /* assign ring to adapter */
1115 adapter->tx_ring[txr_idx] = ring;
1116
1117 /* push pointer to next ring */
1118 ring++;
1119 }
1120
1121 if (rxr_count) {
1122 /* assign generic ring traits */
1123 ring->dev = &adapter->pdev->dev;
1124 ring->netdev = adapter->netdev;
1125
1126 /* configure backlink on ring */
1127 ring->q_vector = q_vector;
1128
1129 /* update q_vector Rx values */
1130 igb_add_ring(ring, &q_vector->rx);
1131
1132 /* set flag indicating ring supports SCTP checksum offload */
1133 if (adapter->hw.mac.type >= e1000_82576)
1134 set_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags);
1135
1136 /*
1137 * On i350, i210, and i211, loopback VLAN packets
1138 * have the tag byte-swapped.
1139 * */
1140 if (adapter->hw.mac.type >= e1000_i350)
1141 set_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &ring->flags);
1142
1143 /* apply Rx specific ring traits */
1144 ring->count = adapter->rx_ring_count;
1145 ring->queue_index = rxr_idx;
1146
1147 /* assign ring to adapter */
1148 adapter->rx_ring[rxr_idx] = ring;
1149 }
1150
1151 return 0;
1152}
1153
1154
Auke Kok9d5c8242008-01-24 02:22:38 -08001155/**
Alexander Duyck047e0032009-10-27 15:49:27 +00001156 * igb_alloc_q_vectors - Allocate memory for interrupt vectors
1157 * @adapter: board private structure to initialize
1158 *
1159 * We allocate one q_vector per queue interrupt. If allocation fails we
1160 * return -ENOMEM.
1161 **/
1162static int igb_alloc_q_vectors(struct igb_adapter *adapter)
1163{
Alexander Duyck5536d212012-09-25 00:31:17 +00001164 int q_vectors = adapter->num_q_vectors;
1165 int rxr_remaining = adapter->num_rx_queues;
1166 int txr_remaining = adapter->num_tx_queues;
1167 int rxr_idx = 0, txr_idx = 0, v_idx = 0;
1168 int err;
Alexander Duyck047e0032009-10-27 15:49:27 +00001169
Alexander Duyck5536d212012-09-25 00:31:17 +00001170 if (q_vectors >= (rxr_remaining + txr_remaining)) {
1171 for (; rxr_remaining; v_idx++) {
1172 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1173 0, 0, 1, rxr_idx);
1174
1175 if (err)
1176 goto err_out;
1177
1178 /* update counts and index */
1179 rxr_remaining--;
1180 rxr_idx++;
1181 }
1182 }
1183
1184 for (; v_idx < q_vectors; v_idx++) {
1185 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
1186 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
1187 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1188 tqpv, txr_idx, rqpv, rxr_idx);
1189
1190 if (err)
Alexander Duyck047e0032009-10-27 15:49:27 +00001191 goto err_out;
Alexander Duyck5536d212012-09-25 00:31:17 +00001192
1193 /* update counts and index */
1194 rxr_remaining -= rqpv;
1195 txr_remaining -= tqpv;
1196 rxr_idx++;
1197 txr_idx++;
Alexander Duyck047e0032009-10-27 15:49:27 +00001198 }
Alexander Duyck81c2fc22011-08-26 07:45:20 +00001199
Alexander Duyck047e0032009-10-27 15:49:27 +00001200 return 0;
1201
1202err_out:
Alexander Duyck5536d212012-09-25 00:31:17 +00001203 adapter->num_tx_queues = 0;
1204 adapter->num_rx_queues = 0;
1205 adapter->num_q_vectors = 0;
1206
1207 while (v_idx--)
1208 igb_free_q_vector(adapter, v_idx);
1209
Alexander Duyck047e0032009-10-27 15:49:27 +00001210 return -ENOMEM;
1211}
1212
Alexander Duyck047e0032009-10-27 15:49:27 +00001213/**
1214 * igb_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
1215 *
1216 * This function initializes the interrupts and allocates all of the queues.
1217 **/
Stefan Assmann53c7d062012-12-04 06:00:12 +00001218static int igb_init_interrupt_scheme(struct igb_adapter *adapter, bool msix)
Alexander Duyck047e0032009-10-27 15:49:27 +00001219{
1220 struct pci_dev *pdev = adapter->pdev;
1221 int err;
1222
Stefan Assmann53c7d062012-12-04 06:00:12 +00001223 igb_set_interrupt_capability(adapter, msix);
Alexander Duyck047e0032009-10-27 15:49:27 +00001224
1225 err = igb_alloc_q_vectors(adapter);
1226 if (err) {
1227 dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
1228 goto err_alloc_q_vectors;
1229 }
1230
Alexander Duyck5536d212012-09-25 00:31:17 +00001231 igb_cache_ring_register(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00001232
1233 return 0;
Alexander Duyck5536d212012-09-25 00:31:17 +00001234
Alexander Duyck047e0032009-10-27 15:49:27 +00001235err_alloc_q_vectors:
1236 igb_reset_interrupt_capability(adapter);
1237 return err;
1238}
1239
1240/**
Auke Kok9d5c8242008-01-24 02:22:38 -08001241 * igb_request_irq - initialize interrupts
1242 *
1243 * Attempts to configure interrupts using the best available
1244 * capabilities of the hardware and kernel.
1245 **/
1246static int igb_request_irq(struct igb_adapter *adapter)
1247{
1248 struct net_device *netdev = adapter->netdev;
Alexander Duyck047e0032009-10-27 15:49:27 +00001249 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08001250 int err = 0;
1251
1252 if (adapter->msix_entries) {
1253 err = igb_request_msix(adapter);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001254 if (!err)
Auke Kok9d5c8242008-01-24 02:22:38 -08001255 goto request_done;
Auke Kok9d5c8242008-01-24 02:22:38 -08001256 /* fall back to MSI */
Alexander Duyck5536d212012-09-25 00:31:17 +00001257 igb_free_all_tx_resources(adapter);
1258 igb_free_all_rx_resources(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001259
Alexander Duyck047e0032009-10-27 15:49:27 +00001260 igb_clear_interrupt_scheme(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001261 err = igb_init_interrupt_scheme(adapter, false);
1262 if (err)
Alexander Duyck047e0032009-10-27 15:49:27 +00001263 goto request_done;
Stefan Assmann53c7d062012-12-04 06:00:12 +00001264
Alexander Duyck047e0032009-10-27 15:49:27 +00001265 igb_setup_all_tx_resources(adapter);
1266 igb_setup_all_rx_resources(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001267 igb_configure(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001268 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001269
Alexander Duyckc74d5882011-08-26 07:46:45 +00001270 igb_assign_vector(adapter->q_vector[0], 0);
1271
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001272 if (adapter->flags & IGB_FLAG_HAS_MSI) {
Alexander Duyckc74d5882011-08-26 07:46:45 +00001273 err = request_irq(pdev->irq, igb_intr_msi, 0,
Alexander Duyck047e0032009-10-27 15:49:27 +00001274 netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001275 if (!err)
1276 goto request_done;
Alexander Duyck047e0032009-10-27 15:49:27 +00001277
Auke Kok9d5c8242008-01-24 02:22:38 -08001278 /* fall back to legacy interrupts */
1279 igb_reset_interrupt_capability(adapter);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001280 adapter->flags &= ~IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -08001281 }
1282
Alexander Duyckc74d5882011-08-26 07:46:45 +00001283 err = request_irq(pdev->irq, igb_intr, IRQF_SHARED,
Alexander Duyck047e0032009-10-27 15:49:27 +00001284 netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001285
Andy Gospodarek6cb5e572008-02-15 14:05:25 -08001286 if (err)
Alexander Duyckc74d5882011-08-26 07:46:45 +00001287 dev_err(&pdev->dev, "Error %d getting interrupt\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08001288 err);
Auke Kok9d5c8242008-01-24 02:22:38 -08001289
1290request_done:
1291 return err;
1292}
1293
1294static void igb_free_irq(struct igb_adapter *adapter)
1295{
Auke Kok9d5c8242008-01-24 02:22:38 -08001296 if (adapter->msix_entries) {
1297 int vector = 0, i;
1298
Alexander Duyck047e0032009-10-27 15:49:27 +00001299 free_irq(adapter->msix_entries[vector++].vector, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001300
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001301 for (i = 0; i < adapter->num_q_vectors; i++)
Alexander Duyck047e0032009-10-27 15:49:27 +00001302 free_irq(adapter->msix_entries[vector++].vector,
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001303 adapter->q_vector[i]);
Alexander Duyck047e0032009-10-27 15:49:27 +00001304 } else {
1305 free_irq(adapter->pdev->irq, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001306 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001307}
1308
1309/**
1310 * igb_irq_disable - Mask off interrupt generation on the NIC
1311 * @adapter: board private structure
1312 **/
1313static void igb_irq_disable(struct igb_adapter *adapter)
1314{
1315 struct e1000_hw *hw = &adapter->hw;
1316
Alexander Duyck25568a52009-10-27 23:49:59 +00001317 /*
1318 * we need to be careful when disabling interrupts. The VFs are also
1319 * mapped into these registers and so clearing the bits can cause
1320 * issues on the VF drivers so we only need to clear what we set
1321 */
Auke Kok9d5c8242008-01-24 02:22:38 -08001322 if (adapter->msix_entries) {
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001323 u32 regval = rd32(E1000_EIAM);
1324 wr32(E1000_EIAM, regval & ~adapter->eims_enable_mask);
1325 wr32(E1000_EIMC, adapter->eims_enable_mask);
1326 regval = rd32(E1000_EIAC);
1327 wr32(E1000_EIAC, regval & ~adapter->eims_enable_mask);
Auke Kok9d5c8242008-01-24 02:22:38 -08001328 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001329
1330 wr32(E1000_IAM, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08001331 wr32(E1000_IMC, ~0);
1332 wrfl();
Emil Tantilov81a61852010-08-02 14:40:52 +00001333 if (adapter->msix_entries) {
1334 int i;
1335 for (i = 0; i < adapter->num_q_vectors; i++)
1336 synchronize_irq(adapter->msix_entries[i].vector);
1337 } else {
1338 synchronize_irq(adapter->pdev->irq);
1339 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001340}
1341
1342/**
1343 * igb_irq_enable - Enable default interrupt generation settings
1344 * @adapter: board private structure
1345 **/
1346static void igb_irq_enable(struct igb_adapter *adapter)
1347{
1348 struct e1000_hw *hw = &adapter->hw;
1349
1350 if (adapter->msix_entries) {
Alexander Duyck06218a82011-08-26 07:46:55 +00001351 u32 ims = E1000_IMS_LSC | E1000_IMS_DOUTSYNC | E1000_IMS_DRSTA;
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001352 u32 regval = rd32(E1000_EIAC);
1353 wr32(E1000_EIAC, regval | adapter->eims_enable_mask);
1354 regval = rd32(E1000_EIAM);
1355 wr32(E1000_EIAM, regval | adapter->eims_enable_mask);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001356 wr32(E1000_EIMS, adapter->eims_enable_mask);
Alexander Duyck25568a52009-10-27 23:49:59 +00001357 if (adapter->vfs_allocated_count) {
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001358 wr32(E1000_MBVFIMR, 0xFF);
Alexander Duyck25568a52009-10-27 23:49:59 +00001359 ims |= E1000_IMS_VMMB;
1360 }
1361 wr32(E1000_IMS, ims);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001362 } else {
Alexander Duyck55cac242009-11-19 12:42:21 +00001363 wr32(E1000_IMS, IMS_ENABLE_MASK |
1364 E1000_IMS_DRSTA);
1365 wr32(E1000_IAM, IMS_ENABLE_MASK |
1366 E1000_IMS_DRSTA);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001367 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001368}
1369
1370static void igb_update_mng_vlan(struct igb_adapter *adapter)
1371{
Alexander Duyck51466232009-10-27 23:47:35 +00001372 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08001373 u16 vid = adapter->hw.mng_cookie.vlan_id;
1374 u16 old_vid = adapter->mng_vlan_id;
Auke Kok9d5c8242008-01-24 02:22:38 -08001375
Alexander Duyck51466232009-10-27 23:47:35 +00001376 if (hw->mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
1377 /* add VID to filter table */
1378 igb_vfta_set(hw, vid, true);
1379 adapter->mng_vlan_id = vid;
1380 } else {
1381 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
1382 }
1383
1384 if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
1385 (vid != old_vid) &&
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001386 !test_bit(old_vid, adapter->active_vlans)) {
Alexander Duyck51466232009-10-27 23:47:35 +00001387 /* remove VID from filter table */
1388 igb_vfta_set(hw, old_vid, false);
Auke Kok9d5c8242008-01-24 02:22:38 -08001389 }
1390}
1391
1392/**
1393 * igb_release_hw_control - release control of the h/w to f/w
1394 * @adapter: address of board private structure
1395 *
1396 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
1397 * For ASF and Pass Through versions of f/w this means that the
1398 * driver is no longer loaded.
1399 *
1400 **/
1401static void igb_release_hw_control(struct igb_adapter *adapter)
1402{
1403 struct e1000_hw *hw = &adapter->hw;
1404 u32 ctrl_ext;
1405
1406 /* Let firmware take over control of h/w */
1407 ctrl_ext = rd32(E1000_CTRL_EXT);
1408 wr32(E1000_CTRL_EXT,
1409 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
1410}
1411
Auke Kok9d5c8242008-01-24 02:22:38 -08001412/**
1413 * igb_get_hw_control - get control of the h/w from f/w
1414 * @adapter: address of board private structure
1415 *
1416 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
1417 * For ASF and Pass Through versions of f/w this means that
1418 * the driver is loaded.
1419 *
1420 **/
1421static void igb_get_hw_control(struct igb_adapter *adapter)
1422{
1423 struct e1000_hw *hw = &adapter->hw;
1424 u32 ctrl_ext;
1425
1426 /* Let firmware know the driver has taken over */
1427 ctrl_ext = rd32(E1000_CTRL_EXT);
1428 wr32(E1000_CTRL_EXT,
1429 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
1430}
1431
Auke Kok9d5c8242008-01-24 02:22:38 -08001432/**
1433 * igb_configure - configure the hardware for RX and TX
1434 * @adapter: private board structure
1435 **/
1436static void igb_configure(struct igb_adapter *adapter)
1437{
1438 struct net_device *netdev = adapter->netdev;
1439 int i;
1440
1441 igb_get_hw_control(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001442 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001443
1444 igb_restore_vlan(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001445
Alexander Duyck85b430b2009-10-27 15:50:29 +00001446 igb_setup_tctl(adapter);
Alexander Duyck06cf2662009-10-27 15:53:25 +00001447 igb_setup_mrqc(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001448 igb_setup_rctl(adapter);
Alexander Duyck85b430b2009-10-27 15:50:29 +00001449
1450 igb_configure_tx(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001451 igb_configure_rx(adapter);
Alexander Duyck662d7202008-06-27 11:00:29 -07001452
1453 igb_rx_fifo_flush_82575(&adapter->hw);
1454
Alexander Duyckc493ea42009-03-20 00:16:50 +00001455 /* call igb_desc_unused which always leaves
Auke Kok9d5c8242008-01-24 02:22:38 -08001456 * at least 1 descriptor unused to make sure
1457 * next_to_use != next_to_clean */
1458 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00001459 struct igb_ring *ring = adapter->rx_ring[i];
Alexander Duyckcd392f52011-08-26 07:43:59 +00001460 igb_alloc_rx_buffers(ring, igb_desc_unused(ring));
Auke Kok9d5c8242008-01-24 02:22:38 -08001461 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001462}
1463
Nick Nunley88a268c2010-02-17 01:01:59 +00001464/**
1465 * igb_power_up_link - Power up the phy/serdes link
1466 * @adapter: address of board private structure
1467 **/
1468void igb_power_up_link(struct igb_adapter *adapter)
1469{
Akeem G. Abodunrin76886592012-07-17 04:51:18 +00001470 igb_reset_phy(&adapter->hw);
1471
Nick Nunley88a268c2010-02-17 01:01:59 +00001472 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1473 igb_power_up_phy_copper(&adapter->hw);
1474 else
1475 igb_power_up_serdes_link_82575(&adapter->hw);
1476}
1477
1478/**
1479 * igb_power_down_link - Power down the phy/serdes link
1480 * @adapter: address of board private structure
1481 */
1482static void igb_power_down_link(struct igb_adapter *adapter)
1483{
1484 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1485 igb_power_down_phy_copper_82575(&adapter->hw);
1486 else
1487 igb_shutdown_serdes_link_82575(&adapter->hw);
1488}
Auke Kok9d5c8242008-01-24 02:22:38 -08001489
1490/**
1491 * igb_up - Open the interface and prepare it to handle traffic
1492 * @adapter: board private structure
1493 **/
Auke Kok9d5c8242008-01-24 02:22:38 -08001494int igb_up(struct igb_adapter *adapter)
1495{
1496 struct e1000_hw *hw = &adapter->hw;
1497 int i;
1498
1499 /* hardware has been reset, we need to reload some things */
1500 igb_configure(adapter);
1501
1502 clear_bit(__IGB_DOWN, &adapter->state);
1503
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001504 for (i = 0; i < adapter->num_q_vectors; i++)
1505 napi_enable(&(adapter->q_vector[i]->napi));
1506
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001507 if (adapter->msix_entries)
Auke Kok9d5c8242008-01-24 02:22:38 -08001508 igb_configure_msix(adapter);
Alexander Duyckfeeb2722010-02-03 21:59:51 +00001509 else
1510 igb_assign_vector(adapter->q_vector[0], 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08001511
1512 /* Clear any pending interrupts. */
1513 rd32(E1000_ICR);
1514 igb_irq_enable(adapter);
1515
Alexander Duyckd4960302009-10-27 15:53:45 +00001516 /* notify VFs that reset has been completed */
1517 if (adapter->vfs_allocated_count) {
1518 u32 reg_data = rd32(E1000_CTRL_EXT);
1519 reg_data |= E1000_CTRL_EXT_PFRSTD;
1520 wr32(E1000_CTRL_EXT, reg_data);
1521 }
1522
Jesse Brandeburg4cb9be72009-04-21 18:42:05 +00001523 netif_tx_start_all_queues(adapter->netdev);
1524
Alexander Duyck25568a52009-10-27 23:49:59 +00001525 /* start the watchdog. */
1526 hw->mac.get_link_status = 1;
1527 schedule_work(&adapter->watchdog_task);
1528
Auke Kok9d5c8242008-01-24 02:22:38 -08001529 return 0;
1530}
1531
1532void igb_down(struct igb_adapter *adapter)
1533{
Auke Kok9d5c8242008-01-24 02:22:38 -08001534 struct net_device *netdev = adapter->netdev;
Alexander Duyck330a6d62009-10-27 23:51:35 +00001535 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08001536 u32 tctl, rctl;
1537 int i;
1538
1539 /* signal that we're down so the interrupt handler does not
1540 * reschedule our watchdog timer */
1541 set_bit(__IGB_DOWN, &adapter->state);
1542
1543 /* disable receives in the hardware */
1544 rctl = rd32(E1000_RCTL);
1545 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
1546 /* flush and sleep below */
1547
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001548 netif_tx_stop_all_queues(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001549
1550 /* disable transmits in the hardware */
1551 tctl = rd32(E1000_TCTL);
1552 tctl &= ~E1000_TCTL_EN;
1553 wr32(E1000_TCTL, tctl);
1554 /* flush both disables and wait for them to finish */
1555 wrfl();
1556 msleep(10);
1557
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001558 for (i = 0; i < adapter->num_q_vectors; i++)
1559 napi_disable(&(adapter->q_vector[i]->napi));
Auke Kok9d5c8242008-01-24 02:22:38 -08001560
Auke Kok9d5c8242008-01-24 02:22:38 -08001561 igb_irq_disable(adapter);
1562
1563 del_timer_sync(&adapter->watchdog_timer);
1564 del_timer_sync(&adapter->phy_info_timer);
1565
Auke Kok9d5c8242008-01-24 02:22:38 -08001566 netif_carrier_off(netdev);
Alexander Duyck04fe6352009-02-06 23:22:32 +00001567
1568 /* record the stats before reset*/
Eric Dumazet12dcd862010-10-15 17:27:10 +00001569 spin_lock(&adapter->stats64_lock);
1570 igb_update_stats(adapter, &adapter->stats64);
1571 spin_unlock(&adapter->stats64_lock);
Alexander Duyck04fe6352009-02-06 23:22:32 +00001572
Auke Kok9d5c8242008-01-24 02:22:38 -08001573 adapter->link_speed = 0;
1574 adapter->link_duplex = 0;
1575
Jeff Kirsher30236822008-06-24 17:01:15 -07001576 if (!pci_channel_offline(adapter->pdev))
1577 igb_reset(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001578 igb_clean_all_tx_rings(adapter);
1579 igb_clean_all_rx_rings(adapter);
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00001580#ifdef CONFIG_IGB_DCA
1581
1582 /* since we reset the hardware DCA settings were cleared */
1583 igb_setup_dca(adapter);
1584#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08001585}
1586
1587void igb_reinit_locked(struct igb_adapter *adapter)
1588{
1589 WARN_ON(in_interrupt());
1590 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
1591 msleep(1);
1592 igb_down(adapter);
1593 igb_up(adapter);
1594 clear_bit(__IGB_RESETTING, &adapter->state);
1595}
1596
1597void igb_reset(struct igb_adapter *adapter)
1598{
Alexander Duyck090b1792009-10-27 23:51:55 +00001599 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08001600 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001601 struct e1000_mac_info *mac = &hw->mac;
1602 struct e1000_fc_info *fc = &hw->fc;
Matthew Vickd48507f2012-11-08 04:03:58 +00001603 u32 pba = 0, tx_space, min_tx_space, min_rx_space, hwm;
Auke Kok9d5c8242008-01-24 02:22:38 -08001604
1605 /* Repartition Pba for greater than 9k mtu
1606 * To take effect CTRL.RST is required.
1607 */
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001608 switch (mac->type) {
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +00001609 case e1000_i350:
Alexander Duyck55cac242009-11-19 12:42:21 +00001610 case e1000_82580:
1611 pba = rd32(E1000_RXPBS);
1612 pba = igb_rxpbs_adjust_82580(pba);
1613 break;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001614 case e1000_82576:
Alexander Duyckd249be52009-10-27 23:46:38 +00001615 pba = rd32(E1000_RXPBS);
1616 pba &= E1000_RXPBS_SIZE_MASK_82576;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001617 break;
1618 case e1000_82575:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001619 case e1000_i210:
1620 case e1000_i211:
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001621 default:
1622 pba = E1000_PBA_34K;
1623 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001624 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001625
Alexander Duyck2d064c02008-07-08 15:10:12 -07001626 if ((adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) &&
1627 (mac->type < e1000_82576)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001628 /* adjust PBA for jumbo frames */
1629 wr32(E1000_PBA, pba);
1630
1631 /* To maintain wire speed transmits, the Tx FIFO should be
1632 * large enough to accommodate two full transmit packets,
1633 * rounded up to the next 1KB and expressed in KB. Likewise,
1634 * the Rx FIFO should be large enough to accommodate at least
1635 * one full receive packet and is similarly rounded up and
1636 * expressed in KB. */
1637 pba = rd32(E1000_PBA);
1638 /* upper 16 bits has Tx packet buffer allocation size in KB */
1639 tx_space = pba >> 16;
1640 /* lower 16 bits has Rx packet buffer allocation size in KB */
1641 pba &= 0xffff;
1642 /* the tx fifo also stores 16 bytes of information about the tx
1643 * but don't include ethernet FCS because hardware appends it */
1644 min_tx_space = (adapter->max_frame_size +
Alexander Duyck85e8d002009-02-16 00:00:20 -08001645 sizeof(union e1000_adv_tx_desc) -
Auke Kok9d5c8242008-01-24 02:22:38 -08001646 ETH_FCS_LEN) * 2;
1647 min_tx_space = ALIGN(min_tx_space, 1024);
1648 min_tx_space >>= 10;
1649 /* software strips receive CRC, so leave room for it */
1650 min_rx_space = adapter->max_frame_size;
1651 min_rx_space = ALIGN(min_rx_space, 1024);
1652 min_rx_space >>= 10;
1653
1654 /* If current Tx allocation is less than the min Tx FIFO size,
1655 * and the min Tx FIFO size is less than the current Rx FIFO
1656 * allocation, take space away from current Rx allocation */
1657 if (tx_space < min_tx_space &&
1658 ((min_tx_space - tx_space) < pba)) {
1659 pba = pba - (min_tx_space - tx_space);
1660
1661 /* if short on rx space, rx wins and must trump tx
1662 * adjustment */
1663 if (pba < min_rx_space)
1664 pba = min_rx_space;
1665 }
Alexander Duyck2d064c02008-07-08 15:10:12 -07001666 wr32(E1000_PBA, pba);
Auke Kok9d5c8242008-01-24 02:22:38 -08001667 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001668
1669 /* flow control settings */
1670 /* The high water mark must be low enough to fit one full frame
1671 * (or the size used for early receive) above it in the Rx FIFO.
1672 * Set it to the lower of:
1673 * - 90% of the Rx FIFO size, or
1674 * - the full Rx FIFO size minus one full frame */
1675 hwm = min(((pba << 10) * 9 / 10),
Alexander Duyck2d064c02008-07-08 15:10:12 -07001676 ((pba << 10) - 2 * adapter->max_frame_size));
Auke Kok9d5c8242008-01-24 02:22:38 -08001677
Matthew Vickd48507f2012-11-08 04:03:58 +00001678 fc->high_water = hwm & 0xFFFFFFF0; /* 16-byte granularity */
Alexander Duyckd405ea32009-12-23 13:21:27 +00001679 fc->low_water = fc->high_water - 16;
Auke Kok9d5c8242008-01-24 02:22:38 -08001680 fc->pause_time = 0xFFFF;
1681 fc->send_xon = 1;
Alexander Duyck0cce1192009-07-23 18:10:24 +00001682 fc->current_mode = fc->requested_mode;
Auke Kok9d5c8242008-01-24 02:22:38 -08001683
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001684 /* disable receive for all VFs and wait one second */
1685 if (adapter->vfs_allocated_count) {
1686 int i;
1687 for (i = 0 ; i < adapter->vfs_allocated_count; i++)
Greg Rose8fa7e0f2010-11-06 05:43:21 +00001688 adapter->vf_data[i].flags &= IGB_VF_FLAG_PF_SET_MAC;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001689
1690 /* ping all the active vfs to let them know we are going down */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00001691 igb_ping_all_vfs(adapter);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001692
1693 /* disable transmits and receives */
1694 wr32(E1000_VFRE, 0);
1695 wr32(E1000_VFTE, 0);
1696 }
1697
Auke Kok9d5c8242008-01-24 02:22:38 -08001698 /* Allow time for pending master requests to run */
Alexander Duyck330a6d62009-10-27 23:51:35 +00001699 hw->mac.ops.reset_hw(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001700 wr32(E1000_WUC, 0);
1701
Alexander Duyck330a6d62009-10-27 23:51:35 +00001702 if (hw->mac.ops.init_hw(hw))
Alexander Duyck090b1792009-10-27 23:51:55 +00001703 dev_err(&pdev->dev, "Hardware Error\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08001704
Matthew Vicka27416b2012-04-18 02:57:44 +00001705 /*
1706 * Flow control settings reset on hardware reset, so guarantee flow
1707 * control is off when forcing speed.
1708 */
1709 if (!hw->mac.autoneg)
1710 igb_force_mac_fc(hw);
1711
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00001712 igb_init_dmac(adapter, pba);
Nick Nunley88a268c2010-02-17 01:01:59 +00001713 if (!netif_running(adapter->netdev))
1714 igb_power_down_link(adapter);
1715
Auke Kok9d5c8242008-01-24 02:22:38 -08001716 igb_update_mng_vlan(adapter);
1717
1718 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
1719 wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
1720
Matthew Vick1f6e8172012-08-18 07:26:33 +00001721 /* Re-enable PTP, where applicable. */
1722 igb_ptp_reset(adapter);
Matthew Vick1f6e8172012-08-18 07:26:33 +00001723
Alexander Duyck330a6d62009-10-27 23:51:35 +00001724 igb_get_phy_info(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001725}
1726
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001727static netdev_features_t igb_fix_features(struct net_device *netdev,
1728 netdev_features_t features)
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001729{
1730 /*
1731 * Since there is no support for separate rx/tx vlan accel
1732 * enable/disable make sure tx flag is always in same state as rx.
1733 */
1734 if (features & NETIF_F_HW_VLAN_RX)
1735 features |= NETIF_F_HW_VLAN_TX;
1736 else
1737 features &= ~NETIF_F_HW_VLAN_TX;
1738
1739 return features;
1740}
1741
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001742static int igb_set_features(struct net_device *netdev,
1743 netdev_features_t features)
Michał Mirosławac52caa2011-06-08 08:38:01 +00001744{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001745 netdev_features_t changed = netdev->features ^ features;
Ben Greear89eaefb2012-03-06 09:41:58 +00001746 struct igb_adapter *adapter = netdev_priv(netdev);
Michał Mirosławac52caa2011-06-08 08:38:01 +00001747
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001748 if (changed & NETIF_F_HW_VLAN_RX)
1749 igb_vlan_mode(netdev, features);
1750
Ben Greear89eaefb2012-03-06 09:41:58 +00001751 if (!(changed & NETIF_F_RXALL))
1752 return 0;
1753
1754 netdev->features = features;
1755
1756 if (netif_running(netdev))
1757 igb_reinit_locked(adapter);
1758 else
1759 igb_reset(adapter);
1760
Michał Mirosławac52caa2011-06-08 08:38:01 +00001761 return 0;
1762}
1763
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001764static const struct net_device_ops igb_netdev_ops = {
Alexander Duyck559e9c42009-10-27 23:52:50 +00001765 .ndo_open = igb_open,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001766 .ndo_stop = igb_close,
Alexander Duyckcd392f52011-08-26 07:43:59 +00001767 .ndo_start_xmit = igb_xmit_frame,
Eric Dumazet12dcd862010-10-15 17:27:10 +00001768 .ndo_get_stats64 = igb_get_stats64,
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001769 .ndo_set_rx_mode = igb_set_rx_mode,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001770 .ndo_set_mac_address = igb_set_mac,
1771 .ndo_change_mtu = igb_change_mtu,
1772 .ndo_do_ioctl = igb_ioctl,
1773 .ndo_tx_timeout = igb_tx_timeout,
1774 .ndo_validate_addr = eth_validate_addr,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001775 .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
1776 .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
Williams, Mitch A8151d292010-02-10 01:44:24 +00001777 .ndo_set_vf_mac = igb_ndo_set_vf_mac,
1778 .ndo_set_vf_vlan = igb_ndo_set_vf_vlan,
1779 .ndo_set_vf_tx_rate = igb_ndo_set_vf_bw,
1780 .ndo_get_vf_config = igb_ndo_get_vf_config,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001781#ifdef CONFIG_NET_POLL_CONTROLLER
1782 .ndo_poll_controller = igb_netpoll,
1783#endif
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001784 .ndo_fix_features = igb_fix_features,
1785 .ndo_set_features = igb_set_features,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001786};
1787
Taku Izumi42bfd33a2008-06-20 12:10:30 +09001788/**
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001789 * igb_set_fw_version - Configure version string for ethtool
1790 * @adapter: adapter struct
1791 *
1792 **/
1793void igb_set_fw_version(struct igb_adapter *adapter)
1794{
1795 struct e1000_hw *hw = &adapter->hw;
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001796 struct e1000_fw_version fw;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001797
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001798 igb_get_fw_version(hw, &fw);
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001799
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001800 switch (hw->mac.type) {
1801 case e1000_i211:
1802 snprintf(adapter->fw_version, sizeof(adapter->fw_version),
1803 "%2d.%2d-%d",
1804 fw.invm_major, fw.invm_minor, fw.invm_img_type);
1805 break;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001806
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001807 default:
1808 /* if option is rom valid, display its version too */
1809 if (fw.or_valid) {
1810 snprintf(adapter->fw_version,
1811 sizeof(adapter->fw_version),
1812 "%d.%d, 0x%08x, %d.%d.%d",
1813 fw.eep_major, fw.eep_minor, fw.etrack_id,
1814 fw.or_major, fw.or_build, fw.or_patch);
1815 /* no option rom */
1816 } else {
1817 snprintf(adapter->fw_version,
1818 sizeof(adapter->fw_version),
1819 "%d.%d, 0x%08x",
1820 fw.eep_major, fw.eep_minor, fw.etrack_id);
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001821 }
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001822 break;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001823 }
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001824 return;
1825}
1826
1827/**
Auke Kok9d5c8242008-01-24 02:22:38 -08001828 * igb_probe - Device Initialization Routine
1829 * @pdev: PCI device information struct
1830 * @ent: entry in igb_pci_tbl
1831 *
1832 * Returns 0 on success, negative on failure
1833 *
1834 * igb_probe initializes an adapter identified by a pci_dev structure.
1835 * The OS initialization, configuring of the adapter private structure,
1836 * and a hardware reset occur.
1837 **/
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001838static int igb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Auke Kok9d5c8242008-01-24 02:22:38 -08001839{
1840 struct net_device *netdev;
1841 struct igb_adapter *adapter;
1842 struct e1000_hw *hw;
Alexander Duyck4337e992009-10-27 23:48:31 +00001843 u16 eeprom_data = 0;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00001844 s32 ret_val;
Alexander Duyck4337e992009-10-27 23:48:31 +00001845 static int global_quad_port_a; /* global quad port a indication */
Auke Kok9d5c8242008-01-24 02:22:38 -08001846 const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
1847 unsigned long mmio_start, mmio_len;
David S. Miller2d6a5e92009-03-17 15:01:30 -07001848 int err, pci_using_dac;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00001849 u8 part_str[E1000_PBANUM_LENGTH];
Auke Kok9d5c8242008-01-24 02:22:38 -08001850
Andy Gospodarekbded64a2010-07-21 06:40:31 +00001851 /* Catch broken hardware that put the wrong VF device ID in
1852 * the PCIe SR-IOV capability.
1853 */
1854 if (pdev->is_virtfn) {
1855 WARN(1, KERN_ERR "%s (%hx:%hx) should not be a VF!\n",
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001856 pci_name(pdev), pdev->vendor, pdev->device);
Andy Gospodarekbded64a2010-07-21 06:40:31 +00001857 return -EINVAL;
1858 }
1859
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001860 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001861 if (err)
1862 return err;
1863
1864 pci_using_dac = 0;
Alexander Duyck59d71982010-04-27 13:09:25 +00001865 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08001866 if (!err) {
Alexander Duyck59d71982010-04-27 13:09:25 +00001867 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08001868 if (!err)
1869 pci_using_dac = 1;
1870 } else {
Alexander Duyck59d71982010-04-27 13:09:25 +00001871 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08001872 if (err) {
Alexander Duyck59d71982010-04-27 13:09:25 +00001873 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08001874 if (err) {
1875 dev_err(&pdev->dev, "No usable DMA "
1876 "configuration, aborting\n");
1877 goto err_dma;
1878 }
1879 }
1880 }
1881
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001882 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
1883 IORESOURCE_MEM),
1884 igb_driver_name);
Auke Kok9d5c8242008-01-24 02:22:38 -08001885 if (err)
1886 goto err_pci_reg;
1887
Frans Pop19d5afd2009-10-02 10:04:12 -07001888 pci_enable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08001889
Auke Kok9d5c8242008-01-24 02:22:38 -08001890 pci_set_master(pdev);
Auke Kokc682fc22008-04-23 11:09:34 -07001891 pci_save_state(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001892
1893 err = -ENOMEM;
Alexander Duyck1bfaf072009-02-19 20:39:23 -08001894 netdev = alloc_etherdev_mq(sizeof(struct igb_adapter),
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00001895 IGB_MAX_TX_QUEUES);
Auke Kok9d5c8242008-01-24 02:22:38 -08001896 if (!netdev)
1897 goto err_alloc_etherdev;
1898
1899 SET_NETDEV_DEV(netdev, &pdev->dev);
1900
1901 pci_set_drvdata(pdev, netdev);
1902 adapter = netdev_priv(netdev);
1903 adapter->netdev = netdev;
1904 adapter->pdev = pdev;
1905 hw = &adapter->hw;
1906 hw->back = adapter;
stephen hemmingerb3f4d592012-03-13 06:04:20 +00001907 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
Auke Kok9d5c8242008-01-24 02:22:38 -08001908
1909 mmio_start = pci_resource_start(pdev, 0);
1910 mmio_len = pci_resource_len(pdev, 0);
1911
1912 err = -EIO;
Alexander Duyck28b07592009-02-06 23:20:31 +00001913 hw->hw_addr = ioremap(mmio_start, mmio_len);
1914 if (!hw->hw_addr)
Auke Kok9d5c8242008-01-24 02:22:38 -08001915 goto err_ioremap;
1916
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001917 netdev->netdev_ops = &igb_netdev_ops;
Auke Kok9d5c8242008-01-24 02:22:38 -08001918 igb_set_ethtool_ops(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001919 netdev->watchdog_timeo = 5 * HZ;
Auke Kok9d5c8242008-01-24 02:22:38 -08001920
1921 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
1922
1923 netdev->mem_start = mmio_start;
1924 netdev->mem_end = mmio_start + mmio_len;
1925
Auke Kok9d5c8242008-01-24 02:22:38 -08001926 /* PCI config space info */
1927 hw->vendor_id = pdev->vendor;
1928 hw->device_id = pdev->device;
1929 hw->revision_id = pdev->revision;
1930 hw->subsystem_vendor_id = pdev->subsystem_vendor;
1931 hw->subsystem_device_id = pdev->subsystem_device;
1932
Auke Kok9d5c8242008-01-24 02:22:38 -08001933 /* Copy the default MAC, PHY and NVM function pointers */
1934 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
1935 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
1936 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
1937 /* Initialize skew-specific constants */
1938 err = ei->get_invariants(hw);
1939 if (err)
Alexander Duyck450c87c2009-02-06 23:22:11 +00001940 goto err_sw_init;
Auke Kok9d5c8242008-01-24 02:22:38 -08001941
Alexander Duyck450c87c2009-02-06 23:22:11 +00001942 /* setup the private structure */
Auke Kok9d5c8242008-01-24 02:22:38 -08001943 err = igb_sw_init(adapter);
1944 if (err)
1945 goto err_sw_init;
1946
1947 igb_get_bus_info_pcie(hw);
1948
1949 hw->phy.autoneg_wait_to_complete = false;
Auke Kok9d5c8242008-01-24 02:22:38 -08001950
1951 /* Copper options */
1952 if (hw->phy.media_type == e1000_media_type_copper) {
1953 hw->phy.mdix = AUTO_ALL_MODES;
1954 hw->phy.disable_polarity_correction = false;
1955 hw->phy.ms_type = e1000_ms_hw_default;
1956 }
1957
1958 if (igb_check_reset_block(hw))
1959 dev_info(&pdev->dev,
1960 "PHY reset is blocked due to SOL/IDER session.\n");
1961
Alexander Duyck077887c2011-08-26 07:46:29 +00001962 /*
1963 * features is initialized to 0 in allocation, it might have bits
1964 * set by igb_sw_init so we should use an or instead of an
1965 * assignment.
1966 */
1967 netdev->features |= NETIF_F_SG |
1968 NETIF_F_IP_CSUM |
1969 NETIF_F_IPV6_CSUM |
1970 NETIF_F_TSO |
1971 NETIF_F_TSO6 |
1972 NETIF_F_RXHASH |
1973 NETIF_F_RXCSUM |
1974 NETIF_F_HW_VLAN_RX |
1975 NETIF_F_HW_VLAN_TX;
Michał Mirosławac52caa2011-06-08 08:38:01 +00001976
Alexander Duyck077887c2011-08-26 07:46:29 +00001977 /* copy netdev features into list of user selectable features */
1978 netdev->hw_features |= netdev->features;
Ben Greear89eaefb2012-03-06 09:41:58 +00001979 netdev->hw_features |= NETIF_F_RXALL;
Auke Kok9d5c8242008-01-24 02:22:38 -08001980
Alexander Duyck077887c2011-08-26 07:46:29 +00001981 /* set this bit last since it cannot be part of hw_features */
1982 netdev->features |= NETIF_F_HW_VLAN_FILTER;
1983
1984 netdev->vlan_features |= NETIF_F_TSO |
1985 NETIF_F_TSO6 |
1986 NETIF_F_IP_CSUM |
1987 NETIF_F_IPV6_CSUM |
1988 NETIF_F_SG;
Jeff Kirsher48f29ff2008-06-05 04:06:27 -07001989
Ben Greear6b8f0922012-03-06 09:41:53 +00001990 netdev->priv_flags |= IFF_SUPP_NOFCS;
1991
Yi Zou7b872a52010-09-22 17:57:58 +00001992 if (pci_using_dac) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001993 netdev->features |= NETIF_F_HIGHDMA;
Yi Zou7b872a52010-09-22 17:57:58 +00001994 netdev->vlan_features |= NETIF_F_HIGHDMA;
1995 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001996
Michał Mirosławac52caa2011-06-08 08:38:01 +00001997 if (hw->mac.type >= e1000_82576) {
1998 netdev->hw_features |= NETIF_F_SCTP_CSUM;
Jesse Brandeburgb9473562009-04-27 22:36:13 +00001999 netdev->features |= NETIF_F_SCTP_CSUM;
Michał Mirosławac52caa2011-06-08 08:38:01 +00002000 }
Jesse Brandeburgb9473562009-04-27 22:36:13 +00002001
Jiri Pirko01789342011-08-16 06:29:00 +00002002 netdev->priv_flags |= IFF_UNICAST_FLT;
2003
Alexander Duyck330a6d62009-10-27 23:51:35 +00002004 adapter->en_mng_pt = igb_enable_mng_pass_thru(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002005
2006 /* before reading the NVM, reset the controller to put the device in a
2007 * known good starting state */
2008 hw->mac.ops.reset_hw(hw);
2009
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002010 /*
2011 * make sure the NVM is good , i211 parts have special NVM that
2012 * doesn't contain a checksum
2013 */
2014 if (hw->mac.type != e1000_i211) {
2015 if (hw->nvm.ops.validate(hw) < 0) {
2016 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
2017 err = -EIO;
2018 goto err_eeprom;
2019 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002020 }
2021
2022 /* copy the MAC address out of the NVM */
2023 if (hw->mac.ops.read_mac_addr(hw))
2024 dev_err(&pdev->dev, "NVM Read Error\n");
2025
2026 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
Auke Kok9d5c8242008-01-24 02:22:38 -08002027
Jiri Pirkoaaeb6cd2013-01-08 01:38:26 +00002028 if (!is_valid_ether_addr(netdev->dev_addr)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002029 dev_err(&pdev->dev, "Invalid MAC Address\n");
2030 err = -EIO;
2031 goto err_eeprom;
2032 }
2033
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00002034 /* get firmware version for ethtool -i */
2035 igb_set_fw_version(adapter);
2036
Joe Perchesc061b182010-08-23 18:20:03 +00002037 setup_timer(&adapter->watchdog_timer, igb_watchdog,
Alexander Duyck0e340482009-03-20 00:17:08 +00002038 (unsigned long) adapter);
Joe Perchesc061b182010-08-23 18:20:03 +00002039 setup_timer(&adapter->phy_info_timer, igb_update_phy_info,
Alexander Duyck0e340482009-03-20 00:17:08 +00002040 (unsigned long) adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002041
2042 INIT_WORK(&adapter->reset_task, igb_reset_task);
2043 INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
2044
Alexander Duyck450c87c2009-02-06 23:22:11 +00002045 /* Initialize link properties that are user-changeable */
Auke Kok9d5c8242008-01-24 02:22:38 -08002046 adapter->fc_autoneg = true;
2047 hw->mac.autoneg = true;
2048 hw->phy.autoneg_advertised = 0x2f;
2049
Alexander Duyck0cce1192009-07-23 18:10:24 +00002050 hw->fc.requested_mode = e1000_fc_default;
2051 hw->fc.current_mode = e1000_fc_default;
Auke Kok9d5c8242008-01-24 02:22:38 -08002052
Auke Kok9d5c8242008-01-24 02:22:38 -08002053 igb_validate_mdi_setting(hw);
2054
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002055 /* By default, support wake on port A */
Alexander Duycka2cf8b62009-03-13 20:41:17 +00002056 if (hw->bus.func == 0)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002057 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2058
2059 /* Check the NVM for wake support on non-port A ports */
2060 if (hw->mac.type >= e1000_82580)
Alexander Duyck55cac242009-11-19 12:42:21 +00002061 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A +
2062 NVM_82580_LAN_FUNC_OFFSET(hw->bus.func), 1,
2063 &eeprom_data);
Alexander Duycka2cf8b62009-03-13 20:41:17 +00002064 else if (hw->bus.func == 1)
2065 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
Auke Kok9d5c8242008-01-24 02:22:38 -08002066
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002067 if (eeprom_data & IGB_EEPROM_APME)
2068 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002069
2070 /* now that we have the eeprom settings, apply the special cases where
2071 * the eeprom may be wrong or the board simply won't support wake on
2072 * lan on a particular port */
2073 switch (pdev->device) {
2074 case E1000_DEV_ID_82575GB_QUAD_COPPER:
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002075 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002076 break;
2077 case E1000_DEV_ID_82575EB_FIBER_SERDES:
Alexander Duyck2d064c02008-07-08 15:10:12 -07002078 case E1000_DEV_ID_82576_FIBER:
2079 case E1000_DEV_ID_82576_SERDES:
Auke Kok9d5c8242008-01-24 02:22:38 -08002080 /* Wake events only supported on port A for dual fiber
2081 * regardless of eeprom setting */
2082 if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002083 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002084 break;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002085 case E1000_DEV_ID_82576_QUAD_COPPER:
Stefan Assmannd5aa2252010-04-09 09:51:34 +00002086 case E1000_DEV_ID_82576_QUAD_COPPER_ET2:
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002087 /* if quad port adapter, disable WoL on all but port A */
2088 if (global_quad_port_a != 0)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002089 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002090 else
2091 adapter->flags |= IGB_FLAG_QUAD_PORT_A;
2092 /* Reset for multiple quad port adapters */
2093 if (++global_quad_port_a == 4)
2094 global_quad_port_a = 0;
2095 break;
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002096 default:
2097 /* If the device can't wake, don't set software support */
2098 if (!device_can_wakeup(&adapter->pdev->dev))
2099 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002100 }
2101
2102 /* initialize the wol settings based on the eeprom settings */
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002103 if (adapter->flags & IGB_FLAG_WOL_SUPPORTED)
2104 adapter->wol |= E1000_WUFC_MAG;
2105
2106 /* Some vendors want WoL disabled by default, but still supported */
2107 if ((hw->mac.type == e1000_i350) &&
2108 (pdev->subsystem_vendor == PCI_VENDOR_ID_HP)) {
2109 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2110 adapter->wol = 0;
2111 }
2112
2113 device_set_wakeup_enable(&adapter->pdev->dev,
2114 adapter->flags & IGB_FLAG_WOL_SUPPORTED);
Auke Kok9d5c8242008-01-24 02:22:38 -08002115
2116 /* reset the hardware with the new settings */
2117 igb_reset(adapter);
2118
2119 /* let the f/w know that the h/w is now under the control of the
2120 * driver. */
2121 igb_get_hw_control(adapter);
2122
Auke Kok9d5c8242008-01-24 02:22:38 -08002123 strcpy(netdev->name, "eth%d");
2124 err = register_netdev(netdev);
2125 if (err)
2126 goto err_register;
2127
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00002128 /* carrier off reporting is important to ethtool even BEFORE open */
2129 netif_carrier_off(netdev);
2130
Jeff Kirsher421e02f2008-10-17 11:08:31 -07002131#ifdef CONFIG_IGB_DCA
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08002132 if (dca_add_requester(&pdev->dev) == 0) {
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002133 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002134 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002135 igb_setup_dca(adapter);
2136 }
Alexander Duyckc5b9bd52009-10-27 23:46:01 +00002137
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002138#endif
Matthew Vick3c89f6d2012-08-10 05:40:43 +00002139
Anders Berggren673b8b72011-02-04 07:32:32 +00002140 /* do hw tstamp init after resetting */
Richard Cochran7ebae812012-03-16 10:55:37 +00002141 igb_ptp_init(adapter);
Anders Berggren673b8b72011-02-04 07:32:32 +00002142
Auke Kok9d5c8242008-01-24 02:22:38 -08002143 dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
2144 /* print bus type/speed/width info */
Johannes Berg7c510e42008-10-27 17:47:26 -07002145 dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08002146 netdev->name,
Alexander Duyck559e9c42009-10-27 23:52:50 +00002147 ((hw->bus.speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
Alexander Duyckff846f52010-04-27 01:02:40 +00002148 (hw->bus.speed == e1000_bus_speed_5000) ? "5.0Gb/s" :
Alexander Duyck559e9c42009-10-27 23:52:50 +00002149 "unknown"),
Alexander Duyck59c3de82009-03-31 20:38:00 +00002150 ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
2151 (hw->bus.width == e1000_bus_width_pcie_x2) ? "Width x2" :
2152 (hw->bus.width == e1000_bus_width_pcie_x1) ? "Width x1" :
2153 "unknown"),
Johannes Berg7c510e42008-10-27 17:47:26 -07002154 netdev->dev_addr);
Auke Kok9d5c8242008-01-24 02:22:38 -08002155
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00002156 ret_val = igb_read_part_string(hw, part_str, E1000_PBANUM_LENGTH);
2157 if (ret_val)
2158 strcpy(part_str, "Unknown");
2159 dev_info(&pdev->dev, "%s: PBA No: %s\n", netdev->name, part_str);
Auke Kok9d5c8242008-01-24 02:22:38 -08002160 dev_info(&pdev->dev,
2161 "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
2162 adapter->msix_entries ? "MSI-X" :
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002163 (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
Auke Kok9d5c8242008-01-24 02:22:38 -08002164 adapter->num_rx_queues, adapter->num_tx_queues);
Carolyn Wyborny09b068d2011-03-11 20:42:13 -08002165 switch (hw->mac.type) {
2166 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002167 case e1000_i210:
2168 case e1000_i211:
Carolyn Wyborny09b068d2011-03-11 20:42:13 -08002169 igb_set_eee_i350(hw);
2170 break;
2171 default:
2172 break;
2173 }
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002174
2175 pm_runtime_put_noidle(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002176 return 0;
2177
2178err_register:
2179 igb_release_hw_control(adapter);
2180err_eeprom:
2181 if (!igb_check_reset_block(hw))
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08002182 igb_reset_phy(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002183
2184 if (hw->flash_address)
2185 iounmap(hw->flash_address);
Auke Kok9d5c8242008-01-24 02:22:38 -08002186err_sw_init:
Alexander Duyck047e0032009-10-27 15:49:27 +00002187 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002188 iounmap(hw->hw_addr);
2189err_ioremap:
2190 free_netdev(netdev);
2191err_alloc_etherdev:
Alexander Duyck559e9c42009-10-27 23:52:50 +00002192 pci_release_selected_regions(pdev,
2193 pci_select_bars(pdev, IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08002194err_pci_reg:
2195err_dma:
2196 pci_disable_device(pdev);
2197 return err;
2198}
2199
Greg Rosefa44f2f2013-01-17 01:03:06 -08002200#ifdef CONFIG_PCI_IOV
2201static int igb_disable_sriov(struct pci_dev *pdev)
2202{
2203 struct net_device *netdev = pci_get_drvdata(pdev);
2204 struct igb_adapter *adapter = netdev_priv(netdev);
2205 struct e1000_hw *hw = &adapter->hw;
2206
2207 /* reclaim resources allocated to VFs */
2208 if (adapter->vf_data) {
2209 /* disable iov and allow time for transactions to clear */
2210 if (igb_vfs_are_assigned(adapter)) {
2211 dev_warn(&pdev->dev,
2212 "Cannot deallocate SR-IOV virtual functions while they are assigned - VFs will not be deallocated\n");
2213 return -EPERM;
2214 } else {
2215 pci_disable_sriov(pdev);
2216 msleep(500);
2217 }
2218
2219 kfree(adapter->vf_data);
2220 adapter->vf_data = NULL;
2221 adapter->vfs_allocated_count = 0;
2222 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
2223 wrfl();
2224 msleep(100);
2225 dev_info(&pdev->dev, "IOV Disabled\n");
2226
2227 /* Re-enable DMA Coalescing flag since IOV is turned off */
2228 adapter->flags |= IGB_FLAG_DMAC;
2229 }
2230
2231 return 0;
2232}
2233
2234static int igb_enable_sriov(struct pci_dev *pdev, int num_vfs)
2235{
2236 struct net_device *netdev = pci_get_drvdata(pdev);
2237 struct igb_adapter *adapter = netdev_priv(netdev);
2238 int old_vfs = pci_num_vf(pdev);
2239 int err = 0;
2240 int i;
2241
2242 if (!num_vfs)
2243 goto out;
2244 else if (old_vfs && old_vfs == num_vfs)
2245 goto out;
2246 else if (old_vfs && old_vfs != num_vfs)
2247 err = igb_disable_sriov(pdev);
2248
2249 if (err)
2250 goto out;
2251
2252 if (num_vfs > 7) {
2253 err = -EPERM;
2254 goto out;
2255 }
2256
2257 adapter->vfs_allocated_count = num_vfs;
2258
2259 adapter->vf_data = kcalloc(adapter->vfs_allocated_count,
2260 sizeof(struct vf_data_storage), GFP_KERNEL);
2261
2262 /* if allocation failed then we do not support SR-IOV */
2263 if (!adapter->vf_data) {
2264 adapter->vfs_allocated_count = 0;
2265 dev_err(&pdev->dev,
2266 "Unable to allocate memory for VF Data Storage\n");
2267 err = -ENOMEM;
2268 goto out;
2269 }
2270
2271 err = pci_enable_sriov(pdev, adapter->vfs_allocated_count);
2272 if (err)
2273 goto err_out;
2274
2275 dev_info(&pdev->dev, "%d VFs allocated\n",
2276 adapter->vfs_allocated_count);
2277 for (i = 0; i < adapter->vfs_allocated_count; i++)
2278 igb_vf_configure(adapter, i);
2279
2280 /* DMA Coalescing is not supported in IOV mode. */
2281 adapter->flags &= ~IGB_FLAG_DMAC;
2282 goto out;
2283
2284err_out:
2285 kfree(adapter->vf_data);
2286 adapter->vf_data = NULL;
2287 adapter->vfs_allocated_count = 0;
2288out:
2289 return err;
2290}
2291
2292#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08002293/**
2294 * igb_remove - Device Removal Routine
2295 * @pdev: PCI device information struct
2296 *
2297 * igb_remove is called by the PCI subsystem to alert the driver
2298 * that it should release a PCI device. The could be caused by a
2299 * Hot-Plug event, or because the driver is going to be removed from
2300 * memory.
2301 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002302static void igb_remove(struct pci_dev *pdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08002303{
2304 struct net_device *netdev = pci_get_drvdata(pdev);
2305 struct igb_adapter *adapter = netdev_priv(netdev);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002306 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08002307
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002308 pm_runtime_get_noresume(&pdev->dev);
Matthew Vicka79f4f82012-08-10 05:40:44 +00002309 igb_ptp_stop(adapter);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002310
Tejun Heo760141a2010-12-12 16:45:14 +01002311 /*
2312 * The watchdog timer may be rescheduled, so explicitly
2313 * disable watchdog from being rescheduled.
2314 */
Auke Kok9d5c8242008-01-24 02:22:38 -08002315 set_bit(__IGB_DOWN, &adapter->state);
2316 del_timer_sync(&adapter->watchdog_timer);
2317 del_timer_sync(&adapter->phy_info_timer);
2318
Tejun Heo760141a2010-12-12 16:45:14 +01002319 cancel_work_sync(&adapter->reset_task);
2320 cancel_work_sync(&adapter->watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08002321
Jeff Kirsher421e02f2008-10-17 11:08:31 -07002322#ifdef CONFIG_IGB_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002323 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002324 dev_info(&pdev->dev, "DCA disabled\n");
2325 dca_remove_requester(&pdev->dev);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002326 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08002327 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002328 }
2329#endif
2330
Auke Kok9d5c8242008-01-24 02:22:38 -08002331 /* Release control of h/w to f/w. If f/w is AMT enabled, this
2332 * would have already happened in close and is redundant. */
2333 igb_release_hw_control(adapter);
2334
2335 unregister_netdev(netdev);
2336
Alexander Duyck047e0032009-10-27 15:49:27 +00002337 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002338
Alexander Duyck37680112009-02-19 20:40:30 -08002339#ifdef CONFIG_PCI_IOV
Greg Rosefa44f2f2013-01-17 01:03:06 -08002340 igb_disable_sriov(pdev);
Alexander Duyck37680112009-02-19 20:40:30 -08002341#endif
Alexander Duyck559e9c42009-10-27 23:52:50 +00002342
Alexander Duyck28b07592009-02-06 23:20:31 +00002343 iounmap(hw->hw_addr);
2344 if (hw->flash_address)
2345 iounmap(hw->flash_address);
Alexander Duyck559e9c42009-10-27 23:52:50 +00002346 pci_release_selected_regions(pdev,
2347 pci_select_bars(pdev, IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08002348
Carolyn Wyborny1128c752011-10-14 00:13:49 +00002349 kfree(adapter->shadow_vfta);
Auke Kok9d5c8242008-01-24 02:22:38 -08002350 free_netdev(netdev);
2351
Frans Pop19d5afd2009-10-02 10:04:12 -07002352 pci_disable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08002353
Auke Kok9d5c8242008-01-24 02:22:38 -08002354 pci_disable_device(pdev);
2355}
2356
2357/**
Alexander Duycka6b623e2009-10-27 23:47:53 +00002358 * igb_probe_vfs - Initialize vf data storage and add VFs to pci config space
2359 * @adapter: board private structure to initialize
2360 *
2361 * This function initializes the vf specific data storage and then attempts to
2362 * allocate the VFs. The reason for ordering it this way is because it is much
2363 * mor expensive time wise to disable SR-IOV than it is to allocate and free
2364 * the memory for the VFs.
2365 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002366static void igb_probe_vfs(struct igb_adapter *adapter)
Alexander Duycka6b623e2009-10-27 23:47:53 +00002367{
2368#ifdef CONFIG_PCI_IOV
2369 struct pci_dev *pdev = adapter->pdev;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002370 struct e1000_hw *hw = &adapter->hw;
Alexander Duycka6b623e2009-10-27 23:47:53 +00002371
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002372 /* Virtualization features not supported on i210 family. */
2373 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211))
2374 return;
2375
Greg Rosefa44f2f2013-01-17 01:03:06 -08002376 igb_enable_sriov(pdev, max_vfs);
2377 pci_sriov_set_totalvfs(pdev, 7);
Alexander Duycka6b623e2009-10-27 23:47:53 +00002378
Alexander Duycka6b623e2009-10-27 23:47:53 +00002379#endif /* CONFIG_PCI_IOV */
2380}
2381
Greg Rosefa44f2f2013-01-17 01:03:06 -08002382static void igb_init_queue_configuration(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002383{
2384 struct e1000_hw *hw = &adapter->hw;
Matthew Vick374a5422012-05-18 04:54:58 +00002385 u32 max_rss_queues;
Auke Kok9d5c8242008-01-24 02:22:38 -08002386
Matthew Vick374a5422012-05-18 04:54:58 +00002387 /* Determine the maximum number of RSS queues supported. */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002388 switch (hw->mac.type) {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002389 case e1000_i211:
Matthew Vick374a5422012-05-18 04:54:58 +00002390 max_rss_queues = IGB_MAX_RX_QUEUES_I211;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002391 break;
Matthew Vick374a5422012-05-18 04:54:58 +00002392 case e1000_82575:
2393 case e1000_i210:
2394 max_rss_queues = IGB_MAX_RX_QUEUES_82575;
2395 break;
2396 case e1000_i350:
2397 /* I350 cannot do RSS and SR-IOV at the same time */
2398 if (!!adapter->vfs_allocated_count) {
2399 max_rss_queues = 1;
2400 break;
2401 }
2402 /* fall through */
2403 case e1000_82576:
2404 if (!!adapter->vfs_allocated_count) {
2405 max_rss_queues = 2;
2406 break;
2407 }
2408 /* fall through */
2409 case e1000_82580:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002410 default:
Matthew Vick374a5422012-05-18 04:54:58 +00002411 max_rss_queues = IGB_MAX_RX_QUEUES;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002412 break;
2413 }
Alexander Duycka99955f2009-11-12 18:37:19 +00002414
Matthew Vick374a5422012-05-18 04:54:58 +00002415 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
2416
2417 /* Determine if we need to pair queues. */
2418 switch (hw->mac.type) {
2419 case e1000_82575:
2420 case e1000_i211:
2421 /* Device supports enough interrupts without queue pairing. */
2422 break;
2423 case e1000_82576:
2424 /*
2425 * If VFs are going to be allocated with RSS queues then we
2426 * should pair the queues in order to conserve interrupts due
2427 * to limited supply.
2428 */
2429 if ((adapter->rss_queues > 1) &&
2430 (adapter->vfs_allocated_count > 6))
2431 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2432 /* fall through */
2433 case e1000_82580:
2434 case e1000_i350:
2435 case e1000_i210:
2436 default:
2437 /*
2438 * If rss_queues > half of max_rss_queues, pair the queues in
2439 * order to conserve interrupts due to limited supply.
2440 */
2441 if (adapter->rss_queues > (max_rss_queues / 2))
2442 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2443 break;
2444 }
Greg Rosefa44f2f2013-01-17 01:03:06 -08002445}
2446
2447/**
2448 * igb_sw_init - Initialize general software structures (struct igb_adapter)
2449 * @adapter: board private structure to initialize
2450 *
2451 * igb_sw_init initializes the Adapter private data structure.
2452 * Fields are initialized based on PCI device information and
2453 * OS network device settings (MTU size).
2454 **/
2455static int igb_sw_init(struct igb_adapter *adapter)
2456{
2457 struct e1000_hw *hw = &adapter->hw;
2458 struct net_device *netdev = adapter->netdev;
2459 struct pci_dev *pdev = adapter->pdev;
2460
2461 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
2462
2463 /* set default ring sizes */
2464 adapter->tx_ring_count = IGB_DEFAULT_TXD;
2465 adapter->rx_ring_count = IGB_DEFAULT_RXD;
2466
2467 /* set default ITR values */
2468 adapter->rx_itr_setting = IGB_DEFAULT_ITR;
2469 adapter->tx_itr_setting = IGB_DEFAULT_ITR;
2470
2471 /* set default work limits */
2472 adapter->tx_work_limit = IGB_DEFAULT_TX_WORK;
2473
2474 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
2475 VLAN_HLEN;
2476 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
2477
2478 spin_lock_init(&adapter->stats64_lock);
2479#ifdef CONFIG_PCI_IOV
2480 switch (hw->mac.type) {
2481 case e1000_82576:
2482 case e1000_i350:
2483 if (max_vfs > 7) {
2484 dev_warn(&pdev->dev,
2485 "Maximum of 7 VFs per PF, using max\n");
2486 adapter->vfs_allocated_count = 7;
2487 } else
2488 adapter->vfs_allocated_count = max_vfs;
2489 if (adapter->vfs_allocated_count)
2490 dev_warn(&pdev->dev,
2491 "Enabling SR-IOV VFs using the module parameter is deprecated - please use the pci sysfs interface.\n");
2492 break;
2493 default:
2494 break;
2495 }
2496#endif /* CONFIG_PCI_IOV */
2497
2498 igb_init_queue_configuration(adapter);
Alexander Duycka99955f2009-11-12 18:37:19 +00002499
Carolyn Wyborny1128c752011-10-14 00:13:49 +00002500 /* Setup and initialize a copy of the hw vlan table array */
2501 adapter->shadow_vfta = kzalloc(sizeof(u32) *
2502 E1000_VLAN_FILTER_TBL_SIZE,
2503 GFP_ATOMIC);
2504
Alexander Duycka6b623e2009-10-27 23:47:53 +00002505 /* This call may decrease the number of queues */
Stefan Assmann53c7d062012-12-04 06:00:12 +00002506 if (igb_init_interrupt_scheme(adapter, true)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002507 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
2508 return -ENOMEM;
2509 }
2510
Alexander Duycka6b623e2009-10-27 23:47:53 +00002511 igb_probe_vfs(adapter);
2512
Auke Kok9d5c8242008-01-24 02:22:38 -08002513 /* Explicitly disable IRQ since the NIC can be in any state. */
2514 igb_irq_disable(adapter);
2515
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002516 if (hw->mac.type >= e1000_i350)
Carolyn Wyborny831ec0b2011-03-11 20:43:54 -08002517 adapter->flags &= ~IGB_FLAG_DMAC;
2518
Auke Kok9d5c8242008-01-24 02:22:38 -08002519 set_bit(__IGB_DOWN, &adapter->state);
2520 return 0;
2521}
2522
2523/**
2524 * igb_open - Called when a network interface is made active
2525 * @netdev: network interface device structure
2526 *
2527 * Returns 0 on success, negative value on failure
2528 *
2529 * The open entry point is called when a network interface is made
2530 * active by the system (IFF_UP). At this point all resources needed
2531 * for transmit and receive operations are allocated, the interrupt
2532 * handler is registered with the OS, the watchdog timer is started,
2533 * and the stack is notified that the interface is ready.
2534 **/
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002535static int __igb_open(struct net_device *netdev, bool resuming)
Auke Kok9d5c8242008-01-24 02:22:38 -08002536{
2537 struct igb_adapter *adapter = netdev_priv(netdev);
2538 struct e1000_hw *hw = &adapter->hw;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002539 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002540 int err;
2541 int i;
2542
2543 /* disallow open during test */
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002544 if (test_bit(__IGB_TESTING, &adapter->state)) {
2545 WARN_ON(resuming);
Auke Kok9d5c8242008-01-24 02:22:38 -08002546 return -EBUSY;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002547 }
2548
2549 if (!resuming)
2550 pm_runtime_get_sync(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002551
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00002552 netif_carrier_off(netdev);
2553
Auke Kok9d5c8242008-01-24 02:22:38 -08002554 /* allocate transmit descriptors */
2555 err = igb_setup_all_tx_resources(adapter);
2556 if (err)
2557 goto err_setup_tx;
2558
2559 /* allocate receive descriptors */
2560 err = igb_setup_all_rx_resources(adapter);
2561 if (err)
2562 goto err_setup_rx;
2563
Nick Nunley88a268c2010-02-17 01:01:59 +00002564 igb_power_up_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002565
Auke Kok9d5c8242008-01-24 02:22:38 -08002566 /* before we allocate an interrupt, we must be ready to handle it.
2567 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
2568 * as soon as we call pci_request_irq, so we have to setup our
2569 * clean_rx handler before we do so. */
2570 igb_configure(adapter);
2571
2572 err = igb_request_irq(adapter);
2573 if (err)
2574 goto err_req_irq;
2575
Alexander Duyck0c2cc022012-09-25 00:31:22 +00002576 /* Notify the stack of the actual queue counts. */
2577 err = netif_set_real_num_tx_queues(adapter->netdev,
2578 adapter->num_tx_queues);
2579 if (err)
2580 goto err_set_queues;
2581
2582 err = netif_set_real_num_rx_queues(adapter->netdev,
2583 adapter->num_rx_queues);
2584 if (err)
2585 goto err_set_queues;
2586
Auke Kok9d5c8242008-01-24 02:22:38 -08002587 /* From here on the code is the same as igb_up() */
2588 clear_bit(__IGB_DOWN, &adapter->state);
2589
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00002590 for (i = 0; i < adapter->num_q_vectors; i++)
2591 napi_enable(&(adapter->q_vector[i]->napi));
Auke Kok9d5c8242008-01-24 02:22:38 -08002592
2593 /* Clear any pending interrupts. */
2594 rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07002595
2596 igb_irq_enable(adapter);
2597
Alexander Duyckd4960302009-10-27 15:53:45 +00002598 /* notify VFs that reset has been completed */
2599 if (adapter->vfs_allocated_count) {
2600 u32 reg_data = rd32(E1000_CTRL_EXT);
2601 reg_data |= E1000_CTRL_EXT_PFRSTD;
2602 wr32(E1000_CTRL_EXT, reg_data);
2603 }
2604
Jeff Kirsherd55b53f2008-07-18 04:33:03 -07002605 netif_tx_start_all_queues(netdev);
2606
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002607 if (!resuming)
2608 pm_runtime_put(&pdev->dev);
2609
Alexander Duyck25568a52009-10-27 23:49:59 +00002610 /* start the watchdog. */
2611 hw->mac.get_link_status = 1;
2612 schedule_work(&adapter->watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08002613
2614 return 0;
2615
Alexander Duyck0c2cc022012-09-25 00:31:22 +00002616err_set_queues:
2617 igb_free_irq(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002618err_req_irq:
2619 igb_release_hw_control(adapter);
Nick Nunley88a268c2010-02-17 01:01:59 +00002620 igb_power_down_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002621 igb_free_all_rx_resources(adapter);
2622err_setup_rx:
2623 igb_free_all_tx_resources(adapter);
2624err_setup_tx:
2625 igb_reset(adapter);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002626 if (!resuming)
2627 pm_runtime_put(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002628
2629 return err;
2630}
2631
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002632static int igb_open(struct net_device *netdev)
2633{
2634 return __igb_open(netdev, false);
2635}
2636
Auke Kok9d5c8242008-01-24 02:22:38 -08002637/**
2638 * igb_close - Disables a network interface
2639 * @netdev: network interface device structure
2640 *
2641 * Returns 0, this is not allowed to fail
2642 *
2643 * The close entry point is called when an interface is de-activated
2644 * by the OS. The hardware is still under the driver's control, but
2645 * needs to be disabled. A global MAC reset is issued to stop the
2646 * hardware, and all transmit and receive resources are freed.
2647 **/
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002648static int __igb_close(struct net_device *netdev, bool suspending)
Auke Kok9d5c8242008-01-24 02:22:38 -08002649{
2650 struct igb_adapter *adapter = netdev_priv(netdev);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002651 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002652
2653 WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
Auke Kok9d5c8242008-01-24 02:22:38 -08002654
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002655 if (!suspending)
2656 pm_runtime_get_sync(&pdev->dev);
2657
2658 igb_down(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002659 igb_free_irq(adapter);
2660
2661 igb_free_all_tx_resources(adapter);
2662 igb_free_all_rx_resources(adapter);
2663
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002664 if (!suspending)
2665 pm_runtime_put_sync(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002666 return 0;
2667}
2668
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002669static int igb_close(struct net_device *netdev)
2670{
2671 return __igb_close(netdev, false);
2672}
2673
Auke Kok9d5c8242008-01-24 02:22:38 -08002674/**
2675 * igb_setup_tx_resources - allocate Tx resources (Descriptors)
Auke Kok9d5c8242008-01-24 02:22:38 -08002676 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2677 *
2678 * Return 0 on success, negative on failure
2679 **/
Alexander Duyck80785292009-10-27 15:51:47 +00002680int igb_setup_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002681{
Alexander Duyck59d71982010-04-27 13:09:25 +00002682 struct device *dev = tx_ring->dev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002683 int size;
2684
Alexander Duyck06034642011-08-26 07:44:22 +00002685 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002686
2687 tx_ring->tx_buffer_info = vzalloc(size);
Alexander Duyck06034642011-08-26 07:44:22 +00002688 if (!tx_ring->tx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08002689 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -08002690
2691 /* round up to nearest 4K */
Alexander Duyck85e8d002009-02-16 00:00:20 -08002692 tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08002693 tx_ring->size = ALIGN(tx_ring->size, 4096);
2694
Alexander Duyck5536d212012-09-25 00:31:17 +00002695 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
2696 &tx_ring->dma, GFP_KERNEL);
Auke Kok9d5c8242008-01-24 02:22:38 -08002697 if (!tx_ring->desc)
2698 goto err;
2699
Auke Kok9d5c8242008-01-24 02:22:38 -08002700 tx_ring->next_to_use = 0;
2701 tx_ring->next_to_clean = 0;
Alexander Duyck81c2fc22011-08-26 07:45:20 +00002702
Auke Kok9d5c8242008-01-24 02:22:38 -08002703 return 0;
2704
2705err:
Alexander Duyck06034642011-08-26 07:44:22 +00002706 vfree(tx_ring->tx_buffer_info);
Alexander Duyckf33005a2012-09-13 06:27:55 +00002707 tx_ring->tx_buffer_info = NULL;
2708 dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08002709 return -ENOMEM;
2710}
2711
2712/**
2713 * igb_setup_all_tx_resources - wrapper to allocate Tx resources
2714 * (Descriptors) for all queues
2715 * @adapter: board private structure
2716 *
2717 * Return 0 on success, negative on failure
2718 **/
2719static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
2720{
Alexander Duyck439705e2009-10-27 23:49:20 +00002721 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002722 int i, err = 0;
2723
2724 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00002725 err = igb_setup_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002726 if (err) {
Alexander Duyck439705e2009-10-27 23:49:20 +00002727 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08002728 "Allocation for Tx Queue %u failed\n", i);
2729 for (i--; i >= 0; i--)
Alexander Duyck3025a442010-02-17 01:02:39 +00002730 igb_free_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002731 break;
2732 }
2733 }
2734
2735 return err;
2736}
2737
2738/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002739 * igb_setup_tctl - configure the transmit control registers
2740 * @adapter: Board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08002741 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002742void igb_setup_tctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002743{
Auke Kok9d5c8242008-01-24 02:22:38 -08002744 struct e1000_hw *hw = &adapter->hw;
2745 u32 tctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08002746
Alexander Duyck85b430b2009-10-27 15:50:29 +00002747 /* disable queue 0 which is enabled by default on 82575 and 82576 */
2748 wr32(E1000_TXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08002749
2750 /* Program the Transmit Control Register */
Auke Kok9d5c8242008-01-24 02:22:38 -08002751 tctl = rd32(E1000_TCTL);
2752 tctl &= ~E1000_TCTL_CT;
2753 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
2754 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2755
2756 igb_config_collision_dist(hw);
2757
Auke Kok9d5c8242008-01-24 02:22:38 -08002758 /* Enable transmits */
2759 tctl |= E1000_TCTL_EN;
2760
2761 wr32(E1000_TCTL, tctl);
2762}
2763
2764/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002765 * igb_configure_tx_ring - Configure transmit ring after Reset
2766 * @adapter: board private structure
2767 * @ring: tx ring to configure
2768 *
2769 * Configure a transmit ring after a reset.
2770 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002771void igb_configure_tx_ring(struct igb_adapter *adapter,
2772 struct igb_ring *ring)
Alexander Duyck85b430b2009-10-27 15:50:29 +00002773{
2774 struct e1000_hw *hw = &adapter->hw;
Alexander Duycka74420e2011-08-26 07:43:27 +00002775 u32 txdctl = 0;
Alexander Duyck85b430b2009-10-27 15:50:29 +00002776 u64 tdba = ring->dma;
2777 int reg_idx = ring->reg_idx;
2778
2779 /* disable the queue */
Alexander Duycka74420e2011-08-26 07:43:27 +00002780 wr32(E1000_TXDCTL(reg_idx), 0);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002781 wrfl();
2782 mdelay(10);
2783
2784 wr32(E1000_TDLEN(reg_idx),
2785 ring->count * sizeof(union e1000_adv_tx_desc));
2786 wr32(E1000_TDBAL(reg_idx),
2787 tdba & 0x00000000ffffffffULL);
2788 wr32(E1000_TDBAH(reg_idx), tdba >> 32);
2789
Alexander Duyckfce99e32009-10-27 15:51:27 +00002790 ring->tail = hw->hw_addr + E1000_TDT(reg_idx);
Alexander Duycka74420e2011-08-26 07:43:27 +00002791 wr32(E1000_TDH(reg_idx), 0);
Alexander Duyckfce99e32009-10-27 15:51:27 +00002792 writel(0, ring->tail);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002793
2794 txdctl |= IGB_TX_PTHRESH;
2795 txdctl |= IGB_TX_HTHRESH << 8;
2796 txdctl |= IGB_TX_WTHRESH << 16;
2797
2798 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
2799 wr32(E1000_TXDCTL(reg_idx), txdctl);
2800}
2801
2802/**
2803 * igb_configure_tx - Configure transmit Unit after Reset
2804 * @adapter: board private structure
2805 *
2806 * Configure the Tx unit of the MAC after a reset.
2807 **/
2808static void igb_configure_tx(struct igb_adapter *adapter)
2809{
2810 int i;
2811
2812 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00002813 igb_configure_tx_ring(adapter, adapter->tx_ring[i]);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002814}
2815
2816/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002817 * igb_setup_rx_resources - allocate Rx resources (Descriptors)
Auke Kok9d5c8242008-01-24 02:22:38 -08002818 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2819 *
2820 * Returns 0 on success, negative on failure
2821 **/
Alexander Duyck80785292009-10-27 15:51:47 +00002822int igb_setup_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002823{
Alexander Duyck59d71982010-04-27 13:09:25 +00002824 struct device *dev = rx_ring->dev;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002825 int size;
Auke Kok9d5c8242008-01-24 02:22:38 -08002826
Alexander Duyck06034642011-08-26 07:44:22 +00002827 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002828
2829 rx_ring->rx_buffer_info = vzalloc(size);
Alexander Duyck06034642011-08-26 07:44:22 +00002830 if (!rx_ring->rx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08002831 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -08002832
Auke Kok9d5c8242008-01-24 02:22:38 -08002833 /* Round up to nearest 4K */
Alexander Duyckf33005a2012-09-13 06:27:55 +00002834 rx_ring->size = rx_ring->count * sizeof(union e1000_adv_rx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08002835 rx_ring->size = ALIGN(rx_ring->size, 4096);
2836
Alexander Duyck5536d212012-09-25 00:31:17 +00002837 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
2838 &rx_ring->dma, GFP_KERNEL);
Auke Kok9d5c8242008-01-24 02:22:38 -08002839 if (!rx_ring->desc)
2840 goto err;
2841
Alexander Duyckcbc8e552012-09-25 00:31:02 +00002842 rx_ring->next_to_alloc = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002843 rx_ring->next_to_clean = 0;
2844 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002845
Auke Kok9d5c8242008-01-24 02:22:38 -08002846 return 0;
2847
2848err:
Alexander Duyck06034642011-08-26 07:44:22 +00002849 vfree(rx_ring->rx_buffer_info);
2850 rx_ring->rx_buffer_info = NULL;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002851 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08002852 return -ENOMEM;
2853}
2854
2855/**
2856 * igb_setup_all_rx_resources - wrapper to allocate Rx resources
2857 * (Descriptors) for all queues
2858 * @adapter: board private structure
2859 *
2860 * Return 0 on success, negative on failure
2861 **/
2862static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
2863{
Alexander Duyck439705e2009-10-27 23:49:20 +00002864 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002865 int i, err = 0;
2866
2867 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00002868 err = igb_setup_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002869 if (err) {
Alexander Duyck439705e2009-10-27 23:49:20 +00002870 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08002871 "Allocation for Rx Queue %u failed\n", i);
2872 for (i--; i >= 0; i--)
Alexander Duyck3025a442010-02-17 01:02:39 +00002873 igb_free_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002874 break;
2875 }
2876 }
2877
2878 return err;
2879}
2880
2881/**
Alexander Duyck06cf2662009-10-27 15:53:25 +00002882 * igb_setup_mrqc - configure the multiple receive queue control registers
2883 * @adapter: Board private structure
2884 **/
2885static void igb_setup_mrqc(struct igb_adapter *adapter)
2886{
2887 struct e1000_hw *hw = &adapter->hw;
2888 u32 mrqc, rxcsum;
Alexander Duyck797fd4b2012-09-13 06:28:11 +00002889 u32 j, num_rx_queues, shift = 0;
Alexander Duycka57fe232012-09-13 06:28:16 +00002890 static const u32 rsskey[10] = { 0xDA565A6D, 0xC20E5B25, 0x3D256741,
2891 0xB08FA343, 0xCB2BCAD0, 0xB4307BAE,
2892 0xA32DCB77, 0x0CF23080, 0x3BB7426A,
2893 0xFA01ACBE };
Alexander Duyck06cf2662009-10-27 15:53:25 +00002894
2895 /* Fill out hash function seeds */
Alexander Duycka57fe232012-09-13 06:28:16 +00002896 for (j = 0; j < 10; j++)
2897 wr32(E1000_RSSRK(j), rsskey[j]);
Alexander Duyck06cf2662009-10-27 15:53:25 +00002898
Alexander Duycka99955f2009-11-12 18:37:19 +00002899 num_rx_queues = adapter->rss_queues;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002900
Alexander Duyck797fd4b2012-09-13 06:28:11 +00002901 switch (hw->mac.type) {
2902 case e1000_82575:
2903 shift = 6;
2904 break;
2905 case e1000_82576:
2906 /* 82576 supports 2 RSS queues for SR-IOV */
2907 if (adapter->vfs_allocated_count) {
Alexander Duyck06cf2662009-10-27 15:53:25 +00002908 shift = 3;
2909 num_rx_queues = 2;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002910 }
Alexander Duyck797fd4b2012-09-13 06:28:11 +00002911 break;
2912 default:
2913 break;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002914 }
2915
Alexander Duyck797fd4b2012-09-13 06:28:11 +00002916 /*
2917 * Populate the indirection table 4 entries at a time. To do this
2918 * we are generating the results for n and n+2 and then interleaving
2919 * those with the results with n+1 and n+3.
2920 */
2921 for (j = 0; j < 32; j++) {
2922 /* first pass generates n and n+2 */
2923 u32 base = ((j * 0x00040004) + 0x00020000) * num_rx_queues;
2924 u32 reta = (base & 0x07800780) >> (7 - shift);
2925
2926 /* second pass generates n+1 and n+3 */
2927 base += 0x00010001 * num_rx_queues;
2928 reta |= (base & 0x07800780) << (1 + shift);
2929
2930 wr32(E1000_RETA(j), reta);
Alexander Duyck06cf2662009-10-27 15:53:25 +00002931 }
2932
2933 /*
2934 * Disable raw packet checksumming so that RSS hash is placed in
2935 * descriptor on writeback. No need to enable TCP/UDP/IP checksum
2936 * offloads as they are enabled by default
2937 */
2938 rxcsum = rd32(E1000_RXCSUM);
2939 rxcsum |= E1000_RXCSUM_PCSD;
2940
2941 if (adapter->hw.mac.type >= e1000_82576)
2942 /* Enable Receive Checksum Offload for SCTP */
2943 rxcsum |= E1000_RXCSUM_CRCOFL;
2944
2945 /* Don't need to set TUOFL or IPOFL, they default to 1 */
2946 wr32(E1000_RXCSUM, rxcsum);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002947
Akeem G. Abodunrin039454a2012-11-13 04:03:21 +00002948 /* Generate RSS hash based on packet types, TCP/UDP
2949 * port numbers and/or IPv4/v6 src and dst addresses
2950 */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002951 mrqc = E1000_MRQC_RSS_FIELD_IPV4 |
2952 E1000_MRQC_RSS_FIELD_IPV4_TCP |
2953 E1000_MRQC_RSS_FIELD_IPV6 |
2954 E1000_MRQC_RSS_FIELD_IPV6_TCP |
2955 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002956
Akeem G. Abodunrin039454a2012-11-13 04:03:21 +00002957 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV4_UDP)
2958 mrqc |= E1000_MRQC_RSS_FIELD_IPV4_UDP;
2959 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV6_UDP)
2960 mrqc |= E1000_MRQC_RSS_FIELD_IPV6_UDP;
2961
Alexander Duyck06cf2662009-10-27 15:53:25 +00002962 /* If VMDq is enabled then we set the appropriate mode for that, else
2963 * we default to RSS so that an RSS hash is calculated per packet even
2964 * if we are only using one queue */
2965 if (adapter->vfs_allocated_count) {
2966 if (hw->mac.type > e1000_82575) {
2967 /* Set the default pool for the PF's first queue */
2968 u32 vtctl = rd32(E1000_VT_CTL);
2969 vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
2970 E1000_VT_CTL_DISABLE_DEF_POOL);
2971 vtctl |= adapter->vfs_allocated_count <<
2972 E1000_VT_CTL_DEFAULT_POOL_SHIFT;
2973 wr32(E1000_VT_CTL, vtctl);
2974 }
Alexander Duycka99955f2009-11-12 18:37:19 +00002975 if (adapter->rss_queues > 1)
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002976 mrqc |= E1000_MRQC_ENABLE_VMDQ_RSS_2Q;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002977 else
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002978 mrqc |= E1000_MRQC_ENABLE_VMDQ;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002979 } else {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002980 if (hw->mac.type != e1000_i211)
2981 mrqc |= E1000_MRQC_ENABLE_RSS_4Q;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002982 }
2983 igb_vmm_control(adapter);
2984
Alexander Duyck06cf2662009-10-27 15:53:25 +00002985 wr32(E1000_MRQC, mrqc);
2986}
2987
2988/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002989 * igb_setup_rctl - configure the receive control registers
2990 * @adapter: Board private structure
2991 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002992void igb_setup_rctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002993{
2994 struct e1000_hw *hw = &adapter->hw;
2995 u32 rctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08002996
2997 rctl = rd32(E1000_RCTL);
2998
2999 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
Alexander Duyck69d728b2008-11-25 01:04:03 -08003000 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
Auke Kok9d5c8242008-01-24 02:22:38 -08003001
Alexander Duyck69d728b2008-11-25 01:04:03 -08003002 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
Alexander Duyck28b07592009-02-06 23:20:31 +00003003 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
Auke Kok9d5c8242008-01-24 02:22:38 -08003004
Auke Kok87cb7e82008-07-08 15:08:29 -07003005 /*
3006 * enable stripping of CRC. It's unlikely this will break BMC
3007 * redirection as it did with e1000. Newer features require
3008 * that the HW strips the CRC.
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003009 */
Auke Kok87cb7e82008-07-08 15:08:29 -07003010 rctl |= E1000_RCTL_SECRC;
Auke Kok9d5c8242008-01-24 02:22:38 -08003011
Alexander Duyck559e9c42009-10-27 23:52:50 +00003012 /* disable store bad packets and clear size bits. */
Alexander Duyckec54d7d2009-01-31 00:52:57 -08003013 rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
Auke Kok9d5c8242008-01-24 02:22:38 -08003014
Alexander Duyck6ec43fe2009-10-27 15:50:48 +00003015 /* enable LPE to prevent packets larger than max_frame_size */
3016 rctl |= E1000_RCTL_LPE;
Auke Kok9d5c8242008-01-24 02:22:38 -08003017
Alexander Duyck952f72a2009-10-27 15:51:07 +00003018 /* disable queue 0 to prevent tail write w/o re-config */
3019 wr32(E1000_RXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08003020
Alexander Duycke1739522009-02-19 20:39:44 -08003021 /* Attention!!! For SR-IOV PF driver operations you must enable
3022 * queue drop for all VF and PF queues to prevent head of line blocking
3023 * if an un-trusted VF does not provide descriptors to hardware.
3024 */
3025 if (adapter->vfs_allocated_count) {
Alexander Duycke1739522009-02-19 20:39:44 -08003026 /* set all queue drop enable bits */
3027 wr32(E1000_QDE, ALL_QUEUES);
Alexander Duycke1739522009-02-19 20:39:44 -08003028 }
3029
Ben Greear89eaefb2012-03-06 09:41:58 +00003030 /* This is useful for sniffing bad packets. */
3031 if (adapter->netdev->features & NETIF_F_RXALL) {
3032 /* UPE and MPE will be handled by normal PROMISC logic
3033 * in e1000e_set_rx_mode */
3034 rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
3035 E1000_RCTL_BAM | /* RX All Bcast Pkts */
3036 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
3037
3038 rctl &= ~(E1000_RCTL_VFE | /* Disable VLAN filter */
3039 E1000_RCTL_DPF | /* Allow filtered pause */
3040 E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
3041 /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
3042 * and that breaks VLANs.
3043 */
3044 }
3045
Auke Kok9d5c8242008-01-24 02:22:38 -08003046 wr32(E1000_RCTL, rctl);
3047}
3048
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003049static inline int igb_set_vf_rlpml(struct igb_adapter *adapter, int size,
3050 int vfn)
3051{
3052 struct e1000_hw *hw = &adapter->hw;
3053 u32 vmolr;
3054
3055 /* if it isn't the PF check to see if VFs are enabled and
3056 * increase the size to support vlan tags */
3057 if (vfn < adapter->vfs_allocated_count &&
3058 adapter->vf_data[vfn].vlans_enabled)
3059 size += VLAN_TAG_SIZE;
3060
3061 vmolr = rd32(E1000_VMOLR(vfn));
3062 vmolr &= ~E1000_VMOLR_RLPML_MASK;
3063 vmolr |= size | E1000_VMOLR_LPE;
3064 wr32(E1000_VMOLR(vfn), vmolr);
3065
3066 return 0;
3067}
3068
Auke Kok9d5c8242008-01-24 02:22:38 -08003069/**
Alexander Duycke1739522009-02-19 20:39:44 -08003070 * igb_rlpml_set - set maximum receive packet size
3071 * @adapter: board private structure
3072 *
3073 * Configure maximum receivable packet size.
3074 **/
3075static void igb_rlpml_set(struct igb_adapter *adapter)
3076{
Alexander Duyck153285f2011-08-26 07:43:32 +00003077 u32 max_frame_size = adapter->max_frame_size;
Alexander Duycke1739522009-02-19 20:39:44 -08003078 struct e1000_hw *hw = &adapter->hw;
3079 u16 pf_id = adapter->vfs_allocated_count;
3080
Alexander Duycke1739522009-02-19 20:39:44 -08003081 if (pf_id) {
3082 igb_set_vf_rlpml(adapter, max_frame_size, pf_id);
Alexander Duyck153285f2011-08-26 07:43:32 +00003083 /*
3084 * If we're in VMDQ or SR-IOV mode, then set global RLPML
3085 * to our max jumbo frame size, in case we need to enable
3086 * jumbo frames on one of the rings later.
3087 * This will not pass over-length frames into the default
3088 * queue because it's gated by the VMOLR.RLPML.
3089 */
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003090 max_frame_size = MAX_JUMBO_FRAME_SIZE;
Alexander Duycke1739522009-02-19 20:39:44 -08003091 }
3092
3093 wr32(E1000_RLPML, max_frame_size);
3094}
3095
Williams, Mitch A8151d292010-02-10 01:44:24 +00003096static inline void igb_set_vmolr(struct igb_adapter *adapter,
3097 int vfn, bool aupe)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003098{
3099 struct e1000_hw *hw = &adapter->hw;
3100 u32 vmolr;
3101
3102 /*
3103 * This register exists only on 82576 and newer so if we are older then
3104 * we should exit and do nothing
3105 */
3106 if (hw->mac.type < e1000_82576)
3107 return;
3108
3109 vmolr = rd32(E1000_VMOLR(vfn));
Williams, Mitch A8151d292010-02-10 01:44:24 +00003110 vmolr |= E1000_VMOLR_STRVLAN; /* Strip vlan tags */
3111 if (aupe)
3112 vmolr |= E1000_VMOLR_AUPE; /* Accept untagged packets */
3113 else
3114 vmolr &= ~(E1000_VMOLR_AUPE); /* Tagged packets ONLY */
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003115
3116 /* clear all bits that might not be set */
3117 vmolr &= ~(E1000_VMOLR_BAM | E1000_VMOLR_RSSE);
3118
Alexander Duycka99955f2009-11-12 18:37:19 +00003119 if (adapter->rss_queues > 1 && vfn == adapter->vfs_allocated_count)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003120 vmolr |= E1000_VMOLR_RSSE; /* enable RSS */
3121 /*
3122 * for VMDq only allow the VFs and pool 0 to accept broadcast and
3123 * multicast packets
3124 */
3125 if (vfn <= adapter->vfs_allocated_count)
3126 vmolr |= E1000_VMOLR_BAM; /* Accept broadcast */
3127
3128 wr32(E1000_VMOLR(vfn), vmolr);
3129}
3130
Alexander Duycke1739522009-02-19 20:39:44 -08003131/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00003132 * igb_configure_rx_ring - Configure a receive ring after Reset
3133 * @adapter: board private structure
3134 * @ring: receive ring to be configured
3135 *
3136 * Configure the Rx unit of the MAC after a reset.
3137 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00003138void igb_configure_rx_ring(struct igb_adapter *adapter,
3139 struct igb_ring *ring)
Alexander Duyck85b430b2009-10-27 15:50:29 +00003140{
3141 struct e1000_hw *hw = &adapter->hw;
3142 u64 rdba = ring->dma;
3143 int reg_idx = ring->reg_idx;
Alexander Duycka74420e2011-08-26 07:43:27 +00003144 u32 srrctl = 0, rxdctl = 0;
Alexander Duyck85b430b2009-10-27 15:50:29 +00003145
3146 /* disable the queue */
Alexander Duycka74420e2011-08-26 07:43:27 +00003147 wr32(E1000_RXDCTL(reg_idx), 0);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003148
3149 /* Set DMA base address registers */
3150 wr32(E1000_RDBAL(reg_idx),
3151 rdba & 0x00000000ffffffffULL);
3152 wr32(E1000_RDBAH(reg_idx), rdba >> 32);
3153 wr32(E1000_RDLEN(reg_idx),
3154 ring->count * sizeof(union e1000_adv_rx_desc));
3155
3156 /* initialize head and tail */
Alexander Duyckfce99e32009-10-27 15:51:27 +00003157 ring->tail = hw->hw_addr + E1000_RDT(reg_idx);
Alexander Duycka74420e2011-08-26 07:43:27 +00003158 wr32(E1000_RDH(reg_idx), 0);
Alexander Duyckfce99e32009-10-27 15:51:27 +00003159 writel(0, ring->tail);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003160
Alexander Duyck952f72a2009-10-27 15:51:07 +00003161 /* set descriptor configuration */
Alexander Duyck44390ca2011-08-26 07:43:38 +00003162 srrctl = IGB_RX_HDR_LEN << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
Alexander Duyckde78d1f2012-09-25 00:31:12 +00003163 srrctl |= IGB_RX_BUFSZ >> E1000_SRRCTL_BSIZEPKT_SHIFT;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003164 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
Alexander Duyck06218a82011-08-26 07:46:55 +00003165 if (hw->mac.type >= e1000_82580)
Nick Nunley757b77e2010-03-26 11:36:47 +00003166 srrctl |= E1000_SRRCTL_TIMESTAMP;
Nick Nunleye6bdb6f2010-02-17 01:03:38 +00003167 /* Only set Drop Enable if we are supporting multiple queues */
3168 if (adapter->vfs_allocated_count || adapter->num_rx_queues > 1)
3169 srrctl |= E1000_SRRCTL_DROP_EN;
Alexander Duyck952f72a2009-10-27 15:51:07 +00003170
3171 wr32(E1000_SRRCTL(reg_idx), srrctl);
3172
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003173 /* set filtering for VMDQ pools */
Williams, Mitch A8151d292010-02-10 01:44:24 +00003174 igb_set_vmolr(adapter, reg_idx & 0x7, true);
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003175
Alexander Duyck85b430b2009-10-27 15:50:29 +00003176 rxdctl |= IGB_RX_PTHRESH;
3177 rxdctl |= IGB_RX_HTHRESH << 8;
3178 rxdctl |= IGB_RX_WTHRESH << 16;
Alexander Duycka74420e2011-08-26 07:43:27 +00003179
3180 /* enable receive descriptor fetching */
3181 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
Alexander Duyck85b430b2009-10-27 15:50:29 +00003182 wr32(E1000_RXDCTL(reg_idx), rxdctl);
3183}
3184
3185/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003186 * igb_configure_rx - Configure receive Unit after Reset
3187 * @adapter: board private structure
3188 *
3189 * Configure the Rx unit of the MAC after a reset.
3190 **/
3191static void igb_configure_rx(struct igb_adapter *adapter)
3192{
Hannes Eder91075842009-02-18 19:36:04 -08003193 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003194
Alexander Duyck68d480c2009-10-05 06:33:08 +00003195 /* set UTA to appropriate mode */
3196 igb_set_uta(adapter);
3197
Alexander Duyck26ad9172009-10-05 06:32:49 +00003198 /* set the correct pool for the PF default MAC address in entry 0 */
3199 igb_rar_set_qsel(adapter, adapter->hw.mac.addr, 0,
3200 adapter->vfs_allocated_count);
3201
Alexander Duyck06cf2662009-10-27 15:53:25 +00003202 /* Setup the HW Rx Head and Tail Descriptor Pointers and
3203 * the Base and Length of the Rx Descriptor Ring */
3204 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003205 igb_configure_rx_ring(adapter, adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003206}
3207
3208/**
3209 * igb_free_tx_resources - Free Tx Resources per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08003210 * @tx_ring: Tx descriptor ring for a specific queue
3211 *
3212 * Free all transmit software resources
3213 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08003214void igb_free_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003215{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003216 igb_clean_tx_ring(tx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003217
Alexander Duyck06034642011-08-26 07:44:22 +00003218 vfree(tx_ring->tx_buffer_info);
3219 tx_ring->tx_buffer_info = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003220
Alexander Duyck439705e2009-10-27 23:49:20 +00003221 /* if not set, then don't free */
3222 if (!tx_ring->desc)
3223 return;
3224
Alexander Duyck59d71982010-04-27 13:09:25 +00003225 dma_free_coherent(tx_ring->dev, tx_ring->size,
3226 tx_ring->desc, tx_ring->dma);
Auke Kok9d5c8242008-01-24 02:22:38 -08003227
3228 tx_ring->desc = NULL;
3229}
3230
3231/**
3232 * igb_free_all_tx_resources - Free Tx Resources for All Queues
3233 * @adapter: board private structure
3234 *
3235 * Free all transmit software resources
3236 **/
3237static void igb_free_all_tx_resources(struct igb_adapter *adapter)
3238{
3239 int i;
3240
3241 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003242 igb_free_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003243}
3244
Alexander Duyckebe42d12011-08-26 07:45:09 +00003245void igb_unmap_and_free_tx_resource(struct igb_ring *ring,
3246 struct igb_tx_buffer *tx_buffer)
Auke Kok9d5c8242008-01-24 02:22:38 -08003247{
Alexander Duyckebe42d12011-08-26 07:45:09 +00003248 if (tx_buffer->skb) {
3249 dev_kfree_skb_any(tx_buffer->skb);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003250 if (dma_unmap_len(tx_buffer, len))
Alexander Duyckebe42d12011-08-26 07:45:09 +00003251 dma_unmap_single(ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003252 dma_unmap_addr(tx_buffer, dma),
3253 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00003254 DMA_TO_DEVICE);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003255 } else if (dma_unmap_len(tx_buffer, len)) {
Alexander Duyckebe42d12011-08-26 07:45:09 +00003256 dma_unmap_page(ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003257 dma_unmap_addr(tx_buffer, dma),
3258 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00003259 DMA_TO_DEVICE);
Alexander Duyck6366ad32009-12-02 16:47:18 +00003260 }
Alexander Duyckebe42d12011-08-26 07:45:09 +00003261 tx_buffer->next_to_watch = NULL;
3262 tx_buffer->skb = NULL;
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003263 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duyckebe42d12011-08-26 07:45:09 +00003264 /* buffer_info must be completely set up in the transmit path */
Auke Kok9d5c8242008-01-24 02:22:38 -08003265}
3266
3267/**
3268 * igb_clean_tx_ring - Free Tx Buffers
Auke Kok9d5c8242008-01-24 02:22:38 -08003269 * @tx_ring: ring to be cleaned
3270 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07003271static void igb_clean_tx_ring(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003272{
Alexander Duyck06034642011-08-26 07:44:22 +00003273 struct igb_tx_buffer *buffer_info;
Auke Kok9d5c8242008-01-24 02:22:38 -08003274 unsigned long size;
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00003275 u16 i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003276
Alexander Duyck06034642011-08-26 07:44:22 +00003277 if (!tx_ring->tx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003278 return;
3279 /* Free all the Tx ring sk_buffs */
3280
3281 for (i = 0; i < tx_ring->count; i++) {
Alexander Duyck06034642011-08-26 07:44:22 +00003282 buffer_info = &tx_ring->tx_buffer_info[i];
Alexander Duyck80785292009-10-27 15:51:47 +00003283 igb_unmap_and_free_tx_resource(tx_ring, buffer_info);
Auke Kok9d5c8242008-01-24 02:22:38 -08003284 }
3285
John Fastabenddad8a3b2012-04-23 12:22:39 +00003286 netdev_tx_reset_queue(txring_txq(tx_ring));
3287
Alexander Duyck06034642011-08-26 07:44:22 +00003288 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
3289 memset(tx_ring->tx_buffer_info, 0, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08003290
3291 /* Zero out the descriptor ring */
Auke Kok9d5c8242008-01-24 02:22:38 -08003292 memset(tx_ring->desc, 0, tx_ring->size);
3293
3294 tx_ring->next_to_use = 0;
3295 tx_ring->next_to_clean = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003296}
3297
3298/**
3299 * igb_clean_all_tx_rings - Free Tx Buffers for all queues
3300 * @adapter: board private structure
3301 **/
3302static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
3303{
3304 int i;
3305
3306 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003307 igb_clean_tx_ring(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003308}
3309
3310/**
3311 * igb_free_rx_resources - Free Rx Resources
Auke Kok9d5c8242008-01-24 02:22:38 -08003312 * @rx_ring: ring to clean the resources from
3313 *
3314 * Free all receive software resources
3315 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08003316void igb_free_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003317{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003318 igb_clean_rx_ring(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003319
Alexander Duyck06034642011-08-26 07:44:22 +00003320 vfree(rx_ring->rx_buffer_info);
3321 rx_ring->rx_buffer_info = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003322
Alexander Duyck439705e2009-10-27 23:49:20 +00003323 /* if not set, then don't free */
3324 if (!rx_ring->desc)
3325 return;
3326
Alexander Duyck59d71982010-04-27 13:09:25 +00003327 dma_free_coherent(rx_ring->dev, rx_ring->size,
3328 rx_ring->desc, rx_ring->dma);
Auke Kok9d5c8242008-01-24 02:22:38 -08003329
3330 rx_ring->desc = NULL;
3331}
3332
3333/**
3334 * igb_free_all_rx_resources - Free Rx Resources for All Queues
3335 * @adapter: board private structure
3336 *
3337 * Free all receive software resources
3338 **/
3339static void igb_free_all_rx_resources(struct igb_adapter *adapter)
3340{
3341 int i;
3342
3343 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003344 igb_free_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003345}
3346
3347/**
3348 * igb_clean_rx_ring - Free Rx Buffers per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08003349 * @rx_ring: ring to free buffers from
3350 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07003351static void igb_clean_rx_ring(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003352{
Auke Kok9d5c8242008-01-24 02:22:38 -08003353 unsigned long size;
Alexander Duyckc023cd82011-08-26 07:43:43 +00003354 u16 i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003355
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003356 if (rx_ring->skb)
3357 dev_kfree_skb(rx_ring->skb);
3358 rx_ring->skb = NULL;
3359
Alexander Duyck06034642011-08-26 07:44:22 +00003360 if (!rx_ring->rx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003361 return;
Alexander Duyck439705e2009-10-27 23:49:20 +00003362
Auke Kok9d5c8242008-01-24 02:22:38 -08003363 /* Free all the Rx ring sk_buffs */
3364 for (i = 0; i < rx_ring->count; i++) {
Alexander Duyck06034642011-08-26 07:44:22 +00003365 struct igb_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08003366
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003367 if (!buffer_info->page)
3368 continue;
3369
3370 dma_unmap_page(rx_ring->dev,
3371 buffer_info->dma,
3372 PAGE_SIZE,
3373 DMA_FROM_DEVICE);
3374 __free_page(buffer_info->page);
3375
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003376 buffer_info->page = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003377 }
3378
Alexander Duyck06034642011-08-26 07:44:22 +00003379 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
3380 memset(rx_ring->rx_buffer_info, 0, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08003381
3382 /* Zero out the descriptor ring */
3383 memset(rx_ring->desc, 0, rx_ring->size);
3384
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003385 rx_ring->next_to_alloc = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003386 rx_ring->next_to_clean = 0;
3387 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003388}
3389
3390/**
3391 * igb_clean_all_rx_rings - Free Rx Buffers for all queues
3392 * @adapter: board private structure
3393 **/
3394static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
3395{
3396 int i;
3397
3398 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003399 igb_clean_rx_ring(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003400}
3401
3402/**
3403 * igb_set_mac - Change the Ethernet Address of the NIC
3404 * @netdev: network interface device structure
3405 * @p: pointer to an address structure
3406 *
3407 * Returns 0 on success, negative on failure
3408 **/
3409static int igb_set_mac(struct net_device *netdev, void *p)
3410{
3411 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck28b07592009-02-06 23:20:31 +00003412 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08003413 struct sockaddr *addr = p;
3414
3415 if (!is_valid_ether_addr(addr->sa_data))
3416 return -EADDRNOTAVAIL;
3417
3418 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Alexander Duyck28b07592009-02-06 23:20:31 +00003419 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
Auke Kok9d5c8242008-01-24 02:22:38 -08003420
Alexander Duyck26ad9172009-10-05 06:32:49 +00003421 /* set the correct pool for the new PF MAC address in entry 0 */
3422 igb_rar_set_qsel(adapter, hw->mac.addr, 0,
3423 adapter->vfs_allocated_count);
Alexander Duycke1739522009-02-19 20:39:44 -08003424
Auke Kok9d5c8242008-01-24 02:22:38 -08003425 return 0;
3426}
3427
3428/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00003429 * igb_write_mc_addr_list - write multicast addresses to MTA
3430 * @netdev: network interface device structure
3431 *
3432 * Writes multicast address list to the MTA hash table.
3433 * Returns: -ENOMEM on failure
3434 * 0 on no addresses written
3435 * X on writing X addresses to MTA
3436 **/
3437static int igb_write_mc_addr_list(struct net_device *netdev)
3438{
3439 struct igb_adapter *adapter = netdev_priv(netdev);
3440 struct e1000_hw *hw = &adapter->hw;
Jiri Pirko22bedad32010-04-01 21:22:57 +00003441 struct netdev_hw_addr *ha;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003442 u8 *mta_list;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003443 int i;
3444
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003445 if (netdev_mc_empty(netdev)) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003446 /* nothing to program, so clear mc list */
3447 igb_update_mc_addr_list(hw, NULL, 0);
3448 igb_restore_vf_multicasts(adapter);
3449 return 0;
3450 }
3451
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003452 mta_list = kzalloc(netdev_mc_count(netdev) * 6, GFP_ATOMIC);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003453 if (!mta_list)
3454 return -ENOMEM;
3455
Alexander Duyck68d480c2009-10-05 06:33:08 +00003456 /* The shared function expects a packed array of only addresses. */
Jiri Pirko48e2f182010-02-22 09:22:26 +00003457 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00003458 netdev_for_each_mc_addr(ha, netdev)
3459 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003460
Alexander Duyck68d480c2009-10-05 06:33:08 +00003461 igb_update_mc_addr_list(hw, mta_list, i);
3462 kfree(mta_list);
3463
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003464 return netdev_mc_count(netdev);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003465}
3466
3467/**
3468 * igb_write_uc_addr_list - write unicast addresses to RAR table
3469 * @netdev: network interface device structure
3470 *
3471 * Writes unicast address list to the RAR table.
3472 * Returns: -ENOMEM on failure/insufficient address space
3473 * 0 on no addresses written
3474 * X on writing X addresses to the RAR table
3475 **/
3476static int igb_write_uc_addr_list(struct net_device *netdev)
3477{
3478 struct igb_adapter *adapter = netdev_priv(netdev);
3479 struct e1000_hw *hw = &adapter->hw;
3480 unsigned int vfn = adapter->vfs_allocated_count;
3481 unsigned int rar_entries = hw->mac.rar_entry_count - (vfn + 1);
3482 int count = 0;
3483
3484 /* return ENOMEM indicating insufficient memory for addresses */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003485 if (netdev_uc_count(netdev) > rar_entries)
Alexander Duyck68d480c2009-10-05 06:33:08 +00003486 return -ENOMEM;
3487
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003488 if (!netdev_uc_empty(netdev) && rar_entries) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003489 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003490
3491 netdev_for_each_uc_addr(ha, netdev) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003492 if (!rar_entries)
3493 break;
3494 igb_rar_set_qsel(adapter, ha->addr,
3495 rar_entries--,
3496 vfn);
3497 count++;
3498 }
3499 }
3500 /* write the addresses in reverse order to avoid write combining */
3501 for (; rar_entries > 0 ; rar_entries--) {
3502 wr32(E1000_RAH(rar_entries), 0);
3503 wr32(E1000_RAL(rar_entries), 0);
3504 }
3505 wrfl();
3506
3507 return count;
3508}
3509
3510/**
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003511 * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
Auke Kok9d5c8242008-01-24 02:22:38 -08003512 * @netdev: network interface device structure
3513 *
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003514 * The set_rx_mode entry point is called whenever the unicast or multicast
3515 * address lists or the network interface flags are updated. This routine is
3516 * responsible for configuring the hardware for proper unicast, multicast,
Auke Kok9d5c8242008-01-24 02:22:38 -08003517 * promiscuous mode, and all-multi behavior.
3518 **/
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003519static void igb_set_rx_mode(struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08003520{
3521 struct igb_adapter *adapter = netdev_priv(netdev);
3522 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003523 unsigned int vfn = adapter->vfs_allocated_count;
3524 u32 rctl, vmolr = 0;
3525 int count;
Auke Kok9d5c8242008-01-24 02:22:38 -08003526
3527 /* Check for Promiscuous and All Multicast modes */
Auke Kok9d5c8242008-01-24 02:22:38 -08003528 rctl = rd32(E1000_RCTL);
3529
Alexander Duyck68d480c2009-10-05 06:33:08 +00003530 /* clear the effected bits */
3531 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_VFE);
3532
Patrick McHardy746b9f02008-07-16 20:15:45 -07003533 if (netdev->flags & IFF_PROMISC) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003534 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003535 vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_MPME);
Patrick McHardy746b9f02008-07-16 20:15:45 -07003536 } else {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003537 if (netdev->flags & IFF_ALLMULTI) {
Patrick McHardy746b9f02008-07-16 20:15:45 -07003538 rctl |= E1000_RCTL_MPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003539 vmolr |= E1000_VMOLR_MPME;
3540 } else {
3541 /*
3542 * Write addresses to the MTA, if the attempt fails
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003543 * then we should just turn on promiscuous mode so
Alexander Duyck68d480c2009-10-05 06:33:08 +00003544 * that we can at least receive multicast traffic
3545 */
3546 count = igb_write_mc_addr_list(netdev);
3547 if (count < 0) {
3548 rctl |= E1000_RCTL_MPE;
3549 vmolr |= E1000_VMOLR_MPME;
3550 } else if (count) {
3551 vmolr |= E1000_VMOLR_ROMPE;
3552 }
3553 }
3554 /*
3555 * Write addresses to available RAR registers, if there is not
3556 * sufficient space to store all the addresses then enable
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003557 * unicast promiscuous mode
Alexander Duyck68d480c2009-10-05 06:33:08 +00003558 */
3559 count = igb_write_uc_addr_list(netdev);
3560 if (count < 0) {
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003561 rctl |= E1000_RCTL_UPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003562 vmolr |= E1000_VMOLR_ROPE;
3563 }
Patrick McHardy78ed11a2008-07-16 20:16:14 -07003564 rctl |= E1000_RCTL_VFE;
Patrick McHardy746b9f02008-07-16 20:15:45 -07003565 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003566 wr32(E1000_RCTL, rctl);
3567
Alexander Duyck68d480c2009-10-05 06:33:08 +00003568 /*
3569 * In order to support SR-IOV and eventually VMDq it is necessary to set
3570 * the VMOLR to enable the appropriate modes. Without this workaround
3571 * we will have issues with VLAN tag stripping not being done for frames
3572 * that are only arriving because we are the default pool
3573 */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003574 if ((hw->mac.type < e1000_82576) || (hw->mac.type > e1000_i350))
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003575 return;
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003576
Alexander Duyck68d480c2009-10-05 06:33:08 +00003577 vmolr |= rd32(E1000_VMOLR(vfn)) &
3578 ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE);
3579 wr32(E1000_VMOLR(vfn), vmolr);
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003580 igb_restore_vf_multicasts(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003581}
3582
Greg Rose13800462010-11-06 02:08:26 +00003583static void igb_check_wvbr(struct igb_adapter *adapter)
3584{
3585 struct e1000_hw *hw = &adapter->hw;
3586 u32 wvbr = 0;
3587
3588 switch (hw->mac.type) {
3589 case e1000_82576:
3590 case e1000_i350:
3591 if (!(wvbr = rd32(E1000_WVBR)))
3592 return;
3593 break;
3594 default:
3595 break;
3596 }
3597
3598 adapter->wvbr |= wvbr;
3599}
3600
3601#define IGB_STAGGERED_QUEUE_OFFSET 8
3602
3603static void igb_spoof_check(struct igb_adapter *adapter)
3604{
3605 int j;
3606
3607 if (!adapter->wvbr)
3608 return;
3609
3610 for(j = 0; j < adapter->vfs_allocated_count; j++) {
3611 if (adapter->wvbr & (1 << j) ||
3612 adapter->wvbr & (1 << (j + IGB_STAGGERED_QUEUE_OFFSET))) {
3613 dev_warn(&adapter->pdev->dev,
3614 "Spoof event(s) detected on VF %d\n", j);
3615 adapter->wvbr &=
3616 ~((1 << j) |
3617 (1 << (j + IGB_STAGGERED_QUEUE_OFFSET)));
3618 }
3619 }
3620}
3621
Auke Kok9d5c8242008-01-24 02:22:38 -08003622/* Need to wait a few seconds after link up to get diagnostic information from
3623 * the phy */
3624static void igb_update_phy_info(unsigned long data)
3625{
3626 struct igb_adapter *adapter = (struct igb_adapter *) data;
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08003627 igb_get_phy_info(&adapter->hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08003628}
3629
3630/**
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003631 * igb_has_link - check shared code for link and determine up/down
3632 * @adapter: pointer to driver private info
3633 **/
Nick Nunley31455352010-02-17 01:01:21 +00003634bool igb_has_link(struct igb_adapter *adapter)
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003635{
3636 struct e1000_hw *hw = &adapter->hw;
3637 bool link_active = false;
3638 s32 ret_val = 0;
3639
3640 /* get_link_status is set on LSC (link status) interrupt or
3641 * rx sequence error interrupt. get_link_status will stay
3642 * false until the e1000_check_for_link establishes link
3643 * for copper adapters ONLY
3644 */
3645 switch (hw->phy.media_type) {
3646 case e1000_media_type_copper:
3647 if (hw->mac.get_link_status) {
3648 ret_val = hw->mac.ops.check_for_link(hw);
3649 link_active = !hw->mac.get_link_status;
3650 } else {
3651 link_active = true;
3652 }
3653 break;
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003654 case e1000_media_type_internal_serdes:
3655 ret_val = hw->mac.ops.check_for_link(hw);
3656 link_active = hw->mac.serdes_has_link;
3657 break;
3658 default:
3659 case e1000_media_type_unknown:
3660 break;
3661 }
3662
3663 return link_active;
3664}
3665
Stefan Assmann563988d2011-04-05 04:27:15 +00003666static bool igb_thermal_sensor_event(struct e1000_hw *hw, u32 event)
3667{
3668 bool ret = false;
3669 u32 ctrl_ext, thstat;
3670
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003671 /* check for thermal sensor event on i350 copper only */
Stefan Assmann563988d2011-04-05 04:27:15 +00003672 if (hw->mac.type == e1000_i350) {
3673 thstat = rd32(E1000_THSTAT);
3674 ctrl_ext = rd32(E1000_CTRL_EXT);
3675
3676 if ((hw->phy.media_type == e1000_media_type_copper) &&
3677 !(ctrl_ext & E1000_CTRL_EXT_LINK_MODE_SGMII)) {
3678 ret = !!(thstat & event);
3679 }
3680 }
3681
3682 return ret;
3683}
3684
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003685/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003686 * igb_watchdog - Timer Call-back
3687 * @data: pointer to adapter cast into an unsigned long
3688 **/
3689static void igb_watchdog(unsigned long data)
3690{
3691 struct igb_adapter *adapter = (struct igb_adapter *)data;
3692 /* Do the rest outside of interrupt context */
3693 schedule_work(&adapter->watchdog_task);
3694}
3695
3696static void igb_watchdog_task(struct work_struct *work)
3697{
3698 struct igb_adapter *adapter = container_of(work,
Alexander Duyck559e9c42009-10-27 23:52:50 +00003699 struct igb_adapter,
3700 watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08003701 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08003702 struct net_device *netdev = adapter->netdev;
Stefan Assmann563988d2011-04-05 04:27:15 +00003703 u32 link;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003704 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003705
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003706 link = igb_has_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003707 if (link) {
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003708 /* Cancel scheduled suspend requests. */
3709 pm_runtime_resume(netdev->dev.parent);
3710
Auke Kok9d5c8242008-01-24 02:22:38 -08003711 if (!netif_carrier_ok(netdev)) {
3712 u32 ctrl;
Alexander Duyck330a6d62009-10-27 23:51:35 +00003713 hw->mac.ops.get_speed_and_duplex(hw,
3714 &adapter->link_speed,
3715 &adapter->link_duplex);
Auke Kok9d5c8242008-01-24 02:22:38 -08003716
3717 ctrl = rd32(E1000_CTRL);
Alexander Duyck527d47c2008-11-27 00:21:39 -08003718 /* Links status message must follow this format */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003719 printk(KERN_INFO "igb: %s NIC Link is Up %d Mbps %s "
3720 "Duplex, Flow Control: %s\n",
Alexander Duyck559e9c42009-10-27 23:52:50 +00003721 netdev->name,
3722 adapter->link_speed,
3723 adapter->link_duplex == FULL_DUPLEX ?
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003724 "Full" : "Half",
3725 (ctrl & E1000_CTRL_TFCE) &&
3726 (ctrl & E1000_CTRL_RFCE) ? "RX/TX" :
3727 (ctrl & E1000_CTRL_RFCE) ? "RX" :
3728 (ctrl & E1000_CTRL_TFCE) ? "TX" : "None");
Auke Kok9d5c8242008-01-24 02:22:38 -08003729
Stefan Assmann563988d2011-04-05 04:27:15 +00003730 /* check for thermal sensor event */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003731 if (igb_thermal_sensor_event(hw,
3732 E1000_THSTAT_LINK_THROTTLE)) {
3733 netdev_info(netdev, "The network adapter link "
3734 "speed was downshifted because it "
3735 "overheated\n");
Carolyn Wyborny7ef5ed12011-03-12 08:59:47 +00003736 }
Stefan Assmann563988d2011-04-05 04:27:15 +00003737
Emil Tantilovd07f3e32010-03-23 18:34:57 +00003738 /* adjust timeout factor according to speed/duplex */
Auke Kok9d5c8242008-01-24 02:22:38 -08003739 adapter->tx_timeout_factor = 1;
3740 switch (adapter->link_speed) {
3741 case SPEED_10:
Auke Kok9d5c8242008-01-24 02:22:38 -08003742 adapter->tx_timeout_factor = 14;
3743 break;
3744 case SPEED_100:
Auke Kok9d5c8242008-01-24 02:22:38 -08003745 /* maybe add some timeout factor ? */
3746 break;
3747 }
3748
3749 netif_carrier_on(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08003750
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003751 igb_ping_all_vfs(adapter);
Lior Levy17dc5662011-02-08 02:28:46 +00003752 igb_check_vf_rate_limit(adapter);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003753
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003754 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08003755 if (!test_bit(__IGB_DOWN, &adapter->state))
3756 mod_timer(&adapter->phy_info_timer,
3757 round_jiffies(jiffies + 2 * HZ));
3758 }
3759 } else {
3760 if (netif_carrier_ok(netdev)) {
3761 adapter->link_speed = 0;
3762 adapter->link_duplex = 0;
Stefan Assmann563988d2011-04-05 04:27:15 +00003763
3764 /* check for thermal sensor event */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003765 if (igb_thermal_sensor_event(hw,
3766 E1000_THSTAT_PWR_DOWN)) {
3767 netdev_err(netdev, "The network adapter was "
3768 "stopped because it overheated\n");
Carolyn Wyborny7ef5ed12011-03-12 08:59:47 +00003769 }
Stefan Assmann563988d2011-04-05 04:27:15 +00003770
Alexander Duyck527d47c2008-11-27 00:21:39 -08003771 /* Links status message must follow this format */
3772 printk(KERN_INFO "igb: %s NIC Link is Down\n",
3773 netdev->name);
Auke Kok9d5c8242008-01-24 02:22:38 -08003774 netif_carrier_off(netdev);
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003775
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003776 igb_ping_all_vfs(adapter);
3777
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003778 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08003779 if (!test_bit(__IGB_DOWN, &adapter->state))
3780 mod_timer(&adapter->phy_info_timer,
3781 round_jiffies(jiffies + 2 * HZ));
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003782
3783 pm_schedule_suspend(netdev->dev.parent,
3784 MSEC_PER_SEC * 5);
Auke Kok9d5c8242008-01-24 02:22:38 -08003785 }
3786 }
3787
Eric Dumazet12dcd862010-10-15 17:27:10 +00003788 spin_lock(&adapter->stats64_lock);
3789 igb_update_stats(adapter, &adapter->stats64);
3790 spin_unlock(&adapter->stats64_lock);
Auke Kok9d5c8242008-01-24 02:22:38 -08003791
Alexander Duyckdbabb062009-11-12 18:38:16 +00003792 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00003793 struct igb_ring *tx_ring = adapter->tx_ring[i];
Alexander Duyckdbabb062009-11-12 18:38:16 +00003794 if (!netif_carrier_ok(netdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003795 /* We've lost link, so the controller stops DMA,
3796 * but we've got queued Tx work that's never going
3797 * to get done, so reset controller to flush Tx.
3798 * (Do the reset outside of interrupt context). */
Alexander Duyckdbabb062009-11-12 18:38:16 +00003799 if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) {
3800 adapter->tx_timeout_count++;
3801 schedule_work(&adapter->reset_task);
3802 /* return immediately since reset is imminent */
3803 return;
3804 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003805 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003806
Alexander Duyckdbabb062009-11-12 18:38:16 +00003807 /* Force detection of hung controller every watchdog period */
Alexander Duyck6d095fa2011-08-26 07:46:19 +00003808 set_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
Alexander Duyckdbabb062009-11-12 18:38:16 +00003809 }
Alexander Duyckf7ba2052009-10-27 23:48:51 +00003810
Auke Kok9d5c8242008-01-24 02:22:38 -08003811 /* Cause software interrupt to ensure rx ring is cleaned */
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003812 if (adapter->msix_entries) {
Alexander Duyck047e0032009-10-27 15:49:27 +00003813 u32 eics = 0;
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00003814 for (i = 0; i < adapter->num_q_vectors; i++)
3815 eics |= adapter->q_vector[i]->eims_value;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003816 wr32(E1000_EICS, eics);
3817 } else {
3818 wr32(E1000_ICS, E1000_ICS_RXDMT0);
3819 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003820
Greg Rose13800462010-11-06 02:08:26 +00003821 igb_spoof_check(adapter);
3822
Auke Kok9d5c8242008-01-24 02:22:38 -08003823 /* Reset the timer */
3824 if (!test_bit(__IGB_DOWN, &adapter->state))
3825 mod_timer(&adapter->watchdog_timer,
3826 round_jiffies(jiffies + 2 * HZ));
3827}
3828
3829enum latency_range {
3830 lowest_latency = 0,
3831 low_latency = 1,
3832 bulk_latency = 2,
3833 latency_invalid = 255
3834};
3835
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003836/**
3837 * igb_update_ring_itr - update the dynamic ITR value based on packet size
3838 *
3839 * Stores a new ITR value based on strictly on packet size. This
3840 * algorithm is less sophisticated than that used in igb_update_itr,
3841 * due to the difficulty of synchronizing statistics across multiple
Stefan Weileef35c22010-08-06 21:11:15 +02003842 * receive rings. The divisors and thresholds used by this function
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003843 * were determined based on theoretical maximum wire speed and testing
3844 * data, in order to minimize response time while increasing bulk
3845 * throughput.
3846 * This functionality is controlled by the InterruptThrottleRate module
3847 * parameter (see igb_param.c)
3848 * NOTE: This function is called only when operating in a multiqueue
3849 * receive environment.
Alexander Duyck047e0032009-10-27 15:49:27 +00003850 * @q_vector: pointer to q_vector
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003851 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00003852static void igb_update_ring_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08003853{
Alexander Duyck047e0032009-10-27 15:49:27 +00003854 int new_val = q_vector->itr_val;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003855 int avg_wire_size = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +00003856 struct igb_adapter *adapter = q_vector->adapter;
Eric Dumazet12dcd862010-10-15 17:27:10 +00003857 unsigned int packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08003858
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003859 /* For non-gigabit speeds, just fix the interrupt rate at 4000
3860 * ints/sec - ITR timer value of 120 ticks.
3861 */
3862 if (adapter->link_speed != SPEED_1000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003863 new_val = IGB_4K_ITR;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003864 goto set_itr_val;
3865 }
Alexander Duyck047e0032009-10-27 15:49:27 +00003866
Alexander Duyck0ba82992011-08-26 07:45:47 +00003867 packets = q_vector->rx.total_packets;
3868 if (packets)
3869 avg_wire_size = q_vector->rx.total_bytes / packets;
Eric Dumazet12dcd862010-10-15 17:27:10 +00003870
Alexander Duyck0ba82992011-08-26 07:45:47 +00003871 packets = q_vector->tx.total_packets;
3872 if (packets)
3873 avg_wire_size = max_t(u32, avg_wire_size,
3874 q_vector->tx.total_bytes / packets);
Alexander Duyck047e0032009-10-27 15:49:27 +00003875
3876 /* if avg_wire_size isn't set no work was done */
3877 if (!avg_wire_size)
3878 goto clear_counts;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003879
3880 /* Add 24 bytes to size to account for CRC, preamble, and gap */
3881 avg_wire_size += 24;
3882
3883 /* Don't starve jumbo frames */
3884 avg_wire_size = min(avg_wire_size, 3000);
3885
3886 /* Give a little boost to mid-size frames */
3887 if ((avg_wire_size > 300) && (avg_wire_size < 1200))
3888 new_val = avg_wire_size / 3;
3889 else
3890 new_val = avg_wire_size / 2;
3891
Alexander Duyck0ba82992011-08-26 07:45:47 +00003892 /* conservative mode (itr 3) eliminates the lowest_latency setting */
3893 if (new_val < IGB_20K_ITR &&
3894 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
3895 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
3896 new_val = IGB_20K_ITR;
Nick Nunleyabe1c362010-02-17 01:03:19 +00003897
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003898set_itr_val:
Alexander Duyck047e0032009-10-27 15:49:27 +00003899 if (new_val != q_vector->itr_val) {
3900 q_vector->itr_val = new_val;
3901 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08003902 }
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003903clear_counts:
Alexander Duyck0ba82992011-08-26 07:45:47 +00003904 q_vector->rx.total_bytes = 0;
3905 q_vector->rx.total_packets = 0;
3906 q_vector->tx.total_bytes = 0;
3907 q_vector->tx.total_packets = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003908}
3909
3910/**
3911 * igb_update_itr - update the dynamic ITR value based on statistics
3912 * Stores a new ITR value based on packets and byte
3913 * counts during the last interrupt. The advantage of per interrupt
3914 * computation is faster updates and more accurate ITR for the current
3915 * traffic pattern. Constants in this function were computed
3916 * based on theoretical maximum wire speed and thresholds were set based
3917 * on testing data as well as attempting to minimize response time
3918 * while increasing bulk throughput.
3919 * this functionality is controlled by the InterruptThrottleRate module
3920 * parameter (see igb_param.c)
3921 * NOTE: These calculations are only valid when operating in a single-
3922 * queue environment.
Alexander Duyck0ba82992011-08-26 07:45:47 +00003923 * @q_vector: pointer to q_vector
3924 * @ring_container: ring info to update the itr for
Auke Kok9d5c8242008-01-24 02:22:38 -08003925 **/
Alexander Duyck0ba82992011-08-26 07:45:47 +00003926static void igb_update_itr(struct igb_q_vector *q_vector,
3927 struct igb_ring_container *ring_container)
Auke Kok9d5c8242008-01-24 02:22:38 -08003928{
Alexander Duyck0ba82992011-08-26 07:45:47 +00003929 unsigned int packets = ring_container->total_packets;
3930 unsigned int bytes = ring_container->total_bytes;
3931 u8 itrval = ring_container->itr;
Auke Kok9d5c8242008-01-24 02:22:38 -08003932
Alexander Duyck0ba82992011-08-26 07:45:47 +00003933 /* no packets, exit with status unchanged */
Auke Kok9d5c8242008-01-24 02:22:38 -08003934 if (packets == 0)
Alexander Duyck0ba82992011-08-26 07:45:47 +00003935 return;
Auke Kok9d5c8242008-01-24 02:22:38 -08003936
Alexander Duyck0ba82992011-08-26 07:45:47 +00003937 switch (itrval) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003938 case lowest_latency:
3939 /* handle TSO and jumbo frames */
3940 if (bytes/packets > 8000)
Alexander Duyck0ba82992011-08-26 07:45:47 +00003941 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003942 else if ((packets < 5) && (bytes > 512))
Alexander Duyck0ba82992011-08-26 07:45:47 +00003943 itrval = low_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003944 break;
3945 case low_latency: /* 50 usec aka 20000 ints/s */
3946 if (bytes > 10000) {
3947 /* this if handles the TSO accounting */
3948 if (bytes/packets > 8000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003949 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003950 } else if ((packets < 10) || ((bytes/packets) > 1200)) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003951 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003952 } else if ((packets > 35)) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003953 itrval = lowest_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003954 }
3955 } else if (bytes/packets > 2000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003956 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003957 } else if (packets <= 2 && bytes < 512) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003958 itrval = lowest_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003959 }
3960 break;
3961 case bulk_latency: /* 250 usec aka 4000 ints/s */
3962 if (bytes > 25000) {
3963 if (packets > 35)
Alexander Duyck0ba82992011-08-26 07:45:47 +00003964 itrval = low_latency;
Alexander Duyck1e5c3d22009-02-12 18:17:21 +00003965 } else if (bytes < 1500) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003966 itrval = low_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003967 }
3968 break;
3969 }
3970
Alexander Duyck0ba82992011-08-26 07:45:47 +00003971 /* clear work counters since we have the values we need */
3972 ring_container->total_bytes = 0;
3973 ring_container->total_packets = 0;
3974
3975 /* write updated itr to ring container */
3976 ring_container->itr = itrval;
Auke Kok9d5c8242008-01-24 02:22:38 -08003977}
3978
Alexander Duyck0ba82992011-08-26 07:45:47 +00003979static void igb_set_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08003980{
Alexander Duyck0ba82992011-08-26 07:45:47 +00003981 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck047e0032009-10-27 15:49:27 +00003982 u32 new_itr = q_vector->itr_val;
Alexander Duyck0ba82992011-08-26 07:45:47 +00003983 u8 current_itr = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003984
3985 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
3986 if (adapter->link_speed != SPEED_1000) {
3987 current_itr = 0;
Alexander Duyck0ba82992011-08-26 07:45:47 +00003988 new_itr = IGB_4K_ITR;
Auke Kok9d5c8242008-01-24 02:22:38 -08003989 goto set_itr_now;
3990 }
3991
Alexander Duyck0ba82992011-08-26 07:45:47 +00003992 igb_update_itr(q_vector, &q_vector->tx);
3993 igb_update_itr(q_vector, &q_vector->rx);
Auke Kok9d5c8242008-01-24 02:22:38 -08003994
Alexander Duyck0ba82992011-08-26 07:45:47 +00003995 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
Auke Kok9d5c8242008-01-24 02:22:38 -08003996
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003997 /* conservative mode (itr 3) eliminates the lowest_latency setting */
Alexander Duyck0ba82992011-08-26 07:45:47 +00003998 if (current_itr == lowest_latency &&
3999 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
4000 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004001 current_itr = low_latency;
4002
Auke Kok9d5c8242008-01-24 02:22:38 -08004003 switch (current_itr) {
4004 /* counts and packets in update_itr are dependent on these numbers */
4005 case lowest_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00004006 new_itr = IGB_70K_ITR; /* 70,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08004007 break;
4008 case low_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00004009 new_itr = IGB_20K_ITR; /* 20,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08004010 break;
4011 case bulk_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00004012 new_itr = IGB_4K_ITR; /* 4,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08004013 break;
4014 default:
4015 break;
4016 }
4017
4018set_itr_now:
Alexander Duyck047e0032009-10-27 15:49:27 +00004019 if (new_itr != q_vector->itr_val) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004020 /* this attempts to bias the interrupt rate towards Bulk
4021 * by adding intermediate steps when interrupt rate is
4022 * increasing */
Alexander Duyck047e0032009-10-27 15:49:27 +00004023 new_itr = new_itr > q_vector->itr_val ?
4024 max((new_itr * q_vector->itr_val) /
4025 (new_itr + (q_vector->itr_val >> 2)),
Alexander Duyck0ba82992011-08-26 07:45:47 +00004026 new_itr) :
Auke Kok9d5c8242008-01-24 02:22:38 -08004027 new_itr;
4028 /* Don't write the value here; it resets the adapter's
4029 * internal timer, and causes us to delay far longer than
4030 * we should between interrupts. Instead, we write the ITR
4031 * value at the beginning of the next interrupt so the timing
4032 * ends up being correct.
4033 */
Alexander Duyck047e0032009-10-27 15:49:27 +00004034 q_vector->itr_val = new_itr;
4035 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08004036 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004037}
4038
Stephen Hemmingerc50b52a2012-01-18 22:13:26 +00004039static void igb_tx_ctxtdesc(struct igb_ring *tx_ring, u32 vlan_macip_lens,
4040 u32 type_tucmd, u32 mss_l4len_idx)
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004041{
4042 struct e1000_adv_tx_context_desc *context_desc;
4043 u16 i = tx_ring->next_to_use;
4044
4045 context_desc = IGB_TX_CTXTDESC(tx_ring, i);
4046
4047 i++;
4048 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
4049
4050 /* set bits to identify this as an advanced context descriptor */
4051 type_tucmd |= E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
4052
4053 /* For 82575, context index must be unique per ring. */
Alexander Duyck866cff02011-08-26 07:45:36 +00004054 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004055 mss_l4len_idx |= tx_ring->reg_idx << 4;
4056
4057 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
4058 context_desc->seqnum_seed = 0;
4059 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
4060 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
4061}
4062
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004063static int igb_tso(struct igb_ring *tx_ring,
4064 struct igb_tx_buffer *first,
4065 u8 *hdr_len)
Auke Kok9d5c8242008-01-24 02:22:38 -08004066{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004067 struct sk_buff *skb = first->skb;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004068 u32 vlan_macip_lens, type_tucmd;
4069 u32 mss_l4len_idx, l4len;
4070
Alexander Duycked6aa102012-11-13 04:03:22 +00004071 if (skb->ip_summed != CHECKSUM_PARTIAL)
4072 return 0;
4073
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004074 if (!skb_is_gso(skb))
4075 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004076
4077 if (skb_header_cloned(skb)) {
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004078 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004079 if (err)
4080 return err;
4081 }
4082
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004083 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
4084 type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP;
Auke Kok9d5c8242008-01-24 02:22:38 -08004085
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004086 if (first->protocol == __constant_htons(ETH_P_IP)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004087 struct iphdr *iph = ip_hdr(skb);
4088 iph->tot_len = 0;
4089 iph->check = 0;
4090 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
4091 iph->daddr, 0,
4092 IPPROTO_TCP,
4093 0);
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004094 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004095 first->tx_flags |= IGB_TX_FLAGS_TSO |
4096 IGB_TX_FLAGS_CSUM |
4097 IGB_TX_FLAGS_IPV4;
Sridhar Samudrala8e1e8a42010-01-23 02:02:21 -08004098 } else if (skb_is_gso_v6(skb)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004099 ipv6_hdr(skb)->payload_len = 0;
4100 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4101 &ipv6_hdr(skb)->daddr,
4102 0, IPPROTO_TCP, 0);
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004103 first->tx_flags |= IGB_TX_FLAGS_TSO |
4104 IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08004105 }
4106
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004107 /* compute header lengths */
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004108 l4len = tcp_hdrlen(skb);
4109 *hdr_len = skb_transport_offset(skb) + l4len;
Auke Kok9d5c8242008-01-24 02:22:38 -08004110
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004111 /* update gso size and bytecount with header size */
4112 first->gso_segs = skb_shinfo(skb)->gso_segs;
4113 first->bytecount += (first->gso_segs - 1) * *hdr_len;
4114
Auke Kok9d5c8242008-01-24 02:22:38 -08004115 /* MSS L4LEN IDX */
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004116 mss_l4len_idx = l4len << E1000_ADVTXD_L4LEN_SHIFT;
4117 mss_l4len_idx |= skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT;
Auke Kok9d5c8242008-01-24 02:22:38 -08004118
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004119 /* VLAN MACLEN IPLEN */
4120 vlan_macip_lens = skb_network_header_len(skb);
4121 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004122 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
Auke Kok9d5c8242008-01-24 02:22:38 -08004123
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004124 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004125
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004126 return 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08004127}
4128
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004129static void igb_tx_csum(struct igb_ring *tx_ring, struct igb_tx_buffer *first)
Auke Kok9d5c8242008-01-24 02:22:38 -08004130{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004131 struct sk_buff *skb = first->skb;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004132 u32 vlan_macip_lens = 0;
4133 u32 mss_l4len_idx = 0;
4134 u32 type_tucmd = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004135
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004136 if (skb->ip_summed != CHECKSUM_PARTIAL) {
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004137 if (!(first->tx_flags & IGB_TX_FLAGS_VLAN))
4138 return;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004139 } else {
4140 u8 l4_hdr = 0;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004141 switch (first->protocol) {
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004142 case __constant_htons(ETH_P_IP):
4143 vlan_macip_lens |= skb_network_header_len(skb);
4144 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
4145 l4_hdr = ip_hdr(skb)->protocol;
4146 break;
4147 case __constant_htons(ETH_P_IPV6):
4148 vlan_macip_lens |= skb_network_header_len(skb);
4149 l4_hdr = ipv6_hdr(skb)->nexthdr;
4150 break;
4151 default:
4152 if (unlikely(net_ratelimit())) {
4153 dev_warn(tx_ring->dev,
4154 "partial checksum but proto=%x!\n",
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004155 first->protocol);
Arthur Jonesfa4a7ef2009-03-21 16:55:07 -07004156 }
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004157 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08004158 }
4159
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004160 switch (l4_hdr) {
4161 case IPPROTO_TCP:
4162 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
4163 mss_l4len_idx = tcp_hdrlen(skb) <<
4164 E1000_ADVTXD_L4LEN_SHIFT;
4165 break;
4166 case IPPROTO_SCTP:
4167 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
4168 mss_l4len_idx = sizeof(struct sctphdr) <<
4169 E1000_ADVTXD_L4LEN_SHIFT;
4170 break;
4171 case IPPROTO_UDP:
4172 mss_l4len_idx = sizeof(struct udphdr) <<
4173 E1000_ADVTXD_L4LEN_SHIFT;
4174 break;
4175 default:
4176 if (unlikely(net_ratelimit())) {
4177 dev_warn(tx_ring->dev,
4178 "partial checksum but l4 proto=%x!\n",
4179 l4_hdr);
4180 }
4181 break;
4182 }
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004183
4184 /* update TX checksum flag */
4185 first->tx_flags |= IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08004186 }
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004187
4188 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004189 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004190
4191 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004192}
4193
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004194#define IGB_SET_FLAG(_input, _flag, _result) \
4195 ((_flag <= _result) ? \
4196 ((u32)(_input & _flag) * (_result / _flag)) : \
4197 ((u32)(_input & _flag) / (_flag / _result)))
4198
4199static u32 igb_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
Alexander Duycke032afc2011-08-26 07:44:48 +00004200{
4201 /* set type for advanced descriptor with frame checksum insertion */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004202 u32 cmd_type = E1000_ADVTXD_DTYP_DATA |
4203 E1000_ADVTXD_DCMD_DEXT |
4204 E1000_ADVTXD_DCMD_IFCS;
Alexander Duycke032afc2011-08-26 07:44:48 +00004205
4206 /* set HW vlan bit if vlan is present */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004207 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_VLAN,
4208 (E1000_ADVTXD_DCMD_VLE));
Alexander Duycke032afc2011-08-26 07:44:48 +00004209
4210 /* set segmentation bits for TSO */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004211 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSO,
4212 (E1000_ADVTXD_DCMD_TSE));
4213
4214 /* set timestamp bit if present */
4215 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSTAMP,
4216 (E1000_ADVTXD_MAC_TSTAMP));
4217
4218 /* insert frame checksum */
4219 cmd_type ^= IGB_SET_FLAG(skb->no_fcs, 1, E1000_ADVTXD_DCMD_IFCS);
Alexander Duycke032afc2011-08-26 07:44:48 +00004220
4221 return cmd_type;
4222}
4223
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004224static void igb_tx_olinfo_status(struct igb_ring *tx_ring,
4225 union e1000_adv_tx_desc *tx_desc,
4226 u32 tx_flags, unsigned int paylen)
Alexander Duycke032afc2011-08-26 07:44:48 +00004227{
4228 u32 olinfo_status = paylen << E1000_ADVTXD_PAYLEN_SHIFT;
4229
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004230 /* 82575 requires a unique index per ring */
4231 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
Alexander Duycke032afc2011-08-26 07:44:48 +00004232 olinfo_status |= tx_ring->reg_idx << 4;
4233
4234 /* insert L4 checksum */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004235 olinfo_status |= IGB_SET_FLAG(tx_flags,
4236 IGB_TX_FLAGS_CSUM,
4237 (E1000_TXD_POPTS_TXSM << 8));
Alexander Duycke032afc2011-08-26 07:44:48 +00004238
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004239 /* insert IPv4 checksum */
4240 olinfo_status |= IGB_SET_FLAG(tx_flags,
4241 IGB_TX_FLAGS_IPV4,
4242 (E1000_TXD_POPTS_IXSM << 8));
Alexander Duycke032afc2011-08-26 07:44:48 +00004243
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004244 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
Alexander Duycke032afc2011-08-26 07:44:48 +00004245}
4246
Alexander Duyckebe42d12011-08-26 07:45:09 +00004247/*
4248 * The largest size we can write to the descriptor is 65535. In order to
4249 * maintain a power of two alignment we have to limit ourselves to 32K.
4250 */
4251#define IGB_MAX_TXD_PWR 15
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004252#define IGB_MAX_DATA_PER_TXD (1<<IGB_MAX_TXD_PWR)
Auke Kok9d5c8242008-01-24 02:22:38 -08004253
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004254static void igb_tx_map(struct igb_ring *tx_ring,
4255 struct igb_tx_buffer *first,
Alexander Duyckebe42d12011-08-26 07:45:09 +00004256 const u8 hdr_len)
Auke Kok9d5c8242008-01-24 02:22:38 -08004257{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004258 struct sk_buff *skb = first->skb;
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004259 struct igb_tx_buffer *tx_buffer;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004260 union e1000_adv_tx_desc *tx_desc;
Alexander Duyck80d07592012-11-13 04:03:24 +00004261 struct skb_frag_struct *frag;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004262 dma_addr_t dma;
Alexander Duyck80d07592012-11-13 04:03:24 +00004263 unsigned int data_len, size;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004264 u32 tx_flags = first->tx_flags;
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004265 u32 cmd_type = igb_tx_cmd_type(skb, tx_flags);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004266 u16 i = tx_ring->next_to_use;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004267
4268 tx_desc = IGB_TX_DESC(tx_ring, i);
4269
Alexander Duyck80d07592012-11-13 04:03:24 +00004270 igb_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
4271
4272 size = skb_headlen(skb);
4273 data_len = skb->data_len;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004274
4275 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08004276
Alexander Duyck80d07592012-11-13 04:03:24 +00004277 tx_buffer = first;
Alexander Duyck2bbfebe2011-08-26 07:44:59 +00004278
Alexander Duyck80d07592012-11-13 04:03:24 +00004279 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
4280 if (dma_mapping_error(tx_ring->dev, dma))
4281 goto dma_error;
4282
4283 /* record length, and DMA address */
4284 dma_unmap_len_set(tx_buffer, len, size);
4285 dma_unmap_addr_set(tx_buffer, dma, dma);
4286
4287 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4288
Alexander Duyckebe42d12011-08-26 07:45:09 +00004289 while (unlikely(size > IGB_MAX_DATA_PER_TXD)) {
4290 tx_desc->read.cmd_type_len =
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004291 cpu_to_le32(cmd_type ^ IGB_MAX_DATA_PER_TXD);
Auke Kok9d5c8242008-01-24 02:22:38 -08004292
Alexander Duyckebe42d12011-08-26 07:45:09 +00004293 i++;
4294 tx_desc++;
4295 if (i == tx_ring->count) {
4296 tx_desc = IGB_TX_DESC(tx_ring, 0);
4297 i = 0;
4298 }
Alexander Duyck80d07592012-11-13 04:03:24 +00004299 tx_desc->read.olinfo_status = 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004300
4301 dma += IGB_MAX_DATA_PER_TXD;
4302 size -= IGB_MAX_DATA_PER_TXD;
4303
Alexander Duyckebe42d12011-08-26 07:45:09 +00004304 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4305 }
4306
4307 if (likely(!data_len))
4308 break;
4309
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004310 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004311
Alexander Duyck65689fe2009-03-20 00:17:43 +00004312 i++;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004313 tx_desc++;
4314 if (i == tx_ring->count) {
4315 tx_desc = IGB_TX_DESC(tx_ring, 0);
Alexander Duyck65689fe2009-03-20 00:17:43 +00004316 i = 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004317 }
Alexander Duyck80d07592012-11-13 04:03:24 +00004318 tx_desc->read.olinfo_status = 0;
Alexander Duyck65689fe2009-03-20 00:17:43 +00004319
Eric Dumazet9e903e02011-10-18 21:00:24 +00004320 size = skb_frag_size(frag);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004321 data_len -= size;
4322
4323 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
Alexander Duyck80d07592012-11-13 04:03:24 +00004324 size, DMA_TO_DEVICE);
Alexander Duyck6366ad32009-12-02 16:47:18 +00004325
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004326 tx_buffer = &tx_ring->tx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08004327 }
4328
Alexander Duyckebe42d12011-08-26 07:45:09 +00004329 /* write last descriptor with RS and EOP bits */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004330 cmd_type |= size | IGB_TXD_DCMD;
4331 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
Alexander Duyck8542db02011-08-26 07:44:43 +00004332
Alexander Duyck80d07592012-11-13 04:03:24 +00004333 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
4334
Alexander Duyck8542db02011-08-26 07:44:43 +00004335 /* set the timestamp */
4336 first->time_stamp = jiffies;
4337
Alexander Duyckebe42d12011-08-26 07:45:09 +00004338 /*
4339 * Force memory writes to complete before letting h/w know there
4340 * are new descriptors to fetch. (Only applicable for weak-ordered
4341 * memory model archs, such as IA-64).
4342 *
4343 * We also need this memory barrier to make certain all of the
4344 * status bits have been updated before next_to_watch is written.
4345 */
Auke Kok9d5c8242008-01-24 02:22:38 -08004346 wmb();
4347
Alexander Duyckebe42d12011-08-26 07:45:09 +00004348 /* set next_to_watch value indicating a packet is present */
4349 first->next_to_watch = tx_desc;
4350
4351 i++;
4352 if (i == tx_ring->count)
4353 i = 0;
4354
Auke Kok9d5c8242008-01-24 02:22:38 -08004355 tx_ring->next_to_use = i;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004356
Alexander Duyckfce99e32009-10-27 15:51:27 +00004357 writel(i, tx_ring->tail);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004358
Auke Kok9d5c8242008-01-24 02:22:38 -08004359 /* we need this if more than one processor can write to our tail
4360 * at a time, it syncronizes IO on IA64/Altix systems */
4361 mmiowb();
Alexander Duyckebe42d12011-08-26 07:45:09 +00004362
4363 return;
4364
4365dma_error:
4366 dev_err(tx_ring->dev, "TX DMA map failed\n");
4367
4368 /* clear dma mappings for failed tx_buffer_info map */
4369 for (;;) {
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004370 tx_buffer = &tx_ring->tx_buffer_info[i];
4371 igb_unmap_and_free_tx_resource(tx_ring, tx_buffer);
4372 if (tx_buffer == first)
Alexander Duyckebe42d12011-08-26 07:45:09 +00004373 break;
4374 if (i == 0)
4375 i = tx_ring->count;
4376 i--;
4377 }
4378
4379 tx_ring->next_to_use = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08004380}
4381
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00004382static int __igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004383{
Alexander Duycke694e962009-10-27 15:53:06 +00004384 struct net_device *netdev = tx_ring->netdev;
4385
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004386 netif_stop_subqueue(netdev, tx_ring->queue_index);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004387
Auke Kok9d5c8242008-01-24 02:22:38 -08004388 /* Herbert's original patch had:
4389 * smp_mb__after_netif_stop_queue();
4390 * but since that doesn't exist yet, just open code it. */
4391 smp_mb();
4392
4393 /* We need to check again in a case another CPU has just
4394 * made room available. */
Alexander Duyckc493ea42009-03-20 00:16:50 +00004395 if (igb_desc_unused(tx_ring) < size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004396 return -EBUSY;
4397
4398 /* A reprieve! */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004399 netif_wake_subqueue(netdev, tx_ring->queue_index);
Eric Dumazet12dcd862010-10-15 17:27:10 +00004400
4401 u64_stats_update_begin(&tx_ring->tx_syncp2);
4402 tx_ring->tx_stats.restart_queue2++;
4403 u64_stats_update_end(&tx_ring->tx_syncp2);
4404
Auke Kok9d5c8242008-01-24 02:22:38 -08004405 return 0;
4406}
4407
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00004408static inline int igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004409{
Alexander Duyckc493ea42009-03-20 00:16:50 +00004410 if (igb_desc_unused(tx_ring) >= size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004411 return 0;
Alexander Duycke694e962009-10-27 15:53:06 +00004412 return __igb_maybe_stop_tx(tx_ring, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08004413}
4414
Alexander Duyckcd392f52011-08-26 07:43:59 +00004415netdev_tx_t igb_xmit_frame_ring(struct sk_buff *skb,
4416 struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08004417{
Matthew Vick1f6e8172012-08-18 07:26:33 +00004418 struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
Alexander Duyck8542db02011-08-26 07:44:43 +00004419 struct igb_tx_buffer *first;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004420 int tso;
Nick Nunley91d4ee32010-02-17 01:04:56 +00004421 u32 tx_flags = 0;
Alexander Duyck31f6adb2011-08-26 07:44:53 +00004422 __be16 protocol = vlan_get_protocol(skb);
Nick Nunley91d4ee32010-02-17 01:04:56 +00004423 u8 hdr_len = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004424
Auke Kok9d5c8242008-01-24 02:22:38 -08004425 /* need: 1 descriptor per page,
4426 * + 2 desc gap to keep tail from touching head,
4427 * + 1 desc for skb->data,
4428 * + 1 desc for context descriptor,
4429 * otherwise try next time */
Alexander Duycke694e962009-10-27 15:53:06 +00004430 if (igb_maybe_stop_tx(tx_ring, skb_shinfo(skb)->nr_frags + 4)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004431 /* this is a hard error */
Auke Kok9d5c8242008-01-24 02:22:38 -08004432 return NETDEV_TX_BUSY;
4433 }
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004434
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004435 /* record the location of the first descriptor for this packet */
4436 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
4437 first->skb = skb;
4438 first->bytecount = skb->len;
4439 first->gso_segs = 1;
4440
Matthew Vick1f6e8172012-08-18 07:26:33 +00004441 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4442 !(adapter->ptp_tx_skb))) {
Oliver Hartkopp2244d072010-08-17 08:59:14 +00004443 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004444 tx_flags |= IGB_TX_FLAGS_TSTAMP;
Matthew Vick1f6e8172012-08-18 07:26:33 +00004445
4446 adapter->ptp_tx_skb = skb_get(skb);
4447 if (adapter->hw.mac.type == e1000_82576)
4448 schedule_work(&adapter->ptp_tx_work);
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004449 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004450
Jesse Grosseab6d182010-10-20 13:56:03 +00004451 if (vlan_tx_tag_present(skb)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004452 tx_flags |= IGB_TX_FLAGS_VLAN;
4453 tx_flags |= (vlan_tx_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
4454 }
4455
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004456 /* record initial flags and protocol */
4457 first->tx_flags = tx_flags;
4458 first->protocol = protocol;
Alexander Duyckcdfd01fc2009-10-27 23:50:57 +00004459
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004460 tso = igb_tso(tx_ring, first, &hdr_len);
4461 if (tso < 0)
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004462 goto out_drop;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004463 else if (!tso)
4464 igb_tx_csum(tx_ring, first);
Auke Kok9d5c8242008-01-24 02:22:38 -08004465
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004466 igb_tx_map(tx_ring, first, hdr_len);
Alexander Duyck85ad76b2009-10-27 15:52:46 +00004467
4468 /* Make sure there is space in the ring for the next send. */
Alexander Duycke694e962009-10-27 15:53:06 +00004469 igb_maybe_stop_tx(tx_ring, MAX_SKB_FRAGS + 4);
Alexander Duyck85ad76b2009-10-27 15:52:46 +00004470
Auke Kok9d5c8242008-01-24 02:22:38 -08004471 return NETDEV_TX_OK;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004472
4473out_drop:
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004474 igb_unmap_and_free_tx_resource(tx_ring, first);
4475
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004476 return NETDEV_TX_OK;
Auke Kok9d5c8242008-01-24 02:22:38 -08004477}
4478
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004479static inline struct igb_ring *igb_tx_queue_mapping(struct igb_adapter *adapter,
4480 struct sk_buff *skb)
4481{
4482 unsigned int r_idx = skb->queue_mapping;
4483
4484 if (r_idx >= adapter->num_tx_queues)
4485 r_idx = r_idx % adapter->num_tx_queues;
4486
4487 return adapter->tx_ring[r_idx];
4488}
4489
Alexander Duyckcd392f52011-08-26 07:43:59 +00004490static netdev_tx_t igb_xmit_frame(struct sk_buff *skb,
4491 struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08004492{
4493 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckb1a436c2009-10-27 15:54:43 +00004494
4495 if (test_bit(__IGB_DOWN, &adapter->state)) {
4496 dev_kfree_skb_any(skb);
4497 return NETDEV_TX_OK;
4498 }
4499
4500 if (skb->len <= 0) {
4501 dev_kfree_skb_any(skb);
4502 return NETDEV_TX_OK;
4503 }
4504
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004505 /*
4506 * The minimum packet size with TCTL.PSP set is 17 so pad the skb
4507 * in order to meet this minimum size requirement.
4508 */
Tushar Daveea5ceea2012-09-14 03:43:43 +00004509 if (unlikely(skb->len < 17)) {
4510 if (skb_pad(skb, 17 - skb->len))
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004511 return NETDEV_TX_OK;
4512 skb->len = 17;
Tushar Daveea5ceea2012-09-14 03:43:43 +00004513 skb_set_tail_pointer(skb, 17);
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004514 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004515
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004516 return igb_xmit_frame_ring(skb, igb_tx_queue_mapping(adapter, skb));
Auke Kok9d5c8242008-01-24 02:22:38 -08004517}
4518
4519/**
4520 * igb_tx_timeout - Respond to a Tx Hang
4521 * @netdev: network interface device structure
4522 **/
4523static void igb_tx_timeout(struct net_device *netdev)
4524{
4525 struct igb_adapter *adapter = netdev_priv(netdev);
4526 struct e1000_hw *hw = &adapter->hw;
4527
4528 /* Do the reset outside of interrupt context */
4529 adapter->tx_timeout_count++;
Alexander Duyckf7ba2052009-10-27 23:48:51 +00004530
Alexander Duyck06218a82011-08-26 07:46:55 +00004531 if (hw->mac.type >= e1000_82580)
Alexander Duyck55cac242009-11-19 12:42:21 +00004532 hw->dev_spec._82575.global_device_reset = true;
4533
Auke Kok9d5c8242008-01-24 02:22:38 -08004534 schedule_work(&adapter->reset_task);
Alexander Duyck265de402009-02-06 23:22:52 +00004535 wr32(E1000_EICS,
4536 (adapter->eims_enable_mask & ~adapter->eims_other));
Auke Kok9d5c8242008-01-24 02:22:38 -08004537}
4538
4539static void igb_reset_task(struct work_struct *work)
4540{
4541 struct igb_adapter *adapter;
4542 adapter = container_of(work, struct igb_adapter, reset_task);
4543
Taku Izumic97ec422010-04-27 14:39:30 +00004544 igb_dump(adapter);
4545 netdev_err(adapter->netdev, "Reset adapter\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004546 igb_reinit_locked(adapter);
4547}
4548
4549/**
Eric Dumazet12dcd862010-10-15 17:27:10 +00004550 * igb_get_stats64 - Get System Network Statistics
Auke Kok9d5c8242008-01-24 02:22:38 -08004551 * @netdev: network interface device structure
Eric Dumazet12dcd862010-10-15 17:27:10 +00004552 * @stats: rtnl_link_stats64 pointer
Auke Kok9d5c8242008-01-24 02:22:38 -08004553 *
Auke Kok9d5c8242008-01-24 02:22:38 -08004554 **/
Eric Dumazet12dcd862010-10-15 17:27:10 +00004555static struct rtnl_link_stats64 *igb_get_stats64(struct net_device *netdev,
4556 struct rtnl_link_stats64 *stats)
Auke Kok9d5c8242008-01-24 02:22:38 -08004557{
Eric Dumazet12dcd862010-10-15 17:27:10 +00004558 struct igb_adapter *adapter = netdev_priv(netdev);
4559
4560 spin_lock(&adapter->stats64_lock);
4561 igb_update_stats(adapter, &adapter->stats64);
4562 memcpy(stats, &adapter->stats64, sizeof(*stats));
4563 spin_unlock(&adapter->stats64_lock);
4564
4565 return stats;
Auke Kok9d5c8242008-01-24 02:22:38 -08004566}
4567
4568/**
4569 * igb_change_mtu - Change the Maximum Transfer Unit
4570 * @netdev: network interface device structure
4571 * @new_mtu: new value for maximum frame size
4572 *
4573 * Returns 0 on success, negative on failure
4574 **/
4575static int igb_change_mtu(struct net_device *netdev, int new_mtu)
4576{
4577 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck090b1792009-10-27 23:51:55 +00004578 struct pci_dev *pdev = adapter->pdev;
Alexander Duyck153285f2011-08-26 07:43:32 +00004579 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Auke Kok9d5c8242008-01-24 02:22:38 -08004580
Alexander Duyckc809d222009-10-27 23:52:13 +00004581 if ((new_mtu < 68) || (max_frame > MAX_JUMBO_FRAME_SIZE)) {
Alexander Duyck090b1792009-10-27 23:51:55 +00004582 dev_err(&pdev->dev, "Invalid MTU setting\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004583 return -EINVAL;
4584 }
4585
Alexander Duyck153285f2011-08-26 07:43:32 +00004586#define MAX_STD_JUMBO_FRAME_SIZE 9238
Auke Kok9d5c8242008-01-24 02:22:38 -08004587 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
Alexander Duyck090b1792009-10-27 23:51:55 +00004588 dev_err(&pdev->dev, "MTU > 9216 not supported.\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004589 return -EINVAL;
4590 }
4591
4592 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
4593 msleep(1);
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004594
Auke Kok9d5c8242008-01-24 02:22:38 -08004595 /* igb_down has a dependency on max_frame_size */
4596 adapter->max_frame_size = max_frame;
Alexander Duyck559e9c42009-10-27 23:52:50 +00004597
Alexander Duyck4c844852009-10-27 15:52:07 +00004598 if (netif_running(netdev))
4599 igb_down(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08004600
Alexander Duyck090b1792009-10-27 23:51:55 +00004601 dev_info(&pdev->dev, "changing MTU from %d to %d\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08004602 netdev->mtu, new_mtu);
4603 netdev->mtu = new_mtu;
4604
4605 if (netif_running(netdev))
4606 igb_up(adapter);
4607 else
4608 igb_reset(adapter);
4609
4610 clear_bit(__IGB_RESETTING, &adapter->state);
4611
4612 return 0;
4613}
4614
4615/**
4616 * igb_update_stats - Update the board statistics counters
4617 * @adapter: board private structure
4618 **/
4619
Eric Dumazet12dcd862010-10-15 17:27:10 +00004620void igb_update_stats(struct igb_adapter *adapter,
4621 struct rtnl_link_stats64 *net_stats)
Auke Kok9d5c8242008-01-24 02:22:38 -08004622{
4623 struct e1000_hw *hw = &adapter->hw;
4624 struct pci_dev *pdev = adapter->pdev;
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004625 u32 reg, mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004626 u16 phy_tmp;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004627 int i;
4628 u64 bytes, packets;
Eric Dumazet12dcd862010-10-15 17:27:10 +00004629 unsigned int start;
4630 u64 _bytes, _packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08004631
4632#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
4633
4634 /*
4635 * Prevent stats update while adapter is being reset, or if the pci
4636 * connection is down.
4637 */
4638 if (adapter->link_speed == 0)
4639 return;
4640 if (pci_channel_offline(pdev))
4641 return;
4642
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004643 bytes = 0;
4644 packets = 0;
4645 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyckae1c07a2012-08-08 05:23:22 +00004646 u32 rqdpc = rd32(E1000_RQDPC(i));
Alexander Duyck3025a442010-02-17 01:02:39 +00004647 struct igb_ring *ring = adapter->rx_ring[i];
Eric Dumazet12dcd862010-10-15 17:27:10 +00004648
Alexander Duyckae1c07a2012-08-08 05:23:22 +00004649 if (rqdpc) {
4650 ring->rx_stats.drops += rqdpc;
4651 net_stats->rx_fifo_errors += rqdpc;
4652 }
Eric Dumazet12dcd862010-10-15 17:27:10 +00004653
4654 do {
4655 start = u64_stats_fetch_begin_bh(&ring->rx_syncp);
4656 _bytes = ring->rx_stats.bytes;
4657 _packets = ring->rx_stats.packets;
4658 } while (u64_stats_fetch_retry_bh(&ring->rx_syncp, start));
4659 bytes += _bytes;
4660 packets += _packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004661 }
4662
Alexander Duyck128e45e2009-11-12 18:37:38 +00004663 net_stats->rx_bytes = bytes;
4664 net_stats->rx_packets = packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004665
4666 bytes = 0;
4667 packets = 0;
4668 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00004669 struct igb_ring *ring = adapter->tx_ring[i];
Eric Dumazet12dcd862010-10-15 17:27:10 +00004670 do {
4671 start = u64_stats_fetch_begin_bh(&ring->tx_syncp);
4672 _bytes = ring->tx_stats.bytes;
4673 _packets = ring->tx_stats.packets;
4674 } while (u64_stats_fetch_retry_bh(&ring->tx_syncp, start));
4675 bytes += _bytes;
4676 packets += _packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004677 }
Alexander Duyck128e45e2009-11-12 18:37:38 +00004678 net_stats->tx_bytes = bytes;
4679 net_stats->tx_packets = packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004680
4681 /* read stats registers */
Auke Kok9d5c8242008-01-24 02:22:38 -08004682 adapter->stats.crcerrs += rd32(E1000_CRCERRS);
4683 adapter->stats.gprc += rd32(E1000_GPRC);
4684 adapter->stats.gorc += rd32(E1000_GORCL);
4685 rd32(E1000_GORCH); /* clear GORCL */
4686 adapter->stats.bprc += rd32(E1000_BPRC);
4687 adapter->stats.mprc += rd32(E1000_MPRC);
4688 adapter->stats.roc += rd32(E1000_ROC);
4689
4690 adapter->stats.prc64 += rd32(E1000_PRC64);
4691 adapter->stats.prc127 += rd32(E1000_PRC127);
4692 adapter->stats.prc255 += rd32(E1000_PRC255);
4693 adapter->stats.prc511 += rd32(E1000_PRC511);
4694 adapter->stats.prc1023 += rd32(E1000_PRC1023);
4695 adapter->stats.prc1522 += rd32(E1000_PRC1522);
4696 adapter->stats.symerrs += rd32(E1000_SYMERRS);
4697 adapter->stats.sec += rd32(E1000_SEC);
4698
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004699 mpc = rd32(E1000_MPC);
4700 adapter->stats.mpc += mpc;
4701 net_stats->rx_fifo_errors += mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004702 adapter->stats.scc += rd32(E1000_SCC);
4703 adapter->stats.ecol += rd32(E1000_ECOL);
4704 adapter->stats.mcc += rd32(E1000_MCC);
4705 adapter->stats.latecol += rd32(E1000_LATECOL);
4706 adapter->stats.dc += rd32(E1000_DC);
4707 adapter->stats.rlec += rd32(E1000_RLEC);
4708 adapter->stats.xonrxc += rd32(E1000_XONRXC);
4709 adapter->stats.xontxc += rd32(E1000_XONTXC);
4710 adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
4711 adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
4712 adapter->stats.fcruc += rd32(E1000_FCRUC);
4713 adapter->stats.gptc += rd32(E1000_GPTC);
4714 adapter->stats.gotc += rd32(E1000_GOTCL);
4715 rd32(E1000_GOTCH); /* clear GOTCL */
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004716 adapter->stats.rnbc += rd32(E1000_RNBC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004717 adapter->stats.ruc += rd32(E1000_RUC);
4718 adapter->stats.rfc += rd32(E1000_RFC);
4719 adapter->stats.rjc += rd32(E1000_RJC);
4720 adapter->stats.tor += rd32(E1000_TORH);
4721 adapter->stats.tot += rd32(E1000_TOTH);
4722 adapter->stats.tpr += rd32(E1000_TPR);
4723
4724 adapter->stats.ptc64 += rd32(E1000_PTC64);
4725 adapter->stats.ptc127 += rd32(E1000_PTC127);
4726 adapter->stats.ptc255 += rd32(E1000_PTC255);
4727 adapter->stats.ptc511 += rd32(E1000_PTC511);
4728 adapter->stats.ptc1023 += rd32(E1000_PTC1023);
4729 adapter->stats.ptc1522 += rd32(E1000_PTC1522);
4730
4731 adapter->stats.mptc += rd32(E1000_MPTC);
4732 adapter->stats.bptc += rd32(E1000_BPTC);
4733
Nick Nunley2d0b0f62010-02-17 01:02:59 +00004734 adapter->stats.tpt += rd32(E1000_TPT);
4735 adapter->stats.colc += rd32(E1000_COLC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004736
4737 adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
Nick Nunley43915c7c2010-02-17 01:03:58 +00004738 /* read internal phy specific stats */
4739 reg = rd32(E1000_CTRL_EXT);
4740 if (!(reg & E1000_CTRL_EXT_LINK_MODE_MASK)) {
4741 adapter->stats.rxerrc += rd32(E1000_RXERRC);
Carolyn Wyborny3dbdf962012-09-12 04:36:24 +00004742
4743 /* this stat has invalid values on i210/i211 */
4744 if ((hw->mac.type != e1000_i210) &&
4745 (hw->mac.type != e1000_i211))
4746 adapter->stats.tncrs += rd32(E1000_TNCRS);
Nick Nunley43915c7c2010-02-17 01:03:58 +00004747 }
4748
Auke Kok9d5c8242008-01-24 02:22:38 -08004749 adapter->stats.tsctc += rd32(E1000_TSCTC);
4750 adapter->stats.tsctfc += rd32(E1000_TSCTFC);
4751
4752 adapter->stats.iac += rd32(E1000_IAC);
4753 adapter->stats.icrxoc += rd32(E1000_ICRXOC);
4754 adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
4755 adapter->stats.icrxatc += rd32(E1000_ICRXATC);
4756 adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
4757 adapter->stats.ictxatc += rd32(E1000_ICTXATC);
4758 adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
4759 adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
4760 adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
4761
4762 /* Fill out the OS statistics structure */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004763 net_stats->multicast = adapter->stats.mprc;
4764 net_stats->collisions = adapter->stats.colc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004765
4766 /* Rx Errors */
4767
4768 /* RLEC on some newer hardware can be incorrect so build
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00004769 * our own version based on RUC and ROC */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004770 net_stats->rx_errors = adapter->stats.rxerrc +
Auke Kok9d5c8242008-01-24 02:22:38 -08004771 adapter->stats.crcerrs + adapter->stats.algnerrc +
4772 adapter->stats.ruc + adapter->stats.roc +
4773 adapter->stats.cexterr;
Alexander Duyck128e45e2009-11-12 18:37:38 +00004774 net_stats->rx_length_errors = adapter->stats.ruc +
4775 adapter->stats.roc;
4776 net_stats->rx_crc_errors = adapter->stats.crcerrs;
4777 net_stats->rx_frame_errors = adapter->stats.algnerrc;
4778 net_stats->rx_missed_errors = adapter->stats.mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004779
4780 /* Tx Errors */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004781 net_stats->tx_errors = adapter->stats.ecol +
4782 adapter->stats.latecol;
4783 net_stats->tx_aborted_errors = adapter->stats.ecol;
4784 net_stats->tx_window_errors = adapter->stats.latecol;
4785 net_stats->tx_carrier_errors = adapter->stats.tncrs;
Auke Kok9d5c8242008-01-24 02:22:38 -08004786
4787 /* Tx Dropped needs to be maintained elsewhere */
4788
4789 /* Phy Stats */
4790 if (hw->phy.media_type == e1000_media_type_copper) {
4791 if ((adapter->link_speed == SPEED_1000) &&
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004792 (!igb_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004793 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
4794 adapter->phy_stats.idle_errors += phy_tmp;
4795 }
4796 }
4797
4798 /* Management Stats */
4799 adapter->stats.mgptc += rd32(E1000_MGTPTC);
4800 adapter->stats.mgprc += rd32(E1000_MGTPRC);
4801 adapter->stats.mgpdc += rd32(E1000_MGTPDC);
Carolyn Wyborny0a915b92011-02-26 07:42:37 +00004802
4803 /* OS2BMC Stats */
4804 reg = rd32(E1000_MANC);
4805 if (reg & E1000_MANC_EN_BMC2OS) {
4806 adapter->stats.o2bgptc += rd32(E1000_O2BGPTC);
4807 adapter->stats.o2bspc += rd32(E1000_O2BSPC);
4808 adapter->stats.b2ospc += rd32(E1000_B2OSPC);
4809 adapter->stats.b2ogprc += rd32(E1000_B2OGPRC);
4810 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004811}
4812
Auke Kok9d5c8242008-01-24 02:22:38 -08004813static irqreturn_t igb_msix_other(int irq, void *data)
4814{
Alexander Duyck047e0032009-10-27 15:49:27 +00004815 struct igb_adapter *adapter = data;
Auke Kok9d5c8242008-01-24 02:22:38 -08004816 struct e1000_hw *hw = &adapter->hw;
PJ Waskiewicz844290e2008-06-27 11:00:39 -07004817 u32 icr = rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07004818 /* reading ICR causes bit 31 of EICR to be cleared */
Alexander Duyckdda0e082009-02-06 23:19:08 +00004819
Alexander Duyck7f081d42010-01-07 17:41:00 +00004820 if (icr & E1000_ICR_DRSTA)
4821 schedule_work(&adapter->reset_task);
4822
Alexander Duyck047e0032009-10-27 15:49:27 +00004823 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00004824 /* HW is reporting DMA is out of sync */
4825 adapter->stats.doosync++;
Greg Rose13800462010-11-06 02:08:26 +00004826 /* The DMA Out of Sync is also indication of a spoof event
4827 * in IOV mode. Check the Wrong VM Behavior register to
4828 * see if it is really a spoof event. */
4829 igb_check_wvbr(adapter);
Alexander Duyckdda0e082009-02-06 23:19:08 +00004830 }
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00004831
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004832 /* Check for a mailbox event */
4833 if (icr & E1000_ICR_VMMB)
4834 igb_msg_task(adapter);
4835
4836 if (icr & E1000_ICR_LSC) {
4837 hw->mac.get_link_status = 1;
4838 /* guard against interrupt when we're going down */
4839 if (!test_bit(__IGB_DOWN, &adapter->state))
4840 mod_timer(&adapter->watchdog_timer, jiffies + 1);
4841 }
4842
Matthew Vick1f6e8172012-08-18 07:26:33 +00004843 if (icr & E1000_ICR_TS) {
4844 u32 tsicr = rd32(E1000_TSICR);
4845
4846 if (tsicr & E1000_TSICR_TXTS) {
4847 /* acknowledge the interrupt */
4848 wr32(E1000_TSICR, E1000_TSICR_TXTS);
4849 /* retrieve hardware timestamp */
4850 schedule_work(&adapter->ptp_tx_work);
4851 }
4852 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00004853
PJ Waskiewicz844290e2008-06-27 11:00:39 -07004854 wr32(E1000_EIMS, adapter->eims_other);
Auke Kok9d5c8242008-01-24 02:22:38 -08004855
4856 return IRQ_HANDLED;
4857}
4858
Alexander Duyck047e0032009-10-27 15:49:27 +00004859static void igb_write_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08004860{
Alexander Duyck26b39272010-02-17 01:00:41 +00004861 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck047e0032009-10-27 15:49:27 +00004862 u32 itr_val = q_vector->itr_val & 0x7FFC;
Auke Kok9d5c8242008-01-24 02:22:38 -08004863
Alexander Duyck047e0032009-10-27 15:49:27 +00004864 if (!q_vector->set_itr)
4865 return;
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004866
Alexander Duyck047e0032009-10-27 15:49:27 +00004867 if (!itr_val)
4868 itr_val = 0x4;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004869
Alexander Duyck26b39272010-02-17 01:00:41 +00004870 if (adapter->hw.mac.type == e1000_82575)
4871 itr_val |= itr_val << 16;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004872 else
Alexander Duyck0ba82992011-08-26 07:45:47 +00004873 itr_val |= E1000_EITR_CNT_IGNR;
Alexander Duyck047e0032009-10-27 15:49:27 +00004874
4875 writel(itr_val, q_vector->itr_register);
4876 q_vector->set_itr = 0;
4877}
4878
4879static irqreturn_t igb_msix_ring(int irq, void *data)
4880{
4881 struct igb_q_vector *q_vector = data;
4882
4883 /* Write the ITR value calculated from the previous interrupt. */
4884 igb_write_itr(q_vector);
4885
4886 napi_schedule(&q_vector->napi);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004887
Auke Kok9d5c8242008-01-24 02:22:38 -08004888 return IRQ_HANDLED;
4889}
4890
Jeff Kirsher421e02f2008-10-17 11:08:31 -07004891#ifdef CONFIG_IGB_DCA
Alexander Duyck6a050042012-09-25 00:31:27 +00004892static void igb_update_tx_dca(struct igb_adapter *adapter,
4893 struct igb_ring *tx_ring,
4894 int cpu)
4895{
4896 struct e1000_hw *hw = &adapter->hw;
4897 u32 txctrl = dca3_get_tag(tx_ring->dev, cpu);
4898
4899 if (hw->mac.type != e1000_82575)
4900 txctrl <<= E1000_DCA_TXCTRL_CPUID_SHIFT;
4901
4902 /*
4903 * We can enable relaxed ordering for reads, but not writes when
4904 * DCA is enabled. This is due to a known issue in some chipsets
4905 * which will cause the DCA tag to be cleared.
4906 */
4907 txctrl |= E1000_DCA_TXCTRL_DESC_RRO_EN |
4908 E1000_DCA_TXCTRL_DATA_RRO_EN |
4909 E1000_DCA_TXCTRL_DESC_DCA_EN;
4910
4911 wr32(E1000_DCA_TXCTRL(tx_ring->reg_idx), txctrl);
4912}
4913
4914static void igb_update_rx_dca(struct igb_adapter *adapter,
4915 struct igb_ring *rx_ring,
4916 int cpu)
4917{
4918 struct e1000_hw *hw = &adapter->hw;
4919 u32 rxctrl = dca3_get_tag(&adapter->pdev->dev, cpu);
4920
4921 if (hw->mac.type != e1000_82575)
4922 rxctrl <<= E1000_DCA_RXCTRL_CPUID_SHIFT;
4923
4924 /*
4925 * We can enable relaxed ordering for reads, but not writes when
4926 * DCA is enabled. This is due to a known issue in some chipsets
4927 * which will cause the DCA tag to be cleared.
4928 */
4929 rxctrl |= E1000_DCA_RXCTRL_DESC_RRO_EN |
4930 E1000_DCA_RXCTRL_DESC_DCA_EN;
4931
4932 wr32(E1000_DCA_RXCTRL(rx_ring->reg_idx), rxctrl);
4933}
4934
Alexander Duyck047e0032009-10-27 15:49:27 +00004935static void igb_update_dca(struct igb_q_vector *q_vector)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004936{
Alexander Duyck047e0032009-10-27 15:49:27 +00004937 struct igb_adapter *adapter = q_vector->adapter;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004938 int cpu = get_cpu();
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004939
Alexander Duyck047e0032009-10-27 15:49:27 +00004940 if (q_vector->cpu == cpu)
4941 goto out_no_update;
4942
Alexander Duyck6a050042012-09-25 00:31:27 +00004943 if (q_vector->tx.ring)
4944 igb_update_tx_dca(adapter, q_vector->tx.ring, cpu);
4945
4946 if (q_vector->rx.ring)
4947 igb_update_rx_dca(adapter, q_vector->rx.ring, cpu);
4948
Alexander Duyck047e0032009-10-27 15:49:27 +00004949 q_vector->cpu = cpu;
4950out_no_update:
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004951 put_cpu();
4952}
4953
4954static void igb_setup_dca(struct igb_adapter *adapter)
4955{
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00004956 struct e1000_hw *hw = &adapter->hw;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004957 int i;
4958
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004959 if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004960 return;
4961
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00004962 /* Always use CB2 mode, difference is masked in the CB driver. */
4963 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
4964
Alexander Duyck047e0032009-10-27 15:49:27 +00004965 for (i = 0; i < adapter->num_q_vectors; i++) {
Alexander Duyck26b39272010-02-17 01:00:41 +00004966 adapter->q_vector[i]->cpu = -1;
4967 igb_update_dca(adapter->q_vector[i]);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004968 }
4969}
4970
4971static int __igb_notify_dca(struct device *dev, void *data)
4972{
4973 struct net_device *netdev = dev_get_drvdata(dev);
4974 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck090b1792009-10-27 23:51:55 +00004975 struct pci_dev *pdev = adapter->pdev;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004976 struct e1000_hw *hw = &adapter->hw;
4977 unsigned long event = *(unsigned long *)data;
4978
4979 switch (event) {
4980 case DCA_PROVIDER_ADD:
4981 /* if already enabled, don't do it again */
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004982 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004983 break;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004984 if (dca_add_requester(dev) == 0) {
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08004985 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Alexander Duyck090b1792009-10-27 23:51:55 +00004986 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004987 igb_setup_dca(adapter);
4988 break;
4989 }
4990 /* Fall Through since DCA is disabled. */
4991 case DCA_PROVIDER_REMOVE:
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004992 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004993 /* without this a class_device is left
Alexander Duyck047e0032009-10-27 15:49:27 +00004994 * hanging around in the sysfs model */
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004995 dca_remove_requester(dev);
Alexander Duyck090b1792009-10-27 23:51:55 +00004996 dev_info(&pdev->dev, "DCA disabled\n");
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004997 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08004998 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004999 }
5000 break;
5001 }
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08005002
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005003 return 0;
5004}
5005
5006static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
5007 void *p)
5008{
5009 int ret_val;
5010
5011 ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
5012 __igb_notify_dca);
5013
5014 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
5015}
Jeff Kirsher421e02f2008-10-17 11:08:31 -07005016#endif /* CONFIG_IGB_DCA */
Auke Kok9d5c8242008-01-24 02:22:38 -08005017
Greg Rose0224d662011-10-14 02:57:14 +00005018#ifdef CONFIG_PCI_IOV
5019static int igb_vf_configure(struct igb_adapter *adapter, int vf)
5020{
5021 unsigned char mac_addr[ETH_ALEN];
Greg Rose0224d662011-10-14 02:57:14 +00005022
Joe Perches7efd26d2012-07-12 19:33:06 +00005023 eth_random_addr(mac_addr);
Greg Rose0224d662011-10-14 02:57:14 +00005024 igb_set_vf_mac(adapter, vf, mac_addr);
5025
Stefan Assmannf5571472012-08-18 04:06:11 +00005026 return 0;
Greg Rose0224d662011-10-14 02:57:14 +00005027}
5028
Stefan Assmannf5571472012-08-18 04:06:11 +00005029static bool igb_vfs_are_assigned(struct igb_adapter *adapter)
Greg Rose0224d662011-10-14 02:57:14 +00005030{
Greg Rose0224d662011-10-14 02:57:14 +00005031 struct pci_dev *pdev = adapter->pdev;
Stefan Assmannf5571472012-08-18 04:06:11 +00005032 struct pci_dev *vfdev;
5033 int dev_id;
Greg Rose0224d662011-10-14 02:57:14 +00005034
5035 switch (adapter->hw.mac.type) {
5036 case e1000_82576:
Stefan Assmannf5571472012-08-18 04:06:11 +00005037 dev_id = IGB_82576_VF_DEV_ID;
Greg Rose0224d662011-10-14 02:57:14 +00005038 break;
5039 case e1000_i350:
Stefan Assmannf5571472012-08-18 04:06:11 +00005040 dev_id = IGB_I350_VF_DEV_ID;
Greg Rose0224d662011-10-14 02:57:14 +00005041 break;
5042 default:
Stefan Assmannf5571472012-08-18 04:06:11 +00005043 return false;
Greg Rose0224d662011-10-14 02:57:14 +00005044 }
5045
Stefan Assmannf5571472012-08-18 04:06:11 +00005046 /* loop through all the VFs to see if we own any that are assigned */
5047 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
5048 while (vfdev) {
5049 /* if we don't own it we don't care */
5050 if (vfdev->is_virtfn && vfdev->physfn == pdev) {
5051 /* if it is assigned we cannot release it */
5052 if (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
Greg Rose0224d662011-10-14 02:57:14 +00005053 return true;
5054 }
Stefan Assmannf5571472012-08-18 04:06:11 +00005055
5056 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, vfdev);
Greg Rose0224d662011-10-14 02:57:14 +00005057 }
Stefan Assmannf5571472012-08-18 04:06:11 +00005058
Greg Rose0224d662011-10-14 02:57:14 +00005059 return false;
5060}
5061
5062#endif
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005063static void igb_ping_all_vfs(struct igb_adapter *adapter)
5064{
5065 struct e1000_hw *hw = &adapter->hw;
5066 u32 ping;
5067 int i;
5068
5069 for (i = 0 ; i < adapter->vfs_allocated_count; i++) {
5070 ping = E1000_PF_CONTROL_MSG;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005071 if (adapter->vf_data[i].flags & IGB_VF_FLAG_CTS)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005072 ping |= E1000_VT_MSGTYPE_CTS;
5073 igb_write_mbx(hw, &ping, 1, i);
5074 }
5075}
5076
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005077static int igb_set_vf_promisc(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5078{
5079 struct e1000_hw *hw = &adapter->hw;
5080 u32 vmolr = rd32(E1000_VMOLR(vf));
5081 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5082
Alexander Duyckd85b90042010-09-22 17:56:20 +00005083 vf_data->flags &= ~(IGB_VF_FLAG_UNI_PROMISC |
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005084 IGB_VF_FLAG_MULTI_PROMISC);
5085 vmolr &= ~(E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5086
5087 if (*msgbuf & E1000_VF_SET_PROMISC_MULTICAST) {
5088 vmolr |= E1000_VMOLR_MPME;
Alexander Duyckd85b90042010-09-22 17:56:20 +00005089 vf_data->flags |= IGB_VF_FLAG_MULTI_PROMISC;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005090 *msgbuf &= ~E1000_VF_SET_PROMISC_MULTICAST;
5091 } else {
5092 /*
5093 * if we have hashes and we are clearing a multicast promisc
5094 * flag we need to write the hashes to the MTA as this step
5095 * was previously skipped
5096 */
5097 if (vf_data->num_vf_mc_hashes > 30) {
5098 vmolr |= E1000_VMOLR_MPME;
5099 } else if (vf_data->num_vf_mc_hashes) {
5100 int j;
5101 vmolr |= E1000_VMOLR_ROMPE;
5102 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5103 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5104 }
5105 }
5106
5107 wr32(E1000_VMOLR(vf), vmolr);
5108
5109 /* there are flags left unprocessed, likely not supported */
5110 if (*msgbuf & E1000_VT_MSGINFO_MASK)
5111 return -EINVAL;
5112
5113 return 0;
5114
5115}
5116
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005117static int igb_set_vf_multicasts(struct igb_adapter *adapter,
5118 u32 *msgbuf, u32 vf)
5119{
5120 int n = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5121 u16 *hash_list = (u16 *)&msgbuf[1];
5122 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5123 int i;
5124
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005125 /* salt away the number of multicast addresses assigned
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005126 * to this VF for later use to restore when the PF multi cast
5127 * list changes
5128 */
5129 vf_data->num_vf_mc_hashes = n;
5130
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005131 /* only up to 30 hash values supported */
5132 if (n > 30)
5133 n = 30;
5134
5135 /* store the hashes for later use */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005136 for (i = 0; i < n; i++)
Joe Perchesa419aef2009-08-18 11:18:35 -07005137 vf_data->vf_mc_hashes[i] = hash_list[i];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005138
5139 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005140 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005141
5142 return 0;
5143}
5144
5145static void igb_restore_vf_multicasts(struct igb_adapter *adapter)
5146{
5147 struct e1000_hw *hw = &adapter->hw;
5148 struct vf_data_storage *vf_data;
5149 int i, j;
5150
5151 for (i = 0; i < adapter->vfs_allocated_count; i++) {
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005152 u32 vmolr = rd32(E1000_VMOLR(i));
5153 vmolr &= ~(E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5154
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005155 vf_data = &adapter->vf_data[i];
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005156
5157 if ((vf_data->num_vf_mc_hashes > 30) ||
5158 (vf_data->flags & IGB_VF_FLAG_MULTI_PROMISC)) {
5159 vmolr |= E1000_VMOLR_MPME;
5160 } else if (vf_data->num_vf_mc_hashes) {
5161 vmolr |= E1000_VMOLR_ROMPE;
5162 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5163 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5164 }
5165 wr32(E1000_VMOLR(i), vmolr);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005166 }
5167}
5168
5169static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf)
5170{
5171 struct e1000_hw *hw = &adapter->hw;
5172 u32 pool_mask, reg, vid;
5173 int i;
5174
5175 pool_mask = 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5176
5177 /* Find the vlan filter for this id */
5178 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5179 reg = rd32(E1000_VLVF(i));
5180
5181 /* remove the vf from the pool */
5182 reg &= ~pool_mask;
5183
5184 /* if pool is empty then remove entry from vfta */
5185 if (!(reg & E1000_VLVF_POOLSEL_MASK) &&
5186 (reg & E1000_VLVF_VLANID_ENABLE)) {
5187 reg = 0;
5188 vid = reg & E1000_VLVF_VLANID_MASK;
5189 igb_vfta_set(hw, vid, false);
5190 }
5191
5192 wr32(E1000_VLVF(i), reg);
5193 }
Alexander Duyckae641bd2009-09-03 14:49:33 +00005194
5195 adapter->vf_data[vf].vlans_enabled = 0;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005196}
5197
5198static s32 igb_vlvf_set(struct igb_adapter *adapter, u32 vid, bool add, u32 vf)
5199{
5200 struct e1000_hw *hw = &adapter->hw;
5201 u32 reg, i;
5202
Alexander Duyck51466232009-10-27 23:47:35 +00005203 /* The vlvf table only exists on 82576 hardware and newer */
5204 if (hw->mac.type < e1000_82576)
5205 return -1;
5206
5207 /* we only need to do this if VMDq is enabled */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005208 if (!adapter->vfs_allocated_count)
5209 return -1;
5210
5211 /* Find the vlan filter for this id */
5212 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5213 reg = rd32(E1000_VLVF(i));
5214 if ((reg & E1000_VLVF_VLANID_ENABLE) &&
5215 vid == (reg & E1000_VLVF_VLANID_MASK))
5216 break;
5217 }
5218
5219 if (add) {
5220 if (i == E1000_VLVF_ARRAY_SIZE) {
5221 /* Did not find a matching VLAN ID entry that was
5222 * enabled. Search for a free filter entry, i.e.
5223 * one without the enable bit set
5224 */
5225 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5226 reg = rd32(E1000_VLVF(i));
5227 if (!(reg & E1000_VLVF_VLANID_ENABLE))
5228 break;
5229 }
5230 }
5231 if (i < E1000_VLVF_ARRAY_SIZE) {
5232 /* Found an enabled/available entry */
5233 reg |= 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5234
5235 /* if !enabled we need to set this up in vfta */
5236 if (!(reg & E1000_VLVF_VLANID_ENABLE)) {
Alexander Duyck51466232009-10-27 23:47:35 +00005237 /* add VID to filter table */
5238 igb_vfta_set(hw, vid, true);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005239 reg |= E1000_VLVF_VLANID_ENABLE;
5240 }
Alexander Duyckcad6d052009-03-13 20:41:37 +00005241 reg &= ~E1000_VLVF_VLANID_MASK;
5242 reg |= vid;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005243 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00005244
5245 /* do not modify RLPML for PF devices */
5246 if (vf >= adapter->vfs_allocated_count)
5247 return 0;
5248
5249 if (!adapter->vf_data[vf].vlans_enabled) {
5250 u32 size;
5251 reg = rd32(E1000_VMOLR(vf));
5252 size = reg & E1000_VMOLR_RLPML_MASK;
5253 size += 4;
5254 reg &= ~E1000_VMOLR_RLPML_MASK;
5255 reg |= size;
5256 wr32(E1000_VMOLR(vf), reg);
5257 }
Alexander Duyckae641bd2009-09-03 14:49:33 +00005258
Alexander Duyck51466232009-10-27 23:47:35 +00005259 adapter->vf_data[vf].vlans_enabled++;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005260 }
5261 } else {
5262 if (i < E1000_VLVF_ARRAY_SIZE) {
5263 /* remove vf from the pool */
5264 reg &= ~(1 << (E1000_VLVF_POOLSEL_SHIFT + vf));
5265 /* if pool is empty then remove entry from vfta */
5266 if (!(reg & E1000_VLVF_POOLSEL_MASK)) {
5267 reg = 0;
5268 igb_vfta_set(hw, vid, false);
5269 }
5270 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00005271
5272 /* do not modify RLPML for PF devices */
5273 if (vf >= adapter->vfs_allocated_count)
5274 return 0;
5275
5276 adapter->vf_data[vf].vlans_enabled--;
5277 if (!adapter->vf_data[vf].vlans_enabled) {
5278 u32 size;
5279 reg = rd32(E1000_VMOLR(vf));
5280 size = reg & E1000_VMOLR_RLPML_MASK;
5281 size -= 4;
5282 reg &= ~E1000_VMOLR_RLPML_MASK;
5283 reg |= size;
5284 wr32(E1000_VMOLR(vf), reg);
5285 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005286 }
5287 }
Williams, Mitch A8151d292010-02-10 01:44:24 +00005288 return 0;
5289}
5290
5291static void igb_set_vmvir(struct igb_adapter *adapter, u32 vid, u32 vf)
5292{
5293 struct e1000_hw *hw = &adapter->hw;
5294
5295 if (vid)
5296 wr32(E1000_VMVIR(vf), (vid | E1000_VMVIR_VLANA_DEFAULT));
5297 else
5298 wr32(E1000_VMVIR(vf), 0);
5299}
5300
5301static int igb_ndo_set_vf_vlan(struct net_device *netdev,
5302 int vf, u16 vlan, u8 qos)
5303{
5304 int err = 0;
5305 struct igb_adapter *adapter = netdev_priv(netdev);
5306
5307 if ((vf >= adapter->vfs_allocated_count) || (vlan > 4095) || (qos > 7))
5308 return -EINVAL;
5309 if (vlan || qos) {
5310 err = igb_vlvf_set(adapter, vlan, !!vlan, vf);
5311 if (err)
5312 goto out;
5313 igb_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
5314 igb_set_vmolr(adapter, vf, !vlan);
5315 adapter->vf_data[vf].pf_vlan = vlan;
5316 adapter->vf_data[vf].pf_qos = qos;
5317 dev_info(&adapter->pdev->dev,
5318 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
5319 if (test_bit(__IGB_DOWN, &adapter->state)) {
5320 dev_warn(&adapter->pdev->dev,
5321 "The VF VLAN has been set,"
5322 " but the PF device is not up.\n");
5323 dev_warn(&adapter->pdev->dev,
5324 "Bring the PF device up before"
5325 " attempting to use the VF device.\n");
5326 }
5327 } else {
5328 igb_vlvf_set(adapter, adapter->vf_data[vf].pf_vlan,
5329 false, vf);
5330 igb_set_vmvir(adapter, vlan, vf);
5331 igb_set_vmolr(adapter, vf, true);
5332 adapter->vf_data[vf].pf_vlan = 0;
5333 adapter->vf_data[vf].pf_qos = 0;
5334 }
5335out:
5336 return err;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005337}
5338
5339static int igb_set_vf_vlan(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5340{
5341 int add = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5342 int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
5343
5344 return igb_vlvf_set(adapter, vid, add, vf);
5345}
5346
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005347static inline void igb_vf_reset(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005348{
Greg Rose8fa7e0f2010-11-06 05:43:21 +00005349 /* clear flags - except flag that indicates PF has set the MAC */
5350 adapter->vf_data[vf].flags &= IGB_VF_FLAG_PF_SET_MAC;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005351 adapter->vf_data[vf].last_nack = jiffies;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005352
5353 /* reset offloads to defaults */
Williams, Mitch A8151d292010-02-10 01:44:24 +00005354 igb_set_vmolr(adapter, vf, true);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005355
5356 /* reset vlans for device */
5357 igb_clear_vf_vfta(adapter, vf);
Williams, Mitch A8151d292010-02-10 01:44:24 +00005358 if (adapter->vf_data[vf].pf_vlan)
5359 igb_ndo_set_vf_vlan(adapter->netdev, vf,
5360 adapter->vf_data[vf].pf_vlan,
5361 adapter->vf_data[vf].pf_qos);
5362 else
5363 igb_clear_vf_vfta(adapter, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005364
5365 /* reset multicast table array for vf */
5366 adapter->vf_data[vf].num_vf_mc_hashes = 0;
5367
5368 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005369 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005370}
5371
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005372static void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf)
5373{
5374 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
5375
5376 /* generate a new mac address as we were hotplug removed/added */
Williams, Mitch A8151d292010-02-10 01:44:24 +00005377 if (!(adapter->vf_data[vf].flags & IGB_VF_FLAG_PF_SET_MAC))
Joe Perches7efd26d2012-07-12 19:33:06 +00005378 eth_random_addr(vf_mac);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005379
5380 /* process remaining reset events */
5381 igb_vf_reset(adapter, vf);
5382}
5383
5384static void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005385{
5386 struct e1000_hw *hw = &adapter->hw;
5387 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005388 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005389 u32 reg, msgbuf[3];
5390 u8 *addr = (u8 *)(&msgbuf[1]);
5391
5392 /* process all the same items cleared in a function level reset */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005393 igb_vf_reset(adapter, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005394
5395 /* set vf mac address */
Alexander Duyck26ad9172009-10-05 06:32:49 +00005396 igb_rar_set_qsel(adapter, vf_mac, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005397
5398 /* enable transmit and receive for vf */
5399 reg = rd32(E1000_VFTE);
5400 wr32(E1000_VFTE, reg | (1 << vf));
5401 reg = rd32(E1000_VFRE);
5402 wr32(E1000_VFRE, reg | (1 << vf));
5403
Greg Rose8fa7e0f2010-11-06 05:43:21 +00005404 adapter->vf_data[vf].flags |= IGB_VF_FLAG_CTS;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005405
5406 /* reply to reset with ack and vf mac address */
5407 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
5408 memcpy(addr, vf_mac, 6);
5409 igb_write_mbx(hw, msgbuf, 3, vf);
5410}
5411
5412static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf)
5413{
Greg Rosede42edd2010-07-01 13:39:23 +00005414 /*
5415 * The VF MAC Address is stored in a packed array of bytes
5416 * starting at the second 32 bit word of the msg array
5417 */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005418 unsigned char *addr = (char *)&msg[1];
5419 int err = -1;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005420
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005421 if (is_valid_ether_addr(addr))
5422 err = igb_set_vf_mac(adapter, vf, addr);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005423
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005424 return err;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005425}
5426
5427static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf)
5428{
5429 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005430 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005431 u32 msg = E1000_VT_MSGTYPE_NACK;
5432
5433 /* if device isn't clear to send it shouldn't be reading either */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005434 if (!(vf_data->flags & IGB_VF_FLAG_CTS) &&
5435 time_after(jiffies, vf_data->last_nack + (2 * HZ))) {
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005436 igb_write_mbx(hw, &msg, 1, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005437 vf_data->last_nack = jiffies;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005438 }
5439}
5440
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005441static void igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005442{
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005443 struct pci_dev *pdev = adapter->pdev;
5444 u32 msgbuf[E1000_VFMAILBOX_SIZE];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005445 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005446 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005447 s32 retval;
5448
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005449 retval = igb_read_mbx(hw, msgbuf, E1000_VFMAILBOX_SIZE, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005450
Alexander Duyckfef45f42009-12-11 22:57:34 -08005451 if (retval) {
5452 /* if receive failed revoke VF CTS stats and restart init */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005453 dev_err(&pdev->dev, "Error receiving message from VF\n");
Alexander Duyckfef45f42009-12-11 22:57:34 -08005454 vf_data->flags &= ~IGB_VF_FLAG_CTS;
5455 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5456 return;
5457 goto out;
5458 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005459
5460 /* this is a message we already processed, do nothing */
5461 if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005462 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005463
5464 /*
5465 * until the vf completes a reset it should not be
5466 * allowed to start any configuration.
5467 */
5468
5469 if (msgbuf[0] == E1000_VF_RESET) {
5470 igb_vf_reset_msg(adapter, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005471 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005472 }
5473
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005474 if (!(vf_data->flags & IGB_VF_FLAG_CTS)) {
Alexander Duyckfef45f42009-12-11 22:57:34 -08005475 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5476 return;
5477 retval = -1;
5478 goto out;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005479 }
5480
5481 switch ((msgbuf[0] & 0xFFFF)) {
5482 case E1000_VF_SET_MAC_ADDR:
Greg Rosea6b5ea32010-11-06 05:42:59 +00005483 retval = -EINVAL;
5484 if (!(vf_data->flags & IGB_VF_FLAG_PF_SET_MAC))
5485 retval = igb_set_vf_mac_addr(adapter, msgbuf, vf);
5486 else
5487 dev_warn(&pdev->dev,
5488 "VF %d attempted to override administratively "
5489 "set MAC address\nReload the VF driver to "
5490 "resume operations\n", vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005491 break;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005492 case E1000_VF_SET_PROMISC:
5493 retval = igb_set_vf_promisc(adapter, msgbuf, vf);
5494 break;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005495 case E1000_VF_SET_MULTICAST:
5496 retval = igb_set_vf_multicasts(adapter, msgbuf, vf);
5497 break;
5498 case E1000_VF_SET_LPE:
5499 retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf);
5500 break;
5501 case E1000_VF_SET_VLAN:
Greg Rosea6b5ea32010-11-06 05:42:59 +00005502 retval = -1;
5503 if (vf_data->pf_vlan)
5504 dev_warn(&pdev->dev,
5505 "VF %d attempted to override administratively "
5506 "set VLAN tag\nReload the VF driver to "
5507 "resume operations\n", vf);
Williams, Mitch A8151d292010-02-10 01:44:24 +00005508 else
5509 retval = igb_set_vf_vlan(adapter, msgbuf, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005510 break;
5511 default:
Alexander Duyck090b1792009-10-27 23:51:55 +00005512 dev_err(&pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005513 retval = -1;
5514 break;
5515 }
5516
Alexander Duyckfef45f42009-12-11 22:57:34 -08005517 msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
5518out:
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005519 /* notify the VF of the results of what it sent us */
5520 if (retval)
5521 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
5522 else
5523 msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
5524
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005525 igb_write_mbx(hw, msgbuf, 1, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005526}
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005527
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005528static void igb_msg_task(struct igb_adapter *adapter)
5529{
5530 struct e1000_hw *hw = &adapter->hw;
5531 u32 vf;
5532
5533 for (vf = 0; vf < adapter->vfs_allocated_count; vf++) {
5534 /* process any reset requests */
5535 if (!igb_check_for_rst(hw, vf))
5536 igb_vf_reset_event(adapter, vf);
5537
5538 /* process any messages pending */
5539 if (!igb_check_for_msg(hw, vf))
5540 igb_rcv_msg_from_vf(adapter, vf);
5541
5542 /* process any acks */
5543 if (!igb_check_for_ack(hw, vf))
5544 igb_rcv_ack_from_vf(adapter, vf);
5545 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005546}
5547
Auke Kok9d5c8242008-01-24 02:22:38 -08005548/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00005549 * igb_set_uta - Set unicast filter table address
5550 * @adapter: board private structure
5551 *
5552 * The unicast table address is a register array of 32-bit registers.
5553 * The table is meant to be used in a way similar to how the MTA is used
5554 * however due to certain limitations in the hardware it is necessary to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005555 * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscuous
5556 * enable bit to allow vlan tag stripping when promiscuous mode is enabled
Alexander Duyck68d480c2009-10-05 06:33:08 +00005557 **/
5558static void igb_set_uta(struct igb_adapter *adapter)
5559{
5560 struct e1000_hw *hw = &adapter->hw;
5561 int i;
5562
5563 /* The UTA table only exists on 82576 hardware and newer */
5564 if (hw->mac.type < e1000_82576)
5565 return;
5566
5567 /* we only need to do this if VMDq is enabled */
5568 if (!adapter->vfs_allocated_count)
5569 return;
5570
5571 for (i = 0; i < hw->mac.uta_reg_count; i++)
5572 array_wr32(E1000_UTA, i, ~0);
5573}
5574
5575/**
Auke Kok9d5c8242008-01-24 02:22:38 -08005576 * igb_intr_msi - Interrupt Handler
5577 * @irq: interrupt number
5578 * @data: pointer to a network interface device structure
5579 **/
5580static irqreturn_t igb_intr_msi(int irq, void *data)
5581{
Alexander Duyck047e0032009-10-27 15:49:27 +00005582 struct igb_adapter *adapter = data;
5583 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08005584 struct e1000_hw *hw = &adapter->hw;
5585 /* read ICR disables interrupts using IAM */
5586 u32 icr = rd32(E1000_ICR);
5587
Alexander Duyck047e0032009-10-27 15:49:27 +00005588 igb_write_itr(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08005589
Alexander Duyck7f081d42010-01-07 17:41:00 +00005590 if (icr & E1000_ICR_DRSTA)
5591 schedule_work(&adapter->reset_task);
5592
Alexander Duyck047e0032009-10-27 15:49:27 +00005593 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00005594 /* HW is reporting DMA is out of sync */
5595 adapter->stats.doosync++;
5596 }
5597
Auke Kok9d5c8242008-01-24 02:22:38 -08005598 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5599 hw->mac.get_link_status = 1;
5600 if (!test_bit(__IGB_DOWN, &adapter->state))
5601 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5602 }
5603
Matthew Vick1f6e8172012-08-18 07:26:33 +00005604 if (icr & E1000_ICR_TS) {
5605 u32 tsicr = rd32(E1000_TSICR);
5606
5607 if (tsicr & E1000_TSICR_TXTS) {
5608 /* acknowledge the interrupt */
5609 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5610 /* retrieve hardware timestamp */
5611 schedule_work(&adapter->ptp_tx_work);
5612 }
5613 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00005614
Alexander Duyck047e0032009-10-27 15:49:27 +00005615 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08005616
5617 return IRQ_HANDLED;
5618}
5619
5620/**
Alexander Duyck4a3c6432009-02-06 23:20:49 +00005621 * igb_intr - Legacy Interrupt Handler
Auke Kok9d5c8242008-01-24 02:22:38 -08005622 * @irq: interrupt number
5623 * @data: pointer to a network interface device structure
5624 **/
5625static irqreturn_t igb_intr(int irq, void *data)
5626{
Alexander Duyck047e0032009-10-27 15:49:27 +00005627 struct igb_adapter *adapter = data;
5628 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08005629 struct e1000_hw *hw = &adapter->hw;
5630 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
5631 * need for the IMC write */
5632 u32 icr = rd32(E1000_ICR);
Auke Kok9d5c8242008-01-24 02:22:38 -08005633
5634 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
5635 * not set, then the adapter didn't send an interrupt */
5636 if (!(icr & E1000_ICR_INT_ASSERTED))
5637 return IRQ_NONE;
5638
Alexander Duyck0ba82992011-08-26 07:45:47 +00005639 igb_write_itr(q_vector);
5640
Alexander Duyck7f081d42010-01-07 17:41:00 +00005641 if (icr & E1000_ICR_DRSTA)
5642 schedule_work(&adapter->reset_task);
5643
Alexander Duyck047e0032009-10-27 15:49:27 +00005644 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00005645 /* HW is reporting DMA is out of sync */
5646 adapter->stats.doosync++;
5647 }
5648
Auke Kok9d5c8242008-01-24 02:22:38 -08005649 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5650 hw->mac.get_link_status = 1;
5651 /* guard against interrupt when we're going down */
5652 if (!test_bit(__IGB_DOWN, &adapter->state))
5653 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5654 }
5655
Matthew Vick1f6e8172012-08-18 07:26:33 +00005656 if (icr & E1000_ICR_TS) {
5657 u32 tsicr = rd32(E1000_TSICR);
5658
5659 if (tsicr & E1000_TSICR_TXTS) {
5660 /* acknowledge the interrupt */
5661 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5662 /* retrieve hardware timestamp */
5663 schedule_work(&adapter->ptp_tx_work);
5664 }
5665 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00005666
Alexander Duyck047e0032009-10-27 15:49:27 +00005667 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08005668
5669 return IRQ_HANDLED;
5670}
5671
Stephen Hemmingerc50b52a2012-01-18 22:13:26 +00005672static void igb_ring_irq_enable(struct igb_q_vector *q_vector)
Alexander Duyck46544252009-02-19 20:39:04 -08005673{
Alexander Duyck047e0032009-10-27 15:49:27 +00005674 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck46544252009-02-19 20:39:04 -08005675 struct e1000_hw *hw = &adapter->hw;
5676
Alexander Duyck0ba82992011-08-26 07:45:47 +00005677 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
5678 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
5679 if ((adapter->num_q_vectors == 1) && !adapter->vf_data)
5680 igb_set_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005681 else
Alexander Duyck047e0032009-10-27 15:49:27 +00005682 igb_update_ring_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005683 }
5684
5685 if (!test_bit(__IGB_DOWN, &adapter->state)) {
5686 if (adapter->msix_entries)
Alexander Duyck047e0032009-10-27 15:49:27 +00005687 wr32(E1000_EIMS, q_vector->eims_value);
Alexander Duyck46544252009-02-19 20:39:04 -08005688 else
5689 igb_irq_enable(adapter);
5690 }
5691}
5692
Auke Kok9d5c8242008-01-24 02:22:38 -08005693/**
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005694 * igb_poll - NAPI Rx polling callback
5695 * @napi: napi polling structure
5696 * @budget: count of how many packets we should handle
Auke Kok9d5c8242008-01-24 02:22:38 -08005697 **/
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005698static int igb_poll(struct napi_struct *napi, int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08005699{
Alexander Duyck047e0032009-10-27 15:49:27 +00005700 struct igb_q_vector *q_vector = container_of(napi,
5701 struct igb_q_vector,
5702 napi);
Alexander Duyck16eb8812011-08-26 07:43:54 +00005703 bool clean_complete = true;
Auke Kok9d5c8242008-01-24 02:22:38 -08005704
Jeff Kirsher421e02f2008-10-17 11:08:31 -07005705#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +00005706 if (q_vector->adapter->flags & IGB_FLAG_DCA_ENABLED)
5707 igb_update_dca(q_vector);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005708#endif
Alexander Duyck0ba82992011-08-26 07:45:47 +00005709 if (q_vector->tx.ring)
Alexander Duyck13fde972011-10-05 13:35:24 +00005710 clean_complete = igb_clean_tx_irq(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08005711
Alexander Duyck0ba82992011-08-26 07:45:47 +00005712 if (q_vector->rx.ring)
Alexander Duyckcd392f52011-08-26 07:43:59 +00005713 clean_complete &= igb_clean_rx_irq(q_vector, budget);
Alexander Duyck047e0032009-10-27 15:49:27 +00005714
Alexander Duyck16eb8812011-08-26 07:43:54 +00005715 /* If all work not completed, return budget and keep polling */
5716 if (!clean_complete)
5717 return budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08005718
Alexander Duyck46544252009-02-19 20:39:04 -08005719 /* If not enough Rx work done, exit the polling mode */
Alexander Duyck16eb8812011-08-26 07:43:54 +00005720 napi_complete(napi);
5721 igb_ring_irq_enable(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005722
Alexander Duyck16eb8812011-08-26 07:43:54 +00005723 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08005724}
Al Viro6d8126f2008-03-16 22:23:24 +00005725
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005726/**
Auke Kok9d5c8242008-01-24 02:22:38 -08005727 * igb_clean_tx_irq - Reclaim resources after transmit completes
Alexander Duyck047e0032009-10-27 15:49:27 +00005728 * @q_vector: pointer to q_vector containing needed info
Ben Hutchings49ce9c22012-07-10 10:56:00 +00005729 *
Auke Kok9d5c8242008-01-24 02:22:38 -08005730 * returns true if ring is completely cleaned
5731 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00005732static bool igb_clean_tx_irq(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08005733{
Alexander Duyck047e0032009-10-27 15:49:27 +00005734 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck0ba82992011-08-26 07:45:47 +00005735 struct igb_ring *tx_ring = q_vector->tx.ring;
Alexander Duyck06034642011-08-26 07:44:22 +00005736 struct igb_tx_buffer *tx_buffer;
Alexander Duyckf4128782012-09-13 06:28:01 +00005737 union e1000_adv_tx_desc *tx_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08005738 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck0ba82992011-08-26 07:45:47 +00005739 unsigned int budget = q_vector->tx.work_limit;
Alexander Duyck8542db02011-08-26 07:44:43 +00005740 unsigned int i = tx_ring->next_to_clean;
Auke Kok9d5c8242008-01-24 02:22:38 -08005741
Alexander Duyck13fde972011-10-05 13:35:24 +00005742 if (test_bit(__IGB_DOWN, &adapter->state))
5743 return true;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08005744
Alexander Duyck06034642011-08-26 07:44:22 +00005745 tx_buffer = &tx_ring->tx_buffer_info[i];
Alexander Duyck13fde972011-10-05 13:35:24 +00005746 tx_desc = IGB_TX_DESC(tx_ring, i);
Alexander Duyck8542db02011-08-26 07:44:43 +00005747 i -= tx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08005748
Alexander Duyckf4128782012-09-13 06:28:01 +00005749 do {
5750 union e1000_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
Alexander Duyck8542db02011-08-26 07:44:43 +00005751
5752 /* if next_to_watch is not set then there is no work pending */
5753 if (!eop_desc)
5754 break;
Alexander Duyck13fde972011-10-05 13:35:24 +00005755
Alexander Duyckf4128782012-09-13 06:28:01 +00005756 /* prevent any other reads prior to eop_desc */
5757 rmb();
5758
Alexander Duyck13fde972011-10-05 13:35:24 +00005759 /* if DD is not set pending work has not been completed */
5760 if (!(eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)))
5761 break;
5762
Alexander Duyck8542db02011-08-26 07:44:43 +00005763 /* clear next_to_watch to prevent false hangs */
5764 tx_buffer->next_to_watch = NULL;
Alexander Duyck13fde972011-10-05 13:35:24 +00005765
Alexander Duyckebe42d12011-08-26 07:45:09 +00005766 /* update the statistics for this packet */
5767 total_bytes += tx_buffer->bytecount;
5768 total_packets += tx_buffer->gso_segs;
Alexander Duyck13fde972011-10-05 13:35:24 +00005769
Alexander Duyckebe42d12011-08-26 07:45:09 +00005770 /* free the skb */
5771 dev_kfree_skb_any(tx_buffer->skb);
Alexander Duyckebe42d12011-08-26 07:45:09 +00005772
5773 /* unmap skb header data */
5774 dma_unmap_single(tx_ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005775 dma_unmap_addr(tx_buffer, dma),
5776 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00005777 DMA_TO_DEVICE);
5778
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005779 /* clear tx_buffer data */
5780 tx_buffer->skb = NULL;
5781 dma_unmap_len_set(tx_buffer, len, 0);
5782
Alexander Duyckebe42d12011-08-26 07:45:09 +00005783 /* clear last DMA location and unmap remaining buffers */
5784 while (tx_desc != eop_desc) {
Alexander Duyck13fde972011-10-05 13:35:24 +00005785 tx_buffer++;
5786 tx_desc++;
Auke Kok9d5c8242008-01-24 02:22:38 -08005787 i++;
Alexander Duyck8542db02011-08-26 07:44:43 +00005788 if (unlikely(!i)) {
5789 i -= tx_ring->count;
Alexander Duyck06034642011-08-26 07:44:22 +00005790 tx_buffer = tx_ring->tx_buffer_info;
Alexander Duyck13fde972011-10-05 13:35:24 +00005791 tx_desc = IGB_TX_DESC(tx_ring, 0);
5792 }
Alexander Duyckebe42d12011-08-26 07:45:09 +00005793
5794 /* unmap any remaining paged data */
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005795 if (dma_unmap_len(tx_buffer, len)) {
Alexander Duyckebe42d12011-08-26 07:45:09 +00005796 dma_unmap_page(tx_ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005797 dma_unmap_addr(tx_buffer, dma),
5798 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00005799 DMA_TO_DEVICE);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005800 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duyckebe42d12011-08-26 07:45:09 +00005801 }
5802 }
5803
Alexander Duyckebe42d12011-08-26 07:45:09 +00005804 /* move us one more past the eop_desc for start of next pkt */
5805 tx_buffer++;
5806 tx_desc++;
5807 i++;
5808 if (unlikely(!i)) {
5809 i -= tx_ring->count;
5810 tx_buffer = tx_ring->tx_buffer_info;
5811 tx_desc = IGB_TX_DESC(tx_ring, 0);
5812 }
Alexander Duyckf4128782012-09-13 06:28:01 +00005813
5814 /* issue prefetch for next Tx descriptor */
5815 prefetch(tx_desc);
5816
5817 /* update budget accounting */
5818 budget--;
5819 } while (likely(budget));
Alexander Duyck0e014cb2008-12-26 01:33:18 -08005820
Eric Dumazetbdbc0632012-01-04 20:23:36 +00005821 netdev_tx_completed_queue(txring_txq(tx_ring),
5822 total_packets, total_bytes);
Alexander Duyck8542db02011-08-26 07:44:43 +00005823 i += tx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08005824 tx_ring->next_to_clean = i;
Alexander Duyck13fde972011-10-05 13:35:24 +00005825 u64_stats_update_begin(&tx_ring->tx_syncp);
5826 tx_ring->tx_stats.bytes += total_bytes;
5827 tx_ring->tx_stats.packets += total_packets;
5828 u64_stats_update_end(&tx_ring->tx_syncp);
Alexander Duyck0ba82992011-08-26 07:45:47 +00005829 q_vector->tx.total_bytes += total_bytes;
5830 q_vector->tx.total_packets += total_packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08005831
Alexander Duyck6d095fa2011-08-26 07:46:19 +00005832 if (test_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
Alexander Duyck13fde972011-10-05 13:35:24 +00005833 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck13fde972011-10-05 13:35:24 +00005834
Auke Kok9d5c8242008-01-24 02:22:38 -08005835 /* Detect a transmit hang in hardware, this serializes the
5836 * check with the clearing of time_stamp and movement of i */
Alexander Duyck6d095fa2011-08-26 07:46:19 +00005837 clear_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
Alexander Duyckf4128782012-09-13 06:28:01 +00005838 if (tx_buffer->next_to_watch &&
Alexander Duyck8542db02011-08-26 07:44:43 +00005839 time_after(jiffies, tx_buffer->time_stamp +
Joe Perches8e95a202009-12-03 07:58:21 +00005840 (adapter->tx_timeout_factor * HZ)) &&
5841 !(rd32(E1000_STATUS) & E1000_STATUS_TXOFF)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08005842
Auke Kok9d5c8242008-01-24 02:22:38 -08005843 /* detected Tx unit hang */
Alexander Duyck59d71982010-04-27 13:09:25 +00005844 dev_err(tx_ring->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08005845 "Detected Tx Unit Hang\n"
Alexander Duyck2d064c02008-07-08 15:10:12 -07005846 " Tx Queue <%d>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08005847 " TDH <%x>\n"
5848 " TDT <%x>\n"
5849 " next_to_use <%x>\n"
5850 " next_to_clean <%x>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08005851 "buffer_info[next_to_clean]\n"
5852 " time_stamp <%lx>\n"
Alexander Duyck8542db02011-08-26 07:44:43 +00005853 " next_to_watch <%p>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08005854 " jiffies <%lx>\n"
5855 " desc.status <%x>\n",
Alexander Duyck2d064c02008-07-08 15:10:12 -07005856 tx_ring->queue_index,
Alexander Duyck238ac812011-08-26 07:43:48 +00005857 rd32(E1000_TDH(tx_ring->reg_idx)),
Alexander Duyckfce99e32009-10-27 15:51:27 +00005858 readl(tx_ring->tail),
Auke Kok9d5c8242008-01-24 02:22:38 -08005859 tx_ring->next_to_use,
5860 tx_ring->next_to_clean,
Alexander Duyck8542db02011-08-26 07:44:43 +00005861 tx_buffer->time_stamp,
Alexander Duyckf4128782012-09-13 06:28:01 +00005862 tx_buffer->next_to_watch,
Auke Kok9d5c8242008-01-24 02:22:38 -08005863 jiffies,
Alexander Duyckf4128782012-09-13 06:28:01 +00005864 tx_buffer->next_to_watch->wb.status);
Alexander Duyck13fde972011-10-05 13:35:24 +00005865 netif_stop_subqueue(tx_ring->netdev,
5866 tx_ring->queue_index);
5867
5868 /* we are about to reset, no point in enabling stuff */
5869 return true;
Auke Kok9d5c8242008-01-24 02:22:38 -08005870 }
5871 }
Alexander Duyck13fde972011-10-05 13:35:24 +00005872
5873 if (unlikely(total_packets &&
5874 netif_carrier_ok(tx_ring->netdev) &&
5875 igb_desc_unused(tx_ring) >= IGB_TX_QUEUE_WAKE)) {
5876 /* Make sure that anybody stopping the queue after this
5877 * sees the new next_to_clean.
5878 */
5879 smp_mb();
5880 if (__netif_subqueue_stopped(tx_ring->netdev,
5881 tx_ring->queue_index) &&
5882 !(test_bit(__IGB_DOWN, &adapter->state))) {
5883 netif_wake_subqueue(tx_ring->netdev,
5884 tx_ring->queue_index);
5885
5886 u64_stats_update_begin(&tx_ring->tx_syncp);
5887 tx_ring->tx_stats.restart_queue++;
5888 u64_stats_update_end(&tx_ring->tx_syncp);
5889 }
5890 }
5891
5892 return !!budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08005893}
5894
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005895/**
5896 * igb_reuse_rx_page - page flip buffer and store it back on the ring
5897 * @rx_ring: rx descriptor ring to store buffers on
5898 * @old_buff: donor buffer to have page reused
5899 *
5900 * Synchronizes page for reuse by the adapter
5901 **/
5902static void igb_reuse_rx_page(struct igb_ring *rx_ring,
5903 struct igb_rx_buffer *old_buff)
5904{
5905 struct igb_rx_buffer *new_buff;
5906 u16 nta = rx_ring->next_to_alloc;
5907
5908 new_buff = &rx_ring->rx_buffer_info[nta];
5909
5910 /* update, and store next to alloc */
5911 nta++;
5912 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
5913
5914 /* transfer page from old buffer to new buffer */
5915 memcpy(new_buff, old_buff, sizeof(struct igb_rx_buffer));
5916
5917 /* sync the buffer for use by the device */
5918 dma_sync_single_range_for_device(rx_ring->dev, old_buff->dma,
5919 old_buff->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005920 IGB_RX_BUFSZ,
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005921 DMA_FROM_DEVICE);
5922}
5923
5924/**
5925 * igb_add_rx_frag - Add contents of Rx buffer to sk_buff
5926 * @rx_ring: rx descriptor ring to transact packets on
5927 * @rx_buffer: buffer containing page to add
5928 * @rx_desc: descriptor containing length of buffer written by hardware
5929 * @skb: sk_buff to place the data into
5930 *
5931 * This function will add the data contained in rx_buffer->page to the skb.
5932 * This is done either through a direct copy if the data in the buffer is
5933 * less than the skb header size, otherwise it will just attach the page as
5934 * a frag to the skb.
5935 *
5936 * The function will then update the page offset if necessary and return
5937 * true if the buffer can be reused by the adapter.
5938 **/
5939static bool igb_add_rx_frag(struct igb_ring *rx_ring,
5940 struct igb_rx_buffer *rx_buffer,
5941 union e1000_adv_rx_desc *rx_desc,
5942 struct sk_buff *skb)
5943{
5944 struct page *page = rx_buffer->page;
5945 unsigned int size = le16_to_cpu(rx_desc->wb.upper.length);
5946
5947 if ((size <= IGB_RX_HDR_LEN) && !skb_is_nonlinear(skb)) {
5948 unsigned char *va = page_address(page) + rx_buffer->page_offset;
5949
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005950 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
5951 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
5952 va += IGB_TS_HDR_LEN;
5953 size -= IGB_TS_HDR_LEN;
5954 }
5955
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005956 memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
5957
5958 /* we can reuse buffer as-is, just make sure it is local */
5959 if (likely(page_to_nid(page) == numa_node_id()))
5960 return true;
5961
5962 /* this page cannot be reused so discard it */
5963 put_page(page);
5964 return false;
5965 }
5966
5967 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005968 rx_buffer->page_offset, size, IGB_RX_BUFSZ);
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005969
5970 /* avoid re-using remote pages */
5971 if (unlikely(page_to_nid(page) != numa_node_id()))
5972 return false;
5973
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005974#if (PAGE_SIZE < 8192)
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005975 /* if we are only owner of page we can reuse it */
5976 if (unlikely(page_count(page) != 1))
5977 return false;
5978
5979 /* flip page offset to other buffer */
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005980 rx_buffer->page_offset ^= IGB_RX_BUFSZ;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005981
5982 /*
5983 * since we are the only owner of the page and we need to
5984 * increment it, just set the value to 2 in order to avoid
5985 * an unnecessary locked operation
5986 */
5987 atomic_set(&page->_count, 2);
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005988#else
5989 /* move offset up to the next cache line */
5990 rx_buffer->page_offset += SKB_DATA_ALIGN(size);
5991
5992 if (rx_buffer->page_offset > (PAGE_SIZE - IGB_RX_BUFSZ))
5993 return false;
5994
5995 /* bump ref count on page before it is given to the stack */
5996 get_page(page);
5997#endif
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005998
5999 return true;
6000}
6001
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006002static struct sk_buff *igb_fetch_rx_buffer(struct igb_ring *rx_ring,
6003 union e1000_adv_rx_desc *rx_desc,
6004 struct sk_buff *skb)
6005{
6006 struct igb_rx_buffer *rx_buffer;
6007 struct page *page;
6008
6009 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
6010
6011 /*
6012 * This memory barrier is needed to keep us from reading
6013 * any other fields out of the rx_desc until we know the
6014 * RXD_STAT_DD bit is set
6015 */
6016 rmb();
6017
6018 page = rx_buffer->page;
6019 prefetchw(page);
6020
6021 if (likely(!skb)) {
6022 void *page_addr = page_address(page) +
6023 rx_buffer->page_offset;
6024
6025 /* prefetch first cache line of first page */
6026 prefetch(page_addr);
6027#if L1_CACHE_BYTES < 128
6028 prefetch(page_addr + L1_CACHE_BYTES);
6029#endif
6030
6031 /* allocate a skb to store the frags */
6032 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
6033 IGB_RX_HDR_LEN);
6034 if (unlikely(!skb)) {
6035 rx_ring->rx_stats.alloc_failed++;
6036 return NULL;
6037 }
6038
6039 /*
6040 * we will be copying header into skb->data in
6041 * pskb_may_pull so it is in our interest to prefetch
6042 * it now to avoid a possible cache miss
6043 */
6044 prefetchw(skb->data);
6045 }
6046
6047 /* we are reusing so sync this buffer for CPU use */
6048 dma_sync_single_range_for_cpu(rx_ring->dev,
6049 rx_buffer->dma,
6050 rx_buffer->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006051 IGB_RX_BUFSZ,
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006052 DMA_FROM_DEVICE);
6053
6054 /* pull page into skb */
6055 if (igb_add_rx_frag(rx_ring, rx_buffer, rx_desc, skb)) {
6056 /* hand second half of page back to the ring */
6057 igb_reuse_rx_page(rx_ring, rx_buffer);
6058 } else {
6059 /* we are not reusing the buffer so unmap it */
6060 dma_unmap_page(rx_ring->dev, rx_buffer->dma,
6061 PAGE_SIZE, DMA_FROM_DEVICE);
6062 }
6063
6064 /* clear contents of rx_buffer */
6065 rx_buffer->page = NULL;
6066
6067 return skb;
6068}
6069
Alexander Duyckcd392f52011-08-26 07:43:59 +00006070static inline void igb_rx_checksum(struct igb_ring *ring,
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006071 union e1000_adv_rx_desc *rx_desc,
6072 struct sk_buff *skb)
Auke Kok9d5c8242008-01-24 02:22:38 -08006073{
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006074 skb_checksum_none_assert(skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08006075
Alexander Duyck294e7d72011-08-26 07:45:57 +00006076 /* Ignore Checksum bit is set */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006077 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_IXSM))
Alexander Duyck294e7d72011-08-26 07:45:57 +00006078 return;
6079
6080 /* Rx checksum disabled via ethtool */
6081 if (!(ring->netdev->features & NETIF_F_RXCSUM))
Auke Kok9d5c8242008-01-24 02:22:38 -08006082 return;
Alexander Duyck85ad76b2009-10-27 15:52:46 +00006083
Auke Kok9d5c8242008-01-24 02:22:38 -08006084 /* TCP/UDP checksum error bit is set */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006085 if (igb_test_staterr(rx_desc,
6086 E1000_RXDEXT_STATERR_TCPE |
6087 E1000_RXDEXT_STATERR_IPE)) {
Jesse Brandeburgb9473562009-04-27 22:36:13 +00006088 /*
6089 * work around errata with sctp packets where the TCPE aka
6090 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
6091 * packets, (aka let the stack check the crc32c)
6092 */
Alexander Duyck866cff02011-08-26 07:45:36 +00006093 if (!((skb->len == 60) &&
6094 test_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
Eric Dumazet12dcd862010-10-15 17:27:10 +00006095 u64_stats_update_begin(&ring->rx_syncp);
Alexander Duyck04a5fcaa2009-10-27 15:52:27 +00006096 ring->rx_stats.csum_err++;
Eric Dumazet12dcd862010-10-15 17:27:10 +00006097 u64_stats_update_end(&ring->rx_syncp);
6098 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006099 /* let the stack verify checksum errors */
Auke Kok9d5c8242008-01-24 02:22:38 -08006100 return;
6101 }
6102 /* It must be a TCP or UDP packet with a valid checksum */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006103 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_TCPCS |
6104 E1000_RXD_STAT_UDPCS))
Auke Kok9d5c8242008-01-24 02:22:38 -08006105 skb->ip_summed = CHECKSUM_UNNECESSARY;
6106
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006107 dev_dbg(ring->dev, "cksum success: bits %08X\n",
6108 le32_to_cpu(rx_desc->wb.upper.status_error));
Auke Kok9d5c8242008-01-24 02:22:38 -08006109}
6110
Alexander Duyck077887c2011-08-26 07:46:29 +00006111static inline void igb_rx_hash(struct igb_ring *ring,
6112 union e1000_adv_rx_desc *rx_desc,
6113 struct sk_buff *skb)
6114{
6115 if (ring->netdev->features & NETIF_F_RXHASH)
6116 skb->rxhash = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
6117}
6118
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006119/**
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006120 * igb_is_non_eop - process handling of non-EOP buffers
6121 * @rx_ring: Rx ring being processed
6122 * @rx_desc: Rx descriptor for current buffer
6123 * @skb: current socket buffer containing buffer in progress
6124 *
6125 * This function updates next to clean. If the buffer is an EOP buffer
6126 * this function exits returning false, otherwise it will place the
6127 * sk_buff in the next buffer to be chained and return true indicating
6128 * that this is in fact a non-EOP buffer.
6129 **/
6130static bool igb_is_non_eop(struct igb_ring *rx_ring,
6131 union e1000_adv_rx_desc *rx_desc)
6132{
6133 u32 ntc = rx_ring->next_to_clean + 1;
6134
6135 /* fetch, update, and store next to clean */
6136 ntc = (ntc < rx_ring->count) ? ntc : 0;
6137 rx_ring->next_to_clean = ntc;
6138
6139 prefetch(IGB_RX_DESC(rx_ring, ntc));
6140
6141 if (likely(igb_test_staterr(rx_desc, E1000_RXD_STAT_EOP)))
6142 return false;
6143
6144 return true;
6145}
6146
6147/**
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006148 * igb_get_headlen - determine size of header for LRO/GRO
6149 * @data: pointer to the start of the headers
6150 * @max_len: total length of section to find headers in
6151 *
6152 * This function is meant to determine the length of headers that will
6153 * be recognized by hardware for LRO, and GRO offloads. The main
6154 * motivation of doing this is to only perform one pull for IPv4 TCP
6155 * packets so that we can do basic things like calculating the gso_size
6156 * based on the average data per packet.
6157 **/
6158static unsigned int igb_get_headlen(unsigned char *data,
6159 unsigned int max_len)
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006160{
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006161 union {
6162 unsigned char *network;
6163 /* l2 headers */
6164 struct ethhdr *eth;
6165 struct vlan_hdr *vlan;
6166 /* l3 headers */
6167 struct iphdr *ipv4;
6168 struct ipv6hdr *ipv6;
6169 } hdr;
6170 __be16 protocol;
6171 u8 nexthdr = 0; /* default to not TCP */
6172 u8 hlen;
6173
6174 /* this should never happen, but better safe than sorry */
6175 if (max_len < ETH_HLEN)
6176 return max_len;
6177
6178 /* initialize network frame pointer */
6179 hdr.network = data;
6180
6181 /* set first protocol and move network header forward */
6182 protocol = hdr.eth->h_proto;
6183 hdr.network += ETH_HLEN;
6184
6185 /* handle any vlan tag if present */
6186 if (protocol == __constant_htons(ETH_P_8021Q)) {
6187 if ((hdr.network - data) > (max_len - VLAN_HLEN))
6188 return max_len;
6189
6190 protocol = hdr.vlan->h_vlan_encapsulated_proto;
6191 hdr.network += VLAN_HLEN;
6192 }
6193
6194 /* handle L3 protocols */
6195 if (protocol == __constant_htons(ETH_P_IP)) {
6196 if ((hdr.network - data) > (max_len - sizeof(struct iphdr)))
6197 return max_len;
6198
6199 /* access ihl as a u8 to avoid unaligned access on ia64 */
6200 hlen = (hdr.network[0] & 0x0F) << 2;
6201
6202 /* verify hlen meets minimum size requirements */
6203 if (hlen < sizeof(struct iphdr))
6204 return hdr.network - data;
6205
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006206 /* record next protocol if header is present */
6207 if (!hdr.ipv4->frag_off)
6208 nexthdr = hdr.ipv4->protocol;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006209 } else if (protocol == __constant_htons(ETH_P_IPV6)) {
6210 if ((hdr.network - data) > (max_len - sizeof(struct ipv6hdr)))
6211 return max_len;
6212
6213 /* record next protocol */
6214 nexthdr = hdr.ipv6->nexthdr;
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006215 hlen = sizeof(struct ipv6hdr);
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006216 } else {
6217 return hdr.network - data;
6218 }
6219
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006220 /* relocate pointer to start of L4 header */
6221 hdr.network += hlen;
6222
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006223 /* finally sort out TCP */
6224 if (nexthdr == IPPROTO_TCP) {
6225 if ((hdr.network - data) > (max_len - sizeof(struct tcphdr)))
6226 return max_len;
6227
6228 /* access doff as a u8 to avoid unaligned access on ia64 */
6229 hlen = (hdr.network[12] & 0xF0) >> 2;
6230
6231 /* verify hlen meets minimum size requirements */
6232 if (hlen < sizeof(struct tcphdr))
6233 return hdr.network - data;
6234
6235 hdr.network += hlen;
6236 } else if (nexthdr == IPPROTO_UDP) {
6237 if ((hdr.network - data) > (max_len - sizeof(struct udphdr)))
6238 return max_len;
6239
6240 hdr.network += sizeof(struct udphdr);
6241 }
6242
6243 /*
6244 * If everything has gone correctly hdr.network should be the
6245 * data section of the packet and will be the end of the header.
6246 * If not then it probably represents the end of the last recognized
6247 * header.
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006248 */
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006249 if ((hdr.network - data) < max_len)
6250 return hdr.network - data;
6251 else
6252 return max_len;
6253}
6254
6255/**
6256 * igb_pull_tail - igb specific version of skb_pull_tail
6257 * @rx_ring: rx descriptor ring packet is being transacted on
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006258 * @rx_desc: pointer to the EOP Rx descriptor
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006259 * @skb: pointer to current skb being adjusted
6260 *
6261 * This function is an igb specific version of __pskb_pull_tail. The
6262 * main difference between this version and the original function is that
6263 * this function can make several assumptions about the state of things
6264 * that allow for significant optimizations versus the standard function.
6265 * As a result we can do things like drop a frag and maintain an accurate
6266 * truesize for the skb.
6267 */
6268static void igb_pull_tail(struct igb_ring *rx_ring,
6269 union e1000_adv_rx_desc *rx_desc,
6270 struct sk_buff *skb)
6271{
6272 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
6273 unsigned char *va;
6274 unsigned int pull_len;
6275
6276 /*
6277 * it is valid to use page_address instead of kmap since we are
6278 * working with pages allocated out of the lomem pool per
6279 * alloc_page(GFP_ATOMIC)
6280 */
6281 va = skb_frag_address(frag);
6282
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006283 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
6284 /* retrieve timestamp from buffer */
6285 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
6286
6287 /* update pointers to remove timestamp header */
6288 skb_frag_size_sub(frag, IGB_TS_HDR_LEN);
6289 frag->page_offset += IGB_TS_HDR_LEN;
6290 skb->data_len -= IGB_TS_HDR_LEN;
6291 skb->len -= IGB_TS_HDR_LEN;
6292
6293 /* move va to start of packet data */
6294 va += IGB_TS_HDR_LEN;
6295 }
6296
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006297 /*
6298 * we need the header to contain the greater of either ETH_HLEN or
6299 * 60 bytes if the skb->len is less than 60 for skb_pad.
6300 */
6301 pull_len = igb_get_headlen(va, IGB_RX_HDR_LEN);
6302
6303 /* align pull length to size of long to optimize memcpy performance */
6304 skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
6305
6306 /* update all of the pointers */
6307 skb_frag_size_sub(frag, pull_len);
6308 frag->page_offset += pull_len;
6309 skb->data_len -= pull_len;
6310 skb->tail += pull_len;
6311}
6312
6313/**
6314 * igb_cleanup_headers - Correct corrupted or empty headers
6315 * @rx_ring: rx descriptor ring packet is being transacted on
6316 * @rx_desc: pointer to the EOP Rx descriptor
6317 * @skb: pointer to current skb being fixed
6318 *
6319 * Address the case where we are pulling data in on pages only
6320 * and as such no data is present in the skb header.
6321 *
6322 * In addition if skb is not at least 60 bytes we need to pad it so that
6323 * it is large enough to qualify as a valid Ethernet frame.
6324 *
6325 * Returns true if an error was encountered and skb was freed.
6326 **/
6327static bool igb_cleanup_headers(struct igb_ring *rx_ring,
6328 union e1000_adv_rx_desc *rx_desc,
6329 struct sk_buff *skb)
6330{
6331
6332 if (unlikely((igb_test_staterr(rx_desc,
6333 E1000_RXDEXT_ERR_FRAME_ERR_MASK)))) {
6334 struct net_device *netdev = rx_ring->netdev;
6335 if (!(netdev->features & NETIF_F_RXALL)) {
6336 dev_kfree_skb_any(skb);
6337 return true;
6338 }
6339 }
6340
6341 /* place header in linear portion of buffer */
6342 if (skb_is_nonlinear(skb))
6343 igb_pull_tail(rx_ring, rx_desc, skb);
6344
6345 /* if skb_pad returns an error the skb was freed */
6346 if (unlikely(skb->len < 60)) {
6347 int pad_len = 60 - skb->len;
6348
6349 if (skb_pad(skb, pad_len))
6350 return true;
6351 __skb_put(skb, pad_len);
6352 }
6353
6354 return false;
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006355}
6356
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006357/**
6358 * igb_process_skb_fields - Populate skb header fields from Rx descriptor
6359 * @rx_ring: rx descriptor ring packet is being transacted on
6360 * @rx_desc: pointer to the EOP Rx descriptor
6361 * @skb: pointer to current skb being populated
6362 *
6363 * This function checks the ring, descriptor, and packet information in
6364 * order to populate the hash, checksum, VLAN, timestamp, protocol, and
6365 * other fields within the skb.
6366 **/
6367static void igb_process_skb_fields(struct igb_ring *rx_ring,
6368 union e1000_adv_rx_desc *rx_desc,
6369 struct sk_buff *skb)
6370{
6371 struct net_device *dev = rx_ring->netdev;
6372
6373 igb_rx_hash(rx_ring, rx_desc, skb);
6374
6375 igb_rx_checksum(rx_ring, rx_desc, skb);
6376
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006377 igb_ptp_rx_hwtstamp(rx_ring->q_vector, rx_desc, skb);
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006378
6379 if ((dev->features & NETIF_F_HW_VLAN_RX) &&
6380 igb_test_staterr(rx_desc, E1000_RXD_STAT_VP)) {
6381 u16 vid;
6382 if (igb_test_staterr(rx_desc, E1000_RXDEXT_STATERR_LB) &&
6383 test_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags))
6384 vid = be16_to_cpu(rx_desc->wb.upper.vlan);
6385 else
6386 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
6387
6388 __vlan_hwaccel_put_tag(skb, vid);
6389 }
6390
6391 skb_record_rx_queue(skb, rx_ring->queue_index);
6392
6393 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
6394}
6395
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006396static bool igb_clean_rx_irq(struct igb_q_vector *q_vector, const int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08006397{
Alexander Duyck0ba82992011-08-26 07:45:47 +00006398 struct igb_ring *rx_ring = q_vector->rx.ring;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006399 struct sk_buff *skb = rx_ring->skb;
Auke Kok9d5c8242008-01-24 02:22:38 -08006400 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck16eb8812011-08-26 07:43:54 +00006401 u16 cleaned_count = igb_desc_unused(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08006402
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006403 do {
6404 union e1000_adv_rx_desc *rx_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08006405
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006406 /* return some buffers to hardware, one at a time is too slow */
6407 if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
6408 igb_alloc_rx_buffers(rx_ring, cleaned_count);
6409 cleaned_count = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006410 }
6411
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006412 rx_desc = IGB_RX_DESC(rx_ring, rx_ring->next_to_clean);
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006413
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006414 if (!igb_test_staterr(rx_desc, E1000_RXD_STAT_DD))
6415 break;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006416
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006417 /* retrieve a buffer from the ring */
6418 skb = igb_fetch_rx_buffer(rx_ring, rx_desc, skb);
Alexander Duyck16eb8812011-08-26 07:43:54 +00006419
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006420 /* exit if we failed to retrieve a buffer */
6421 if (!skb)
6422 break;
6423
6424 cleaned_count++;
6425
6426 /* fetch next buffer in frame if non-eop */
6427 if (igb_is_non_eop(rx_ring, rx_desc))
6428 continue;
Alexander Duyck44390ca2011-08-26 07:43:38 +00006429
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006430 /* verify the packet layout is correct */
6431 if (igb_cleanup_headers(rx_ring, rx_desc, skb)) {
6432 skb = NULL;
6433 continue;
Auke Kok9d5c8242008-01-24 02:22:38 -08006434 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006435
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006436 /* probably a little skewed due to removing CRC */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006437 total_bytes += skb->len;
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006438
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006439 /* populate checksum, timestamp, VLAN, and protocol */
6440 igb_process_skb_fields(rx_ring, rx_desc, skb);
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006441
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006442 napi_gro_receive(&q_vector->napi, skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08006443
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006444 /* reset skb pointer */
6445 skb = NULL;
6446
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006447 /* update budget accounting */
6448 total_packets++;
6449 } while (likely(total_packets < budget));
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006450
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006451 /* place incomplete frames back on ring for completion */
6452 rx_ring->skb = skb;
6453
Eric Dumazet12dcd862010-10-15 17:27:10 +00006454 u64_stats_update_begin(&rx_ring->rx_syncp);
Auke Kok9d5c8242008-01-24 02:22:38 -08006455 rx_ring->rx_stats.packets += total_packets;
6456 rx_ring->rx_stats.bytes += total_bytes;
Eric Dumazet12dcd862010-10-15 17:27:10 +00006457 u64_stats_update_end(&rx_ring->rx_syncp);
Alexander Duyck0ba82992011-08-26 07:45:47 +00006458 q_vector->rx.total_packets += total_packets;
6459 q_vector->rx.total_bytes += total_bytes;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006460
6461 if (cleaned_count)
Alexander Duyckcd392f52011-08-26 07:43:59 +00006462 igb_alloc_rx_buffers(rx_ring, cleaned_count);
Alexander Duyckc023cd82011-08-26 07:43:43 +00006463
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006464 return (total_packets < budget);
Auke Kok9d5c8242008-01-24 02:22:38 -08006465}
6466
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006467static bool igb_alloc_mapped_page(struct igb_ring *rx_ring,
6468 struct igb_rx_buffer *bi)
Alexander Duyckc023cd82011-08-26 07:43:43 +00006469{
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006470 struct page *page = bi->page;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006471 dma_addr_t dma;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006472
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006473 /* since we are recycling buffers we should seldom need to alloc */
6474 if (likely(page))
Alexander Duyckc023cd82011-08-26 07:43:43 +00006475 return true;
6476
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006477 /* alloc new page for storage */
6478 page = __skb_alloc_page(GFP_ATOMIC | __GFP_COLD, NULL);
6479 if (unlikely(!page)) {
6480 rx_ring->rx_stats.alloc_failed++;
6481 return false;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006482 }
6483
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006484 /* map page for use */
6485 dma = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
Alexander Duyckc023cd82011-08-26 07:43:43 +00006486
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006487 /*
6488 * if mapping failed free memory back to system since
6489 * there isn't much point in holding memory we can't use
6490 */
Alexander Duyckc023cd82011-08-26 07:43:43 +00006491 if (dma_mapping_error(rx_ring->dev, dma)) {
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006492 __free_page(page);
6493
Alexander Duyckc023cd82011-08-26 07:43:43 +00006494 rx_ring->rx_stats.alloc_failed++;
6495 return false;
6496 }
6497
6498 bi->dma = dma;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006499 bi->page = page;
6500 bi->page_offset = 0;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006501
Alexander Duyckc023cd82011-08-26 07:43:43 +00006502 return true;
6503}
6504
Auke Kok9d5c8242008-01-24 02:22:38 -08006505/**
Alexander Duyckcd392f52011-08-26 07:43:59 +00006506 * igb_alloc_rx_buffers - Replace used receive buffers; packet split
Auke Kok9d5c8242008-01-24 02:22:38 -08006507 * @adapter: address of board private structure
6508 **/
Alexander Duyckcd392f52011-08-26 07:43:59 +00006509void igb_alloc_rx_buffers(struct igb_ring *rx_ring, u16 cleaned_count)
Auke Kok9d5c8242008-01-24 02:22:38 -08006510{
Auke Kok9d5c8242008-01-24 02:22:38 -08006511 union e1000_adv_rx_desc *rx_desc;
Alexander Duyck06034642011-08-26 07:44:22 +00006512 struct igb_rx_buffer *bi;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006513 u16 i = rx_ring->next_to_use;
Auke Kok9d5c8242008-01-24 02:22:38 -08006514
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006515 /* nothing to do */
6516 if (!cleaned_count)
6517 return;
6518
Alexander Duyck601369062011-08-26 07:44:05 +00006519 rx_desc = IGB_RX_DESC(rx_ring, i);
Alexander Duyck06034642011-08-26 07:44:22 +00006520 bi = &rx_ring->rx_buffer_info[i];
Alexander Duyckc023cd82011-08-26 07:43:43 +00006521 i -= rx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08006522
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006523 do {
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006524 if (!igb_alloc_mapped_page(rx_ring, bi))
Alexander Duyckc023cd82011-08-26 07:43:43 +00006525 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08006526
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006527 /*
6528 * Refresh the desc even if buffer_addrs didn't change
6529 * because each write-back erases this info.
6530 */
6531 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
Auke Kok9d5c8242008-01-24 02:22:38 -08006532
Alexander Duyckc023cd82011-08-26 07:43:43 +00006533 rx_desc++;
6534 bi++;
Auke Kok9d5c8242008-01-24 02:22:38 -08006535 i++;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006536 if (unlikely(!i)) {
Alexander Duyck601369062011-08-26 07:44:05 +00006537 rx_desc = IGB_RX_DESC(rx_ring, 0);
Alexander Duyck06034642011-08-26 07:44:22 +00006538 bi = rx_ring->rx_buffer_info;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006539 i -= rx_ring->count;
6540 }
6541
6542 /* clear the hdr_addr for the next_to_use descriptor */
6543 rx_desc->read.hdr_addr = 0;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006544
6545 cleaned_count--;
6546 } while (cleaned_count);
Auke Kok9d5c8242008-01-24 02:22:38 -08006547
Alexander Duyckc023cd82011-08-26 07:43:43 +00006548 i += rx_ring->count;
6549
Auke Kok9d5c8242008-01-24 02:22:38 -08006550 if (rx_ring->next_to_use != i) {
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006551 /* record the next descriptor to use */
Auke Kok9d5c8242008-01-24 02:22:38 -08006552 rx_ring->next_to_use = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08006553
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006554 /* update next to alloc since we have filled the ring */
6555 rx_ring->next_to_alloc = i;
6556
6557 /*
6558 * Force memory writes to complete before letting h/w
Auke Kok9d5c8242008-01-24 02:22:38 -08006559 * know there are new descriptors to fetch. (Only
6560 * applicable for weak-ordered memory model archs,
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006561 * such as IA-64).
6562 */
Auke Kok9d5c8242008-01-24 02:22:38 -08006563 wmb();
Alexander Duyckfce99e32009-10-27 15:51:27 +00006564 writel(i, rx_ring->tail);
Auke Kok9d5c8242008-01-24 02:22:38 -08006565 }
6566}
6567
6568/**
6569 * igb_mii_ioctl -
6570 * @netdev:
6571 * @ifreq:
6572 * @cmd:
6573 **/
6574static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6575{
6576 struct igb_adapter *adapter = netdev_priv(netdev);
6577 struct mii_ioctl_data *data = if_mii(ifr);
6578
6579 if (adapter->hw.phy.media_type != e1000_media_type_copper)
6580 return -EOPNOTSUPP;
6581
6582 switch (cmd) {
6583 case SIOCGMIIPHY:
6584 data->phy_id = adapter->hw.phy.addr;
6585 break;
6586 case SIOCGMIIREG:
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08006587 if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
6588 &data->val_out))
Auke Kok9d5c8242008-01-24 02:22:38 -08006589 return -EIO;
6590 break;
6591 case SIOCSMIIREG:
6592 default:
6593 return -EOPNOTSUPP;
6594 }
6595 return 0;
6596}
6597
6598/**
6599 * igb_ioctl -
6600 * @netdev:
6601 * @ifreq:
6602 * @cmd:
6603 **/
6604static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6605{
6606 switch (cmd) {
6607 case SIOCGMIIPHY:
6608 case SIOCGMIIREG:
6609 case SIOCSMIIREG:
6610 return igb_mii_ioctl(netdev, ifr, cmd);
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00006611 case SIOCSHWTSTAMP:
Matthew Vicka79f4f82012-08-10 05:40:44 +00006612 return igb_ptp_hwtstamp_ioctl(netdev, ifr, cmd);
Auke Kok9d5c8242008-01-24 02:22:38 -08006613 default:
6614 return -EOPNOTSUPP;
6615 }
6616}
6617
Alexander Duyck009bc062009-07-23 18:08:35 +00006618s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6619{
6620 struct igb_adapter *adapter = hw->back;
Alexander Duyck009bc062009-07-23 18:08:35 +00006621
Jiang Liu23d028c2012-08-20 13:32:20 -06006622 if (pcie_capability_read_word(adapter->pdev, reg, value))
Alexander Duyck009bc062009-07-23 18:08:35 +00006623 return -E1000_ERR_CONFIG;
6624
Alexander Duyck009bc062009-07-23 18:08:35 +00006625 return 0;
6626}
6627
6628s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6629{
6630 struct igb_adapter *adapter = hw->back;
Alexander Duyck009bc062009-07-23 18:08:35 +00006631
Jiang Liu23d028c2012-08-20 13:32:20 -06006632 if (pcie_capability_write_word(adapter->pdev, reg, *value))
Alexander Duyck009bc062009-07-23 18:08:35 +00006633 return -E1000_ERR_CONFIG;
6634
Alexander Duyck009bc062009-07-23 18:08:35 +00006635 return 0;
6636}
6637
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006638static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features)
Auke Kok9d5c8242008-01-24 02:22:38 -08006639{
6640 struct igb_adapter *adapter = netdev_priv(netdev);
6641 struct e1000_hw *hw = &adapter->hw;
6642 u32 ctrl, rctl;
Alexander Duyck5faf0302011-08-26 07:46:08 +00006643 bool enable = !!(features & NETIF_F_HW_VLAN_RX);
Auke Kok9d5c8242008-01-24 02:22:38 -08006644
Alexander Duyck5faf0302011-08-26 07:46:08 +00006645 if (enable) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006646 /* enable VLAN tag insert/strip */
6647 ctrl = rd32(E1000_CTRL);
6648 ctrl |= E1000_CTRL_VME;
6649 wr32(E1000_CTRL, ctrl);
6650
Alexander Duyck51466232009-10-27 23:47:35 +00006651 /* Disable CFI check */
Auke Kok9d5c8242008-01-24 02:22:38 -08006652 rctl = rd32(E1000_RCTL);
Auke Kok9d5c8242008-01-24 02:22:38 -08006653 rctl &= ~E1000_RCTL_CFIEN;
6654 wr32(E1000_RCTL, rctl);
Auke Kok9d5c8242008-01-24 02:22:38 -08006655 } else {
6656 /* disable VLAN tag insert/strip */
6657 ctrl = rd32(E1000_CTRL);
6658 ctrl &= ~E1000_CTRL_VME;
6659 wr32(E1000_CTRL, ctrl);
Auke Kok9d5c8242008-01-24 02:22:38 -08006660 }
6661
Alexander Duycke1739522009-02-19 20:39:44 -08006662 igb_rlpml_set(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006663}
6664
Jiri Pirko8e586132011-12-08 19:52:37 -05006665static int igb_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
Auke Kok9d5c8242008-01-24 02:22:38 -08006666{
6667 struct igb_adapter *adapter = netdev_priv(netdev);
6668 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006669 int pf_id = adapter->vfs_allocated_count;
Auke Kok9d5c8242008-01-24 02:22:38 -08006670
Alexander Duyck51466232009-10-27 23:47:35 +00006671 /* attempt to add filter to vlvf array */
6672 igb_vlvf_set(adapter, vid, true, pf_id);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006673
Alexander Duyck51466232009-10-27 23:47:35 +00006674 /* add the filter since PF can receive vlans w/o entry in vlvf */
6675 igb_vfta_set(hw, vid, true);
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006676
6677 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05006678
6679 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08006680}
6681
Jiri Pirko8e586132011-12-08 19:52:37 -05006682static int igb_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
Auke Kok9d5c8242008-01-24 02:22:38 -08006683{
6684 struct igb_adapter *adapter = netdev_priv(netdev);
6685 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006686 int pf_id = adapter->vfs_allocated_count;
Alexander Duyck51466232009-10-27 23:47:35 +00006687 s32 err;
Auke Kok9d5c8242008-01-24 02:22:38 -08006688
Alexander Duyck51466232009-10-27 23:47:35 +00006689 /* remove vlan from VLVF table array */
6690 err = igb_vlvf_set(adapter, vid, false, pf_id);
Auke Kok9d5c8242008-01-24 02:22:38 -08006691
Alexander Duyck51466232009-10-27 23:47:35 +00006692 /* if vid was not present in VLVF just remove it from table */
6693 if (err)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006694 igb_vfta_set(hw, vid, false);
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006695
6696 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05006697
6698 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08006699}
6700
6701static void igb_restore_vlan(struct igb_adapter *adapter)
6702{
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006703 u16 vid;
Auke Kok9d5c8242008-01-24 02:22:38 -08006704
Alexander Duyck5faf0302011-08-26 07:46:08 +00006705 igb_vlan_mode(adapter->netdev, adapter->netdev->features);
6706
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006707 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
6708 igb_vlan_rx_add_vid(adapter->netdev, vid);
Auke Kok9d5c8242008-01-24 02:22:38 -08006709}
6710
David Decotigny14ad2512011-04-27 18:32:43 +00006711int igb_set_spd_dplx(struct igb_adapter *adapter, u32 spd, u8 dplx)
Auke Kok9d5c8242008-01-24 02:22:38 -08006712{
Alexander Duyck090b1792009-10-27 23:51:55 +00006713 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08006714 struct e1000_mac_info *mac = &adapter->hw.mac;
6715
6716 mac->autoneg = 0;
6717
David Decotigny14ad2512011-04-27 18:32:43 +00006718 /* Make sure dplx is at most 1 bit and lsb of speed is not set
6719 * for the switch() below to work */
6720 if ((spd & 1) || (dplx & ~1))
6721 goto err_inval;
6722
Carolyn Wybornycd2638a2010-10-12 22:27:02 +00006723 /* Fiber NIC's only allow 1000 Gbps Full duplex */
6724 if ((adapter->hw.phy.media_type == e1000_media_type_internal_serdes) &&
David Decotigny14ad2512011-04-27 18:32:43 +00006725 spd != SPEED_1000 &&
6726 dplx != DUPLEX_FULL)
6727 goto err_inval;
Carolyn Wybornycd2638a2010-10-12 22:27:02 +00006728
David Decotigny14ad2512011-04-27 18:32:43 +00006729 switch (spd + dplx) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006730 case SPEED_10 + DUPLEX_HALF:
6731 mac->forced_speed_duplex = ADVERTISE_10_HALF;
6732 break;
6733 case SPEED_10 + DUPLEX_FULL:
6734 mac->forced_speed_duplex = ADVERTISE_10_FULL;
6735 break;
6736 case SPEED_100 + DUPLEX_HALF:
6737 mac->forced_speed_duplex = ADVERTISE_100_HALF;
6738 break;
6739 case SPEED_100 + DUPLEX_FULL:
6740 mac->forced_speed_duplex = ADVERTISE_100_FULL;
6741 break;
6742 case SPEED_1000 + DUPLEX_FULL:
6743 mac->autoneg = 1;
6744 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
6745 break;
6746 case SPEED_1000 + DUPLEX_HALF: /* not supported */
6747 default:
David Decotigny14ad2512011-04-27 18:32:43 +00006748 goto err_inval;
Auke Kok9d5c8242008-01-24 02:22:38 -08006749 }
Jesse Brandeburg8376dad2012-07-26 02:31:19 +00006750
6751 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
6752 adapter->hw.phy.mdix = AUTO_ALL_MODES;
6753
Auke Kok9d5c8242008-01-24 02:22:38 -08006754 return 0;
David Decotigny14ad2512011-04-27 18:32:43 +00006755
6756err_inval:
6757 dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n");
6758 return -EINVAL;
Auke Kok9d5c8242008-01-24 02:22:38 -08006759}
6760
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006761static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake,
6762 bool runtime)
Auke Kok9d5c8242008-01-24 02:22:38 -08006763{
6764 struct net_device *netdev = pci_get_drvdata(pdev);
6765 struct igb_adapter *adapter = netdev_priv(netdev);
6766 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07006767 u32 ctrl, rctl, status;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006768 u32 wufc = runtime ? E1000_WUFC_LNKC : adapter->wol;
Auke Kok9d5c8242008-01-24 02:22:38 -08006769#ifdef CONFIG_PM
6770 int retval = 0;
6771#endif
6772
6773 netif_device_detach(netdev);
6774
Alexander Duycka88f10e2008-07-08 15:13:38 -07006775 if (netif_running(netdev))
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006776 __igb_close(netdev, true);
Alexander Duycka88f10e2008-07-08 15:13:38 -07006777
Alexander Duyck047e0032009-10-27 15:49:27 +00006778 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006779
6780#ifdef CONFIG_PM
6781 retval = pci_save_state(pdev);
6782 if (retval)
6783 return retval;
6784#endif
6785
6786 status = rd32(E1000_STATUS);
6787 if (status & E1000_STATUS_LU)
6788 wufc &= ~E1000_WUFC_LNKC;
6789
6790 if (wufc) {
6791 igb_setup_rctl(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00006792 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08006793
6794 /* turn on all-multi mode if wake on multicast is enabled */
6795 if (wufc & E1000_WUFC_MC) {
6796 rctl = rd32(E1000_RCTL);
6797 rctl |= E1000_RCTL_MPE;
6798 wr32(E1000_RCTL, rctl);
6799 }
6800
6801 ctrl = rd32(E1000_CTRL);
6802 /* advertise wake from D3Cold */
6803 #define E1000_CTRL_ADVD3WUC 0x00100000
6804 /* phy power management enable */
6805 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
6806 ctrl |= E1000_CTRL_ADVD3WUC;
6807 wr32(E1000_CTRL, ctrl);
6808
Auke Kok9d5c8242008-01-24 02:22:38 -08006809 /* Allow time for pending master requests to run */
Alexander Duyck330a6d62009-10-27 23:51:35 +00006810 igb_disable_pcie_master(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08006811
6812 wr32(E1000_WUC, E1000_WUC_PME_EN);
6813 wr32(E1000_WUFC, wufc);
Auke Kok9d5c8242008-01-24 02:22:38 -08006814 } else {
6815 wr32(E1000_WUC, 0);
6816 wr32(E1000_WUFC, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08006817 }
6818
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006819 *enable_wake = wufc || adapter->en_mng_pt;
6820 if (!*enable_wake)
Nick Nunley88a268c2010-02-17 01:01:59 +00006821 igb_power_down_link(adapter);
6822 else
6823 igb_power_up_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006824
6825 /* Release control of h/w to f/w. If f/w is AMT enabled, this
6826 * would have already happened in close and is redundant. */
6827 igb_release_hw_control(adapter);
6828
6829 pci_disable_device(pdev);
6830
Auke Kok9d5c8242008-01-24 02:22:38 -08006831 return 0;
6832}
6833
6834#ifdef CONFIG_PM
Emil Tantilovd9dd9662012-01-28 08:10:35 +00006835#ifdef CONFIG_PM_SLEEP
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006836static int igb_suspend(struct device *dev)
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006837{
6838 int retval;
6839 bool wake;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006840 struct pci_dev *pdev = to_pci_dev(dev);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006841
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006842 retval = __igb_shutdown(pdev, &wake, 0);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006843 if (retval)
6844 return retval;
6845
6846 if (wake) {
6847 pci_prepare_to_sleep(pdev);
6848 } else {
6849 pci_wake_from_d3(pdev, false);
6850 pci_set_power_state(pdev, PCI_D3hot);
6851 }
6852
6853 return 0;
6854}
Emil Tantilovd9dd9662012-01-28 08:10:35 +00006855#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006856
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006857static int igb_resume(struct device *dev)
Auke Kok9d5c8242008-01-24 02:22:38 -08006858{
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006859 struct pci_dev *pdev = to_pci_dev(dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08006860 struct net_device *netdev = pci_get_drvdata(pdev);
6861 struct igb_adapter *adapter = netdev_priv(netdev);
6862 struct e1000_hw *hw = &adapter->hw;
6863 u32 err;
6864
6865 pci_set_power_state(pdev, PCI_D0);
6866 pci_restore_state(pdev);
Nick Nunleyb94f2d72010-02-17 01:02:19 +00006867 pci_save_state(pdev);
Taku Izumi42bfd33a2008-06-20 12:10:30 +09006868
Alexander Duyckaed5dec2009-02-06 23:16:04 +00006869 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08006870 if (err) {
6871 dev_err(&pdev->dev,
6872 "igb: Cannot enable PCI device from suspend\n");
6873 return err;
6874 }
6875 pci_set_master(pdev);
6876
6877 pci_enable_wake(pdev, PCI_D3hot, 0);
6878 pci_enable_wake(pdev, PCI_D3cold, 0);
6879
Stefan Assmann53c7d062012-12-04 06:00:12 +00006880 if (igb_init_interrupt_scheme(adapter, true)) {
Alexander Duycka88f10e2008-07-08 15:13:38 -07006881 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
6882 return -ENOMEM;
Auke Kok9d5c8242008-01-24 02:22:38 -08006883 }
6884
Auke Kok9d5c8242008-01-24 02:22:38 -08006885 igb_reset(adapter);
Alexander Duycka8564f02009-02-06 23:21:10 +00006886
6887 /* let the f/w know that the h/w is now under the control of the
6888 * driver. */
6889 igb_get_hw_control(adapter);
6890
Auke Kok9d5c8242008-01-24 02:22:38 -08006891 wr32(E1000_WUS, ~0);
6892
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006893 if (netdev->flags & IFF_UP) {
Alexander Duyck0c2cc022012-09-25 00:31:22 +00006894 rtnl_lock();
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006895 err = __igb_open(netdev, true);
Alexander Duyck0c2cc022012-09-25 00:31:22 +00006896 rtnl_unlock();
Alexander Duycka88f10e2008-07-08 15:13:38 -07006897 if (err)
6898 return err;
6899 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006900
6901 netif_device_attach(netdev);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006902 return 0;
6903}
6904
6905#ifdef CONFIG_PM_RUNTIME
6906static int igb_runtime_idle(struct device *dev)
6907{
6908 struct pci_dev *pdev = to_pci_dev(dev);
6909 struct net_device *netdev = pci_get_drvdata(pdev);
6910 struct igb_adapter *adapter = netdev_priv(netdev);
6911
6912 if (!igb_has_link(adapter))
6913 pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
6914
6915 return -EBUSY;
6916}
6917
6918static int igb_runtime_suspend(struct device *dev)
6919{
6920 struct pci_dev *pdev = to_pci_dev(dev);
6921 int retval;
6922 bool wake;
6923
6924 retval = __igb_shutdown(pdev, &wake, 1);
6925 if (retval)
6926 return retval;
6927
6928 if (wake) {
6929 pci_prepare_to_sleep(pdev);
6930 } else {
6931 pci_wake_from_d3(pdev, false);
6932 pci_set_power_state(pdev, PCI_D3hot);
6933 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006934
Auke Kok9d5c8242008-01-24 02:22:38 -08006935 return 0;
6936}
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006937
6938static int igb_runtime_resume(struct device *dev)
6939{
6940 return igb_resume(dev);
6941}
6942#endif /* CONFIG_PM_RUNTIME */
Auke Kok9d5c8242008-01-24 02:22:38 -08006943#endif
6944
6945static void igb_shutdown(struct pci_dev *pdev)
6946{
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006947 bool wake;
6948
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006949 __igb_shutdown(pdev, &wake, 0);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006950
6951 if (system_state == SYSTEM_POWER_OFF) {
6952 pci_wake_from_d3(pdev, wake);
6953 pci_set_power_state(pdev, PCI_D3hot);
6954 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006955}
6956
Greg Rosefa44f2f2013-01-17 01:03:06 -08006957#ifdef CONFIG_PCI_IOV
6958static int igb_sriov_reinit(struct pci_dev *dev)
6959{
6960 struct net_device *netdev = pci_get_drvdata(dev);
6961 struct igb_adapter *adapter = netdev_priv(netdev);
6962 struct pci_dev *pdev = adapter->pdev;
6963
6964 rtnl_lock();
6965
6966 if (netif_running(netdev))
6967 igb_close(netdev);
6968
6969 igb_clear_interrupt_scheme(adapter);
6970
6971 igb_init_queue_configuration(adapter);
6972
6973 if (igb_init_interrupt_scheme(adapter, true)) {
6974 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
6975 return -ENOMEM;
6976 }
6977
6978 if (netif_running(netdev))
6979 igb_open(netdev);
6980
6981 rtnl_unlock();
6982
6983 return 0;
6984}
6985
6986static int igb_pci_disable_sriov(struct pci_dev *dev)
6987{
6988 int err = igb_disable_sriov(dev);
6989
6990 if (!err)
6991 err = igb_sriov_reinit(dev);
6992
6993 return err;
6994}
6995
6996static int igb_pci_enable_sriov(struct pci_dev *dev, int num_vfs)
6997{
6998 int err = igb_enable_sriov(dev, num_vfs);
6999
7000 if (err)
7001 goto out;
7002
7003 err = igb_sriov_reinit(dev);
7004 if (!err)
7005 return num_vfs;
7006
7007out:
7008 return err;
7009}
7010
7011#endif
7012static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
7013{
7014#ifdef CONFIG_PCI_IOV
7015 if (num_vfs == 0)
7016 return igb_pci_disable_sriov(dev);
7017 else
7018 return igb_pci_enable_sriov(dev, num_vfs);
7019#endif
7020 return 0;
7021}
7022
Auke Kok9d5c8242008-01-24 02:22:38 -08007023#ifdef CONFIG_NET_POLL_CONTROLLER
7024/*
7025 * Polling 'interrupt' - used by things like netconsole to send skbs
7026 * without having to re-enable interrupts. It's not called while
7027 * the interrupt routine is executing.
7028 */
7029static void igb_netpoll(struct net_device *netdev)
7030{
7031 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00007032 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00007033 struct igb_q_vector *q_vector;
Auke Kok9d5c8242008-01-24 02:22:38 -08007034 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08007035
Alexander Duyck047e0032009-10-27 15:49:27 +00007036 for (i = 0; i < adapter->num_q_vectors; i++) {
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00007037 q_vector = adapter->q_vector[i];
7038 if (adapter->msix_entries)
7039 wr32(E1000_EIMC, q_vector->eims_value);
7040 else
7041 igb_irq_disable(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00007042 napi_schedule(&q_vector->napi);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00007043 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007044}
7045#endif /* CONFIG_NET_POLL_CONTROLLER */
7046
7047/**
7048 * igb_io_error_detected - called when PCI error is detected
7049 * @pdev: Pointer to PCI device
7050 * @state: The current pci connection state
7051 *
7052 * This function is called after a PCI bus error affecting
7053 * this device has been detected.
7054 */
7055static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
7056 pci_channel_state_t state)
7057{
7058 struct net_device *netdev = pci_get_drvdata(pdev);
7059 struct igb_adapter *adapter = netdev_priv(netdev);
7060
7061 netif_device_detach(netdev);
7062
Alexander Duyck59ed6ee2009-06-30 12:46:34 +00007063 if (state == pci_channel_io_perm_failure)
7064 return PCI_ERS_RESULT_DISCONNECT;
7065
Auke Kok9d5c8242008-01-24 02:22:38 -08007066 if (netif_running(netdev))
7067 igb_down(adapter);
7068 pci_disable_device(pdev);
7069
7070 /* Request a slot slot reset. */
7071 return PCI_ERS_RESULT_NEED_RESET;
7072}
7073
7074/**
7075 * igb_io_slot_reset - called after the pci bus has been reset.
7076 * @pdev: Pointer to PCI device
7077 *
7078 * Restart the card from scratch, as if from a cold-boot. Implementation
7079 * resembles the first-half of the igb_resume routine.
7080 */
7081static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
7082{
7083 struct net_device *netdev = pci_get_drvdata(pdev);
7084 struct igb_adapter *adapter = netdev_priv(netdev);
7085 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck40a914f2008-11-27 00:24:37 -08007086 pci_ers_result_t result;
Taku Izumi42bfd33a2008-06-20 12:10:30 +09007087 int err;
Auke Kok9d5c8242008-01-24 02:22:38 -08007088
Alexander Duyckaed5dec2009-02-06 23:16:04 +00007089 if (pci_enable_device_mem(pdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08007090 dev_err(&pdev->dev,
7091 "Cannot re-enable PCI device after reset.\n");
Alexander Duyck40a914f2008-11-27 00:24:37 -08007092 result = PCI_ERS_RESULT_DISCONNECT;
7093 } else {
7094 pci_set_master(pdev);
7095 pci_restore_state(pdev);
Nick Nunleyb94f2d72010-02-17 01:02:19 +00007096 pci_save_state(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08007097
7098 pci_enable_wake(pdev, PCI_D3hot, 0);
7099 pci_enable_wake(pdev, PCI_D3cold, 0);
7100
7101 igb_reset(adapter);
7102 wr32(E1000_WUS, ~0);
7103 result = PCI_ERS_RESULT_RECOVERED;
Auke Kok9d5c8242008-01-24 02:22:38 -08007104 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007105
Jeff Kirsherea943d42008-12-11 20:34:19 -08007106 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7107 if (err) {
7108 dev_err(&pdev->dev, "pci_cleanup_aer_uncorrect_error_status "
7109 "failed 0x%0x\n", err);
7110 /* non-fatal, continue */
7111 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007112
Alexander Duyck40a914f2008-11-27 00:24:37 -08007113 return result;
Auke Kok9d5c8242008-01-24 02:22:38 -08007114}
7115
7116/**
7117 * igb_io_resume - called when traffic can start flowing again.
7118 * @pdev: Pointer to PCI device
7119 *
7120 * This callback is called when the error recovery driver tells us that
7121 * its OK to resume normal operation. Implementation resembles the
7122 * second-half of the igb_resume routine.
7123 */
7124static void igb_io_resume(struct pci_dev *pdev)
7125{
7126 struct net_device *netdev = pci_get_drvdata(pdev);
7127 struct igb_adapter *adapter = netdev_priv(netdev);
7128
Auke Kok9d5c8242008-01-24 02:22:38 -08007129 if (netif_running(netdev)) {
7130 if (igb_up(adapter)) {
7131 dev_err(&pdev->dev, "igb_up failed after reset\n");
7132 return;
7133 }
7134 }
7135
7136 netif_device_attach(netdev);
7137
7138 /* let the f/w know that the h/w is now under the control of the
7139 * driver. */
7140 igb_get_hw_control(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08007141}
7142
Alexander Duyck26ad9172009-10-05 06:32:49 +00007143static void igb_rar_set_qsel(struct igb_adapter *adapter, u8 *addr, u32 index,
7144 u8 qsel)
7145{
7146 u32 rar_low, rar_high;
7147 struct e1000_hw *hw = &adapter->hw;
7148
7149 /* HW expects these in little endian so we reverse the byte order
7150 * from network order (big endian) to little endian
7151 */
7152 rar_low = ((u32) addr[0] | ((u32) addr[1] << 8) |
7153 ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
7154 rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
7155
7156 /* Indicate to hardware the Address is Valid. */
7157 rar_high |= E1000_RAH_AV;
7158
7159 if (hw->mac.type == e1000_82575)
7160 rar_high |= E1000_RAH_POOL_1 * qsel;
7161 else
7162 rar_high |= E1000_RAH_POOL_1 << qsel;
7163
7164 wr32(E1000_RAL(index), rar_low);
7165 wrfl();
7166 wr32(E1000_RAH(index), rar_high);
7167 wrfl();
7168}
7169
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007170static int igb_set_vf_mac(struct igb_adapter *adapter,
7171 int vf, unsigned char *mac_addr)
7172{
7173 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00007174 /* VF MAC addresses start at end of receive addresses and moves
7175 * torwards the first, as a result a collision should not be possible */
7176 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007177
Alexander Duyck37680112009-02-19 20:40:30 -08007178 memcpy(adapter->vf_data[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007179
Alexander Duyck26ad9172009-10-05 06:32:49 +00007180 igb_rar_set_qsel(adapter, mac_addr, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007181
7182 return 0;
7183}
7184
Williams, Mitch A8151d292010-02-10 01:44:24 +00007185static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
7186{
7187 struct igb_adapter *adapter = netdev_priv(netdev);
7188 if (!is_valid_ether_addr(mac) || (vf >= adapter->vfs_allocated_count))
7189 return -EINVAL;
7190 adapter->vf_data[vf].flags |= IGB_VF_FLAG_PF_SET_MAC;
7191 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
7192 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
7193 " change effective.");
7194 if (test_bit(__IGB_DOWN, &adapter->state)) {
7195 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
7196 " but the PF device is not up.\n");
7197 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
7198 " attempting to use the VF device.\n");
7199 }
7200 return igb_set_vf_mac(adapter, vf, mac);
7201}
7202
Lior Levy17dc5662011-02-08 02:28:46 +00007203static int igb_link_mbps(int internal_link_speed)
7204{
7205 switch (internal_link_speed) {
7206 case SPEED_100:
7207 return 100;
7208 case SPEED_1000:
7209 return 1000;
7210 default:
7211 return 0;
7212 }
7213}
7214
7215static void igb_set_vf_rate_limit(struct e1000_hw *hw, int vf, int tx_rate,
7216 int link_speed)
7217{
7218 int rf_dec, rf_int;
7219 u32 bcnrc_val;
7220
7221 if (tx_rate != 0) {
7222 /* Calculate the rate factor values to set */
7223 rf_int = link_speed / tx_rate;
7224 rf_dec = (link_speed - (rf_int * tx_rate));
7225 rf_dec = (rf_dec * (1<<E1000_RTTBCNRC_RF_INT_SHIFT)) / tx_rate;
7226
7227 bcnrc_val = E1000_RTTBCNRC_RS_ENA;
7228 bcnrc_val |= ((rf_int<<E1000_RTTBCNRC_RF_INT_SHIFT) &
7229 E1000_RTTBCNRC_RF_INT_MASK);
7230 bcnrc_val |= (rf_dec & E1000_RTTBCNRC_RF_DEC_MASK);
7231 } else {
7232 bcnrc_val = 0;
7233 }
7234
7235 wr32(E1000_RTTDQSEL, vf); /* vf X uses queue X */
Lior Levyf00b0da2011-06-04 06:05:03 +00007236 /*
7237 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
7238 * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported.
7239 */
7240 wr32(E1000_RTTBCNRM, 0x14);
Lior Levy17dc5662011-02-08 02:28:46 +00007241 wr32(E1000_RTTBCNRC, bcnrc_val);
7242}
7243
7244static void igb_check_vf_rate_limit(struct igb_adapter *adapter)
7245{
7246 int actual_link_speed, i;
7247 bool reset_rate = false;
7248
7249 /* VF TX rate limit was not set or not supported */
7250 if ((adapter->vf_rate_link_speed == 0) ||
7251 (adapter->hw.mac.type != e1000_82576))
7252 return;
7253
7254 actual_link_speed = igb_link_mbps(adapter->link_speed);
7255 if (actual_link_speed != adapter->vf_rate_link_speed) {
7256 reset_rate = true;
7257 adapter->vf_rate_link_speed = 0;
7258 dev_info(&adapter->pdev->dev,
7259 "Link speed has been changed. VF Transmit "
7260 "rate is disabled\n");
7261 }
7262
7263 for (i = 0; i < adapter->vfs_allocated_count; i++) {
7264 if (reset_rate)
7265 adapter->vf_data[i].tx_rate = 0;
7266
7267 igb_set_vf_rate_limit(&adapter->hw, i,
7268 adapter->vf_data[i].tx_rate,
7269 actual_link_speed);
7270 }
7271}
7272
Williams, Mitch A8151d292010-02-10 01:44:24 +00007273static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
7274{
Lior Levy17dc5662011-02-08 02:28:46 +00007275 struct igb_adapter *adapter = netdev_priv(netdev);
7276 struct e1000_hw *hw = &adapter->hw;
7277 int actual_link_speed;
7278
7279 if (hw->mac.type != e1000_82576)
7280 return -EOPNOTSUPP;
7281
7282 actual_link_speed = igb_link_mbps(adapter->link_speed);
7283 if ((vf >= adapter->vfs_allocated_count) ||
7284 (!(rd32(E1000_STATUS) & E1000_STATUS_LU)) ||
7285 (tx_rate < 0) || (tx_rate > actual_link_speed))
7286 return -EINVAL;
7287
7288 adapter->vf_rate_link_speed = actual_link_speed;
7289 adapter->vf_data[vf].tx_rate = (u16)tx_rate;
7290 igb_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
7291
7292 return 0;
Williams, Mitch A8151d292010-02-10 01:44:24 +00007293}
7294
7295static int igb_ndo_get_vf_config(struct net_device *netdev,
7296 int vf, struct ifla_vf_info *ivi)
7297{
7298 struct igb_adapter *adapter = netdev_priv(netdev);
7299 if (vf >= adapter->vfs_allocated_count)
7300 return -EINVAL;
7301 ivi->vf = vf;
7302 memcpy(&ivi->mac, adapter->vf_data[vf].vf_mac_addresses, ETH_ALEN);
Lior Levy17dc5662011-02-08 02:28:46 +00007303 ivi->tx_rate = adapter->vf_data[vf].tx_rate;
Williams, Mitch A8151d292010-02-10 01:44:24 +00007304 ivi->vlan = adapter->vf_data[vf].pf_vlan;
7305 ivi->qos = adapter->vf_data[vf].pf_qos;
7306 return 0;
7307}
7308
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007309static void igb_vmm_control(struct igb_adapter *adapter)
7310{
7311 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck10d8e902009-10-27 15:54:04 +00007312 u32 reg;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007313
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007314 switch (hw->mac.type) {
7315 case e1000_82575:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00007316 case e1000_i210:
7317 case e1000_i211:
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007318 default:
7319 /* replication is not supported for 82575 */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007320 return;
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007321 case e1000_82576:
7322 /* notify HW that the MAC is adding vlan tags */
7323 reg = rd32(E1000_DTXCTL);
7324 reg |= E1000_DTXCTL_VLAN_ADDED;
7325 wr32(E1000_DTXCTL, reg);
7326 case e1000_82580:
7327 /* enable replication vlan tag stripping */
7328 reg = rd32(E1000_RPLOLR);
7329 reg |= E1000_RPLOLR_STRVLAN;
7330 wr32(E1000_RPLOLR, reg);
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +00007331 case e1000_i350:
7332 /* none of the above registers are supported by i350 */
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007333 break;
7334 }
Alexander Duyck10d8e902009-10-27 15:54:04 +00007335
Alexander Duyckd4960302009-10-27 15:53:45 +00007336 if (adapter->vfs_allocated_count) {
7337 igb_vmdq_set_loopback_pf(hw, true);
7338 igb_vmdq_set_replication_pf(hw, true);
Greg Rose13800462010-11-06 02:08:26 +00007339 igb_vmdq_set_anti_spoofing_pf(hw, true,
7340 adapter->vfs_allocated_count);
Alexander Duyckd4960302009-10-27 15:53:45 +00007341 } else {
7342 igb_vmdq_set_loopback_pf(hw, false);
7343 igb_vmdq_set_replication_pf(hw, false);
7344 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007345}
7346
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007347static void igb_init_dmac(struct igb_adapter *adapter, u32 pba)
7348{
7349 struct e1000_hw *hw = &adapter->hw;
7350 u32 dmac_thr;
7351 u16 hwm;
7352
7353 if (hw->mac.type > e1000_82580) {
7354 if (adapter->flags & IGB_FLAG_DMAC) {
7355 u32 reg;
7356
7357 /* force threshold to 0. */
7358 wr32(E1000_DMCTXTH, 0);
7359
7360 /*
Matthew Vicke8c626e2011-11-17 08:33:12 +00007361 * DMA Coalescing high water mark needs to be greater
7362 * than the Rx threshold. Set hwm to PBA - max frame
7363 * size in 16B units, capping it at PBA - 6KB.
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007364 */
Matthew Vicke8c626e2011-11-17 08:33:12 +00007365 hwm = 64 * pba - adapter->max_frame_size / 16;
7366 if (hwm < 64 * (pba - 6))
7367 hwm = 64 * (pba - 6);
7368 reg = rd32(E1000_FCRTC);
7369 reg &= ~E1000_FCRTC_RTH_COAL_MASK;
7370 reg |= ((hwm << E1000_FCRTC_RTH_COAL_SHIFT)
7371 & E1000_FCRTC_RTH_COAL_MASK);
7372 wr32(E1000_FCRTC, reg);
7373
7374 /*
7375 * Set the DMA Coalescing Rx threshold to PBA - 2 * max
7376 * frame size, capping it at PBA - 10KB.
7377 */
7378 dmac_thr = pba - adapter->max_frame_size / 512;
7379 if (dmac_thr < pba - 10)
7380 dmac_thr = pba - 10;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007381 reg = rd32(E1000_DMACR);
7382 reg &= ~E1000_DMACR_DMACTHR_MASK;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007383 reg |= ((dmac_thr << E1000_DMACR_DMACTHR_SHIFT)
7384 & E1000_DMACR_DMACTHR_MASK);
7385
7386 /* transition to L0x or L1 if available..*/
7387 reg |= (E1000_DMACR_DMAC_EN | E1000_DMACR_DMAC_LX_MASK);
7388
7389 /* watchdog timer= +-1000 usec in 32usec intervals */
7390 reg |= (1000 >> 5);
Matthew Vick0c02dd92012-04-14 05:20:32 +00007391
7392 /* Disable BMC-to-OS Watchdog Enable */
7393 reg &= ~E1000_DMACR_DC_BMC2OSW_EN;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007394 wr32(E1000_DMACR, reg);
7395
7396 /*
7397 * no lower threshold to disable
7398 * coalescing(smart fifb)-UTRESH=0
7399 */
7400 wr32(E1000_DMCRTRH, 0);
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007401
7402 reg = (IGB_DMCTLX_DCFLUSH_DIS | 0x4);
7403
7404 wr32(E1000_DMCTLX, reg);
7405
7406 /*
7407 * free space in tx packet buffer to wake from
7408 * DMA coal
7409 */
7410 wr32(E1000_DMCTXTH, (IGB_MIN_TXPBSIZE -
7411 (IGB_TX_BUF_4096 + adapter->max_frame_size)) >> 6);
7412
7413 /*
7414 * make low power state decision controlled
7415 * by DMA coal
7416 */
7417 reg = rd32(E1000_PCIEMISC);
7418 reg &= ~E1000_PCIEMISC_LX_DECISION;
7419 wr32(E1000_PCIEMISC, reg);
7420 } /* endif adapter->dmac is not disabled */
7421 } else if (hw->mac.type == e1000_82580) {
7422 u32 reg = rd32(E1000_PCIEMISC);
7423 wr32(E1000_PCIEMISC, reg & ~E1000_PCIEMISC_LX_DECISION);
7424 wr32(E1000_DMACR, 0);
7425 }
7426}
7427
Auke Kok9d5c8242008-01-24 02:22:38 -08007428/* igb_main.c */