blob: 615b68c0650595ab74979e78a03bf82f16128e4e [file] [log] [blame]
Auke Kok9d5c8242008-01-24 02:22:38 -08001/*******************************************************************************
2
3 Intel(R) Gigabit Ethernet Linux driver
Carolyn Wyborny6e861322012-01-18 22:13:27 +00004 Copyright(c) 2007-2012 Intel Corporation.
Auke Kok9d5c8242008-01-24 02:22:38 -08005
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21
22 Contact Information:
23 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25
26*******************************************************************************/
27
Jeff Kirsher876d2d62011-10-21 20:01:34 +000028#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29
Auke Kok9d5c8242008-01-24 02:22:38 -080030#include <linux/module.h>
31#include <linux/types.h>
32#include <linux/init.h>
Jiri Pirkob2cb09b2011-07-21 03:27:27 +000033#include <linux/bitops.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080034#include <linux/vmalloc.h>
35#include <linux/pagemap.h>
36#include <linux/netdevice.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080037#include <linux/ipv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080039#include <net/checksum.h>
40#include <net/ip6_checksum.h>
Patrick Ohlyc6cb0902009-02-12 05:03:42 +000041#include <linux/net_tstamp.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080042#include <linux/mii.h>
43#include <linux/ethtool.h>
Jiri Pirko01789342011-08-16 06:29:00 +000044#include <linux/if.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080045#include <linux/if_vlan.h>
46#include <linux/pci.h>
Alexander Duyckc54106b2008-10-16 21:26:57 -070047#include <linux/pci-aspm.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080048#include <linux/delay.h>
49#include <linux/interrupt.h>
Alexander Duyck7d13a7d2011-08-26 07:44:32 +000050#include <linux/ip.h>
51#include <linux/tcp.h>
52#include <linux/sctp.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080053#include <linux/if_ether.h>
Alexander Duyck40a914f2008-11-27 00:24:37 -080054#include <linux/aer.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040055#include <linux/prefetch.h>
Yan, Zheng749ab2c2012-01-04 20:23:37 +000056#include <linux/pm_runtime.h>
Jeff Kirsher421e02f2008-10-17 11:08:31 -070057#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -070058#include <linux/dca.h>
59#endif
Auke Kok9d5c8242008-01-24 02:22:38 -080060#include "igb.h"
61
Carolyn Wyborny200e5fd2012-05-31 23:39:30 +000062#define MAJ 4
63#define MIN 0
Carolyn Wyborny3db73802012-10-17 07:01:56 +000064#define BUILD 17
Carolyn Wyborny0d1fe822011-03-11 20:58:19 -080065#define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) "." \
Carolyn Wyborny929dd042011-05-26 03:02:26 +000066__stringify(BUILD) "-k"
Auke Kok9d5c8242008-01-24 02:22:38 -080067char igb_driver_name[] = "igb";
68char igb_driver_version[] = DRV_VERSION;
69static const char igb_driver_string[] =
70 "Intel(R) Gigabit Ethernet Network Driver";
Carolyn Wyborny6e861322012-01-18 22:13:27 +000071static const char igb_copyright[] = "Copyright (c) 2007-2012 Intel Corporation.";
Auke Kok9d5c8242008-01-24 02:22:38 -080072
Auke Kok9d5c8242008-01-24 02:22:38 -080073static const struct e1000_info *igb_info_tbl[] = {
74 [board_82575] = &e1000_82575_info,
75};
76
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000077static DEFINE_PCI_DEVICE_TABLE(igb_pci_tbl) = {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +000078 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I211_COPPER), board_82575 },
79 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER), board_82575 },
80 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_FIBER), board_82575 },
81 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES), board_82575 },
82 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SGMII), board_82575 },
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +000083 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_COPPER), board_82575 },
84 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_FIBER), board_82575 },
85 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SERDES), board_82575 },
86 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SGMII), board_82575 },
Alexander Duyck55cac242009-11-19 12:42:21 +000087 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER), board_82575 },
88 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_FIBER), board_82575 },
Carolyn Wyborny6493d242011-01-14 05:33:46 +000089 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_QUAD_FIBER), board_82575 },
Alexander Duyck55cac242009-11-19 12:42:21 +000090 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SERDES), board_82575 },
91 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SGMII), board_82575 },
92 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER_DUAL), board_82575 },
Joseph Gasparakis308fb392010-09-22 17:56:44 +000093 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SGMII), board_82575 },
94 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SERDES), board_82575 },
Gasparakis, Joseph1b5dda32010-12-09 01:41:01 +000095 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_BACKPLANE), board_82575 },
96 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SFP), board_82575 },
Alexander Duyck2d064c02008-07-08 15:10:12 -070097 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
Alexander Duyck9eb23412009-03-13 20:42:15 +000098 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS), board_82575 },
Alexander Duyck747d49b2009-10-05 06:33:27 +000099 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS_SERDES), board_82575 },
Alexander Duyck2d064c02008-07-08 15:10:12 -0700100 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
101 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
Alexander Duyck4703bf72009-07-23 18:09:48 +0000102 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES_QUAD), board_82575 },
Carolyn Wybornyb894fa22010-03-19 06:07:48 +0000103 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER_ET2), board_82575 },
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +0000104 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
Auke Kok9d5c8242008-01-24 02:22:38 -0800105 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
106 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
107 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
108 /* required last entry */
109 {0, }
110};
111
112MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
113
114void igb_reset(struct igb_adapter *);
115static int igb_setup_all_tx_resources(struct igb_adapter *);
116static int igb_setup_all_rx_resources(struct igb_adapter *);
117static void igb_free_all_tx_resources(struct igb_adapter *);
118static void igb_free_all_rx_resources(struct igb_adapter *);
Alexander Duyck06cf2662009-10-27 15:53:25 +0000119static void igb_setup_mrqc(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800120static int igb_probe(struct pci_dev *, const struct pci_device_id *);
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500121static void igb_remove(struct pci_dev *pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -0800122static int igb_sw_init(struct igb_adapter *);
123static int igb_open(struct net_device *);
124static int igb_close(struct net_device *);
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 *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700196#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700197static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
198static struct notifier_block dca_notifier = {
199 .notifier_call = igb_notify_dca,
200 .next = NULL,
201 .priority = 0
202};
203#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800204#ifdef CONFIG_NET_POLL_CONTROLLER
205/* for netdump / net console */
206static void igb_netpoll(struct net_device *);
207#endif
Alexander Duyck37680112009-02-19 20:40:30 -0800208#ifdef CONFIG_PCI_IOV
Alexander Duyck2a3abf62009-04-07 14:37:52 +0000209static unsigned int max_vfs = 0;
210module_param(max_vfs, uint, 0);
211MODULE_PARM_DESC(max_vfs, "Maximum number of virtual functions to allocate "
212 "per physical function");
213#endif /* CONFIG_PCI_IOV */
214
Auke Kok9d5c8242008-01-24 02:22:38 -0800215static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
216 pci_channel_state_t);
217static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
218static void igb_io_resume(struct pci_dev *);
219
Stephen Hemminger3646f0e2012-09-07 09:33:15 -0700220static const struct pci_error_handlers igb_err_handler = {
Auke Kok9d5c8242008-01-24 02:22:38 -0800221 .error_detected = igb_io_error_detected,
222 .slot_reset = igb_io_slot_reset,
223 .resume = igb_io_resume,
224};
225
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +0000226static void igb_init_dmac(struct igb_adapter *adapter, u32 pba);
Auke Kok9d5c8242008-01-24 02:22:38 -0800227
228static struct pci_driver igb_driver = {
229 .name = igb_driver_name,
230 .id_table = igb_pci_tbl,
231 .probe = igb_probe,
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500232 .remove = igb_remove,
Auke Kok9d5c8242008-01-24 02:22:38 -0800233#ifdef CONFIG_PM
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000234 .driver.pm = &igb_pm_ops,
Auke Kok9d5c8242008-01-24 02:22:38 -0800235#endif
236 .shutdown = igb_shutdown,
237 .err_handler = &igb_err_handler
238};
239
240MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
241MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
242MODULE_LICENSE("GPL");
243MODULE_VERSION(DRV_VERSION);
244
stephen hemmingerb3f4d592012-03-13 06:04:20 +0000245#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
246static int debug = -1;
247module_param(debug, int, 0);
248MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
249
Taku Izumic97ec422010-04-27 14:39:30 +0000250struct igb_reg_info {
251 u32 ofs;
252 char *name;
253};
254
255static const struct igb_reg_info igb_reg_info_tbl[] = {
256
257 /* General Registers */
258 {E1000_CTRL, "CTRL"},
259 {E1000_STATUS, "STATUS"},
260 {E1000_CTRL_EXT, "CTRL_EXT"},
261
262 /* Interrupt Registers */
263 {E1000_ICR, "ICR"},
264
265 /* RX Registers */
266 {E1000_RCTL, "RCTL"},
267 {E1000_RDLEN(0), "RDLEN"},
268 {E1000_RDH(0), "RDH"},
269 {E1000_RDT(0), "RDT"},
270 {E1000_RXDCTL(0), "RXDCTL"},
271 {E1000_RDBAL(0), "RDBAL"},
272 {E1000_RDBAH(0), "RDBAH"},
273
274 /* TX Registers */
275 {E1000_TCTL, "TCTL"},
276 {E1000_TDBAL(0), "TDBAL"},
277 {E1000_TDBAH(0), "TDBAH"},
278 {E1000_TDLEN(0), "TDLEN"},
279 {E1000_TDH(0), "TDH"},
280 {E1000_TDT(0), "TDT"},
281 {E1000_TXDCTL(0), "TXDCTL"},
282 {E1000_TDFH, "TDFH"},
283 {E1000_TDFT, "TDFT"},
284 {E1000_TDFHS, "TDFHS"},
285 {E1000_TDFPC, "TDFPC"},
286
287 /* List Terminator */
288 {}
289};
290
291/*
292 * igb_regdump - register printout routine
293 */
294static void igb_regdump(struct e1000_hw *hw, struct igb_reg_info *reginfo)
295{
296 int n = 0;
297 char rname[16];
298 u32 regs[8];
299
300 switch (reginfo->ofs) {
301 case E1000_RDLEN(0):
302 for (n = 0; n < 4; n++)
303 regs[n] = rd32(E1000_RDLEN(n));
304 break;
305 case E1000_RDH(0):
306 for (n = 0; n < 4; n++)
307 regs[n] = rd32(E1000_RDH(n));
308 break;
309 case E1000_RDT(0):
310 for (n = 0; n < 4; n++)
311 regs[n] = rd32(E1000_RDT(n));
312 break;
313 case E1000_RXDCTL(0):
314 for (n = 0; n < 4; n++)
315 regs[n] = rd32(E1000_RXDCTL(n));
316 break;
317 case E1000_RDBAL(0):
318 for (n = 0; n < 4; n++)
319 regs[n] = rd32(E1000_RDBAL(n));
320 break;
321 case E1000_RDBAH(0):
322 for (n = 0; n < 4; n++)
323 regs[n] = rd32(E1000_RDBAH(n));
324 break;
325 case E1000_TDBAL(0):
326 for (n = 0; n < 4; n++)
327 regs[n] = rd32(E1000_RDBAL(n));
328 break;
329 case E1000_TDBAH(0):
330 for (n = 0; n < 4; n++)
331 regs[n] = rd32(E1000_TDBAH(n));
332 break;
333 case E1000_TDLEN(0):
334 for (n = 0; n < 4; n++)
335 regs[n] = rd32(E1000_TDLEN(n));
336 break;
337 case E1000_TDH(0):
338 for (n = 0; n < 4; n++)
339 regs[n] = rd32(E1000_TDH(n));
340 break;
341 case E1000_TDT(0):
342 for (n = 0; n < 4; n++)
343 regs[n] = rd32(E1000_TDT(n));
344 break;
345 case E1000_TXDCTL(0):
346 for (n = 0; n < 4; n++)
347 regs[n] = rd32(E1000_TXDCTL(n));
348 break;
349 default:
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000350 pr_info("%-15s %08x\n", reginfo->name, rd32(reginfo->ofs));
Taku Izumic97ec422010-04-27 14:39:30 +0000351 return;
352 }
353
354 snprintf(rname, 16, "%s%s", reginfo->name, "[0-3]");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000355 pr_info("%-15s %08x %08x %08x %08x\n", rname, regs[0], regs[1],
356 regs[2], regs[3]);
Taku Izumic97ec422010-04-27 14:39:30 +0000357}
358
359/*
360 * igb_dump - Print registers, tx-rings and rx-rings
361 */
362static void igb_dump(struct igb_adapter *adapter)
363{
364 struct net_device *netdev = adapter->netdev;
365 struct e1000_hw *hw = &adapter->hw;
366 struct igb_reg_info *reginfo;
Taku Izumic97ec422010-04-27 14:39:30 +0000367 struct igb_ring *tx_ring;
368 union e1000_adv_tx_desc *tx_desc;
369 struct my_u0 { u64 a; u64 b; } *u0;
Taku Izumic97ec422010-04-27 14:39:30 +0000370 struct igb_ring *rx_ring;
371 union e1000_adv_rx_desc *rx_desc;
372 u32 staterr;
Alexander Duyck6ad4edf2011-08-26 07:45:26 +0000373 u16 i, n;
Taku Izumic97ec422010-04-27 14:39:30 +0000374
375 if (!netif_msg_hw(adapter))
376 return;
377
378 /* Print netdevice Info */
379 if (netdev) {
380 dev_info(&adapter->pdev->dev, "Net device Info\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000381 pr_info("Device Name state trans_start "
382 "last_rx\n");
383 pr_info("%-15s %016lX %016lX %016lX\n", netdev->name,
384 netdev->state, netdev->trans_start, netdev->last_rx);
Taku Izumic97ec422010-04-27 14:39:30 +0000385 }
386
387 /* Print Registers */
388 dev_info(&adapter->pdev->dev, "Register Dump\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000389 pr_info(" Register Name Value\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000390 for (reginfo = (struct igb_reg_info *)igb_reg_info_tbl;
391 reginfo->name; reginfo++) {
392 igb_regdump(hw, reginfo);
393 }
394
395 /* Print TX Ring Summary */
396 if (!netdev || !netif_running(netdev))
397 goto exit;
398
399 dev_info(&adapter->pdev->dev, "TX Rings Summary\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000400 pr_info("Queue [NTU] [NTC] [bi(ntc)->dma ] leng ntw timestamp\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000401 for (n = 0; n < adapter->num_tx_queues; n++) {
Alexander Duyck06034642011-08-26 07:44:22 +0000402 struct igb_tx_buffer *buffer_info;
Taku Izumic97ec422010-04-27 14:39:30 +0000403 tx_ring = adapter->tx_ring[n];
Alexander Duyck06034642011-08-26 07:44:22 +0000404 buffer_info = &tx_ring->tx_buffer_info[tx_ring->next_to_clean];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000405 pr_info(" %5d %5X %5X %016llX %04X %p %016llX\n",
406 n, tx_ring->next_to_use, tx_ring->next_to_clean,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000407 (u64)dma_unmap_addr(buffer_info, dma),
408 dma_unmap_len(buffer_info, len),
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000409 buffer_info->next_to_watch,
410 (u64)buffer_info->time_stamp);
Taku Izumic97ec422010-04-27 14:39:30 +0000411 }
412
413 /* Print TX Rings */
414 if (!netif_msg_tx_done(adapter))
415 goto rx_ring_summary;
416
417 dev_info(&adapter->pdev->dev, "TX Rings Dump\n");
418
419 /* Transmit Descriptor Formats
420 *
421 * Advanced Transmit Descriptor
422 * +--------------------------------------------------------------+
423 * 0 | Buffer Address [63:0] |
424 * +--------------------------------------------------------------+
425 * 8 | PAYLEN | PORTS |CC|IDX | STA | DCMD |DTYP|MAC|RSV| DTALEN |
426 * +--------------------------------------------------------------+
427 * 63 46 45 40 39 38 36 35 32 31 24 15 0
428 */
429
430 for (n = 0; n < adapter->num_tx_queues; n++) {
431 tx_ring = adapter->tx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000432 pr_info("------------------------------------\n");
433 pr_info("TX QUEUE INDEX = %d\n", tx_ring->queue_index);
434 pr_info("------------------------------------\n");
435 pr_info("T [desc] [address 63:0 ] [PlPOCIStDDM Ln] "
436 "[bi->dma ] leng ntw timestamp "
437 "bi->skb\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000438
439 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000440 const char *next_desc;
Alexander Duyck06034642011-08-26 07:44:22 +0000441 struct igb_tx_buffer *buffer_info;
Alexander Duyck601369062011-08-26 07:44:05 +0000442 tx_desc = IGB_TX_DESC(tx_ring, i);
Alexander Duyck06034642011-08-26 07:44:22 +0000443 buffer_info = &tx_ring->tx_buffer_info[i];
Taku Izumic97ec422010-04-27 14:39:30 +0000444 u0 = (struct my_u0 *)tx_desc;
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000445 if (i == tx_ring->next_to_use &&
446 i == tx_ring->next_to_clean)
447 next_desc = " NTC/U";
448 else if (i == tx_ring->next_to_use)
449 next_desc = " NTU";
450 else if (i == tx_ring->next_to_clean)
451 next_desc = " NTC";
452 else
453 next_desc = "";
454
455 pr_info("T [0x%03X] %016llX %016llX %016llX"
456 " %04X %p %016llX %p%s\n", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000457 le64_to_cpu(u0->a),
458 le64_to_cpu(u0->b),
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000459 (u64)dma_unmap_addr(buffer_info, dma),
460 dma_unmap_len(buffer_info, len),
Taku Izumic97ec422010-04-27 14:39:30 +0000461 buffer_info->next_to_watch,
462 (u64)buffer_info->time_stamp,
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000463 buffer_info->skb, next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000464
Emil Tantilovb6695882012-07-28 05:07:48 +0000465 if (netif_msg_pktdata(adapter) && buffer_info->skb)
Taku Izumic97ec422010-04-27 14:39:30 +0000466 print_hex_dump(KERN_INFO, "",
467 DUMP_PREFIX_ADDRESS,
Emil Tantilovb6695882012-07-28 05:07:48 +0000468 16, 1, buffer_info->skb->data,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000469 dma_unmap_len(buffer_info, len),
470 true);
Taku Izumic97ec422010-04-27 14:39:30 +0000471 }
472 }
473
474 /* Print RX Rings Summary */
475rx_ring_summary:
476 dev_info(&adapter->pdev->dev, "RX Rings Summary\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000477 pr_info("Queue [NTU] [NTC]\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000478 for (n = 0; n < adapter->num_rx_queues; n++) {
479 rx_ring = adapter->rx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000480 pr_info(" %5d %5X %5X\n",
481 n, rx_ring->next_to_use, rx_ring->next_to_clean);
Taku Izumic97ec422010-04-27 14:39:30 +0000482 }
483
484 /* Print RX Rings */
485 if (!netif_msg_rx_status(adapter))
486 goto exit;
487
488 dev_info(&adapter->pdev->dev, "RX Rings Dump\n");
489
490 /* Advanced Receive Descriptor (Read) Format
491 * 63 1 0
492 * +-----------------------------------------------------+
493 * 0 | Packet Buffer Address [63:1] |A0/NSE|
494 * +----------------------------------------------+------+
495 * 8 | Header Buffer Address [63:1] | DD |
496 * +-----------------------------------------------------+
497 *
498 *
499 * Advanced Receive Descriptor (Write-Back) Format
500 *
501 * 63 48 47 32 31 30 21 20 17 16 4 3 0
502 * +------------------------------------------------------+
503 * 0 | Packet IP |SPH| HDR_LEN | RSV|Packet| RSS |
504 * | Checksum Ident | | | | Type | Type |
505 * +------------------------------------------------------+
506 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
507 * +------------------------------------------------------+
508 * 63 48 47 32 31 20 19 0
509 */
510
511 for (n = 0; n < adapter->num_rx_queues; n++) {
512 rx_ring = adapter->rx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000513 pr_info("------------------------------------\n");
514 pr_info("RX QUEUE INDEX = %d\n", rx_ring->queue_index);
515 pr_info("------------------------------------\n");
516 pr_info("R [desc] [ PktBuf A0] [ HeadBuf DD] "
517 "[bi->dma ] [bi->skb] <-- Adv Rx Read format\n");
518 pr_info("RWB[desc] [PcsmIpSHl PtRs] [vl er S cks ln] -----"
519 "----------- [bi->skb] <-- Adv Rx Write-Back format\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000520
521 for (i = 0; i < rx_ring->count; i++) {
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000522 const char *next_desc;
Alexander Duyck06034642011-08-26 07:44:22 +0000523 struct igb_rx_buffer *buffer_info;
524 buffer_info = &rx_ring->rx_buffer_info[i];
Alexander Duyck601369062011-08-26 07:44:05 +0000525 rx_desc = IGB_RX_DESC(rx_ring, i);
Taku Izumic97ec422010-04-27 14:39:30 +0000526 u0 = (struct my_u0 *)rx_desc;
527 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000528
529 if (i == rx_ring->next_to_use)
530 next_desc = " NTU";
531 else if (i == rx_ring->next_to_clean)
532 next_desc = " NTC";
533 else
534 next_desc = "";
535
Taku Izumic97ec422010-04-27 14:39:30 +0000536 if (staterr & E1000_RXD_STAT_DD) {
537 /* Descriptor Done */
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000538 pr_info("%s[0x%03X] %016llX %016llX ---------------- %s\n",
539 "RWB", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000540 le64_to_cpu(u0->a),
541 le64_to_cpu(u0->b),
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000542 next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000543 } else {
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000544 pr_info("%s[0x%03X] %016llX %016llX %016llX %s\n",
545 "R ", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000546 le64_to_cpu(u0->a),
547 le64_to_cpu(u0->b),
548 (u64)buffer_info->dma,
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000549 next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000550
Emil Tantilovb6695882012-07-28 05:07:48 +0000551 if (netif_msg_pktdata(adapter) &&
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000552 buffer_info->dma && buffer_info->page) {
Alexander Duyck44390ca2011-08-26 07:43:38 +0000553 print_hex_dump(KERN_INFO, "",
554 DUMP_PREFIX_ADDRESS,
555 16, 1,
Emil Tantilovb6695882012-07-28 05:07:48 +0000556 page_address(buffer_info->page) +
557 buffer_info->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +0000558 IGB_RX_BUFSZ, true);
Taku Izumic97ec422010-04-27 14:39:30 +0000559 }
560 }
Taku Izumic97ec422010-04-27 14:39:30 +0000561 }
562 }
563
564exit:
565 return;
566}
567
Auke Kok9d5c8242008-01-24 02:22:38 -0800568/**
Alexander Duyckc0410762010-03-25 13:10:08 +0000569 * igb_get_hw_dev - return device
Auke Kok9d5c8242008-01-24 02:22:38 -0800570 * used by hardware layer to print debugging information
571 **/
Alexander Duyckc0410762010-03-25 13:10:08 +0000572struct net_device *igb_get_hw_dev(struct e1000_hw *hw)
Auke Kok9d5c8242008-01-24 02:22:38 -0800573{
574 struct igb_adapter *adapter = hw->back;
Alexander Duyckc0410762010-03-25 13:10:08 +0000575 return adapter->netdev;
Auke Kok9d5c8242008-01-24 02:22:38 -0800576}
Patrick Ohly38c845c2009-02-12 05:03:41 +0000577
578/**
Auke Kok9d5c8242008-01-24 02:22:38 -0800579 * igb_init_module - Driver Registration Routine
580 *
581 * igb_init_module is the first routine called when the driver is
582 * loaded. All it does is register with the PCI subsystem.
583 **/
584static int __init igb_init_module(void)
585{
586 int ret;
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000587 pr_info("%s - version %s\n",
Auke Kok9d5c8242008-01-24 02:22:38 -0800588 igb_driver_string, igb_driver_version);
589
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000590 pr_info("%s\n", igb_copyright);
Auke Kok9d5c8242008-01-24 02:22:38 -0800591
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700592#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700593 dca_register_notify(&dca_notifier);
594#endif
Alexander Duyckbbd98fe2009-01-31 00:52:30 -0800595 ret = pci_register_driver(&igb_driver);
Auke Kok9d5c8242008-01-24 02:22:38 -0800596 return ret;
597}
598
599module_init(igb_init_module);
600
601/**
602 * igb_exit_module - Driver Exit Cleanup Routine
603 *
604 * igb_exit_module is called just before the driver is removed
605 * from memory.
606 **/
607static void __exit igb_exit_module(void)
608{
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700609#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700610 dca_unregister_notify(&dca_notifier);
611#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800612 pci_unregister_driver(&igb_driver);
613}
614
615module_exit(igb_exit_module);
616
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800617#define Q_IDX_82576(i) (((i & 0x1) << 3) + (i >> 1))
618/**
619 * igb_cache_ring_register - Descriptor ring to register mapping
620 * @adapter: board private structure to initialize
621 *
622 * Once we know the feature-set enabled for the device, we'll cache
623 * the register offset the descriptor ring is assigned to.
624 **/
625static void igb_cache_ring_register(struct igb_adapter *adapter)
626{
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000627 int i = 0, j = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000628 u32 rbase_offset = adapter->vfs_allocated_count;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800629
630 switch (adapter->hw.mac.type) {
631 case e1000_82576:
632 /* The queues are allocated for virtualization such that VF 0
633 * is allocated queues 0 and 8, VF 1 queues 1 and 9, etc.
634 * In order to avoid collision we start at the first free queue
635 * and continue consuming queues in the same sequence
636 */
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000637 if (adapter->vfs_allocated_count) {
Alexander Duycka99955f2009-11-12 18:37:19 +0000638 for (; i < adapter->rss_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000639 adapter->rx_ring[i]->reg_idx = rbase_offset +
640 Q_IDX_82576(i);
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000641 }
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800642 case e1000_82575:
Alexander Duyck55cac242009-11-19 12:42:21 +0000643 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000644 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000645 case e1000_i210:
646 case e1000_i211:
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800647 default:
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000648 for (; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000649 adapter->rx_ring[i]->reg_idx = rbase_offset + i;
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000650 for (; j < adapter->num_tx_queues; j++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000651 adapter->tx_ring[j]->reg_idx = rbase_offset + j;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800652 break;
653 }
654}
655
Alexander Duyck4be000c2011-08-26 07:45:52 +0000656/**
657 * igb_write_ivar - configure ivar for given MSI-X vector
658 * @hw: pointer to the HW structure
659 * @msix_vector: vector number we are allocating to a given ring
660 * @index: row index of IVAR register to write within IVAR table
661 * @offset: column offset of in IVAR, should be multiple of 8
662 *
663 * This function is intended to handle the writing of the IVAR register
664 * for adapters 82576 and newer. The IVAR table consists of 2 columns,
665 * each containing an cause allocation for an Rx and Tx ring, and a
666 * variable number of rows depending on the number of queues supported.
667 **/
668static void igb_write_ivar(struct e1000_hw *hw, int msix_vector,
669 int index, int offset)
670{
671 u32 ivar = array_rd32(E1000_IVAR0, index);
672
673 /* clear any bits that are currently set */
674 ivar &= ~((u32)0xFF << offset);
675
676 /* write vector and valid bit */
677 ivar |= (msix_vector | E1000_IVAR_VALID) << offset;
678
679 array_wr32(E1000_IVAR0, index, ivar);
680}
681
Auke Kok9d5c8242008-01-24 02:22:38 -0800682#define IGB_N0_QUEUE -1
Alexander Duyck047e0032009-10-27 15:49:27 +0000683static void igb_assign_vector(struct igb_q_vector *q_vector, int msix_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -0800684{
Alexander Duyck047e0032009-10-27 15:49:27 +0000685 struct igb_adapter *adapter = q_vector->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -0800686 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck047e0032009-10-27 15:49:27 +0000687 int rx_queue = IGB_N0_QUEUE;
688 int tx_queue = IGB_N0_QUEUE;
Alexander Duyck4be000c2011-08-26 07:45:52 +0000689 u32 msixbm = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000690
Alexander Duyck0ba82992011-08-26 07:45:47 +0000691 if (q_vector->rx.ring)
692 rx_queue = q_vector->rx.ring->reg_idx;
693 if (q_vector->tx.ring)
694 tx_queue = q_vector->tx.ring->reg_idx;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700695
696 switch (hw->mac.type) {
697 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800698 /* The 82575 assigns vectors using a bitmask, which matches the
699 bitmask for the EICR/EIMS/EIMC registers. To assign one
700 or more queues to a vector, we write the appropriate bits
701 into the MSIXBM register for that vector. */
Alexander Duyck047e0032009-10-27 15:49:27 +0000702 if (rx_queue > IGB_N0_QUEUE)
Auke Kok9d5c8242008-01-24 02:22:38 -0800703 msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
Alexander Duyck047e0032009-10-27 15:49:27 +0000704 if (tx_queue > IGB_N0_QUEUE)
Auke Kok9d5c8242008-01-24 02:22:38 -0800705 msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
Alexander Duyckfeeb2722010-02-03 21:59:51 +0000706 if (!adapter->msix_entries && msix_vector == 0)
707 msixbm |= E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800708 array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
Alexander Duyck047e0032009-10-27 15:49:27 +0000709 q_vector->eims_value = msixbm;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700710 break;
711 case e1000_82576:
Alexander Duyck4be000c2011-08-26 07:45:52 +0000712 /*
713 * 82576 uses a table that essentially consists of 2 columns
714 * with 8 rows. The ordering is column-major so we use the
715 * lower 3 bits as the row index, and the 4th bit as the
716 * column offset.
717 */
718 if (rx_queue > IGB_N0_QUEUE)
719 igb_write_ivar(hw, msix_vector,
720 rx_queue & 0x7,
721 (rx_queue & 0x8) << 1);
722 if (tx_queue > IGB_N0_QUEUE)
723 igb_write_ivar(hw, msix_vector,
724 tx_queue & 0x7,
725 ((tx_queue & 0x8) << 1) + 8);
Alexander Duyck047e0032009-10-27 15:49:27 +0000726 q_vector->eims_value = 1 << msix_vector;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700727 break;
Alexander Duyck55cac242009-11-19 12:42:21 +0000728 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000729 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000730 case e1000_i210:
731 case e1000_i211:
Alexander Duyck4be000c2011-08-26 07:45:52 +0000732 /*
733 * On 82580 and newer adapters the scheme is similar to 82576
734 * however instead of ordering column-major we have things
735 * ordered row-major. So we traverse the table by using
736 * bit 0 as the column offset, and the remaining bits as the
737 * row index.
738 */
739 if (rx_queue > IGB_N0_QUEUE)
740 igb_write_ivar(hw, msix_vector,
741 rx_queue >> 1,
742 (rx_queue & 0x1) << 4);
743 if (tx_queue > IGB_N0_QUEUE)
744 igb_write_ivar(hw, msix_vector,
745 tx_queue >> 1,
746 ((tx_queue & 0x1) << 4) + 8);
Alexander Duyck55cac242009-11-19 12:42:21 +0000747 q_vector->eims_value = 1 << msix_vector;
748 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700749 default:
750 BUG();
751 break;
752 }
Alexander Duyck26b39272010-02-17 01:00:41 +0000753
754 /* add q_vector eims value to global eims_enable_mask */
755 adapter->eims_enable_mask |= q_vector->eims_value;
756
757 /* configure q_vector to set itr on first interrupt */
758 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -0800759}
760
761/**
762 * igb_configure_msix - Configure MSI-X hardware
763 *
764 * igb_configure_msix sets up the hardware to properly
765 * generate MSI-X interrupts.
766 **/
767static void igb_configure_msix(struct igb_adapter *adapter)
768{
769 u32 tmp;
770 int i, vector = 0;
771 struct e1000_hw *hw = &adapter->hw;
772
773 adapter->eims_enable_mask = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -0800774
775 /* set vector for other causes, i.e. link changes */
Alexander Duyck2d064c02008-07-08 15:10:12 -0700776 switch (hw->mac.type) {
777 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800778 tmp = rd32(E1000_CTRL_EXT);
779 /* enable MSI-X PBA support*/
780 tmp |= E1000_CTRL_EXT_PBA_CLR;
781
782 /* Auto-Mask interrupts upon ICR read. */
783 tmp |= E1000_CTRL_EXT_EIAME;
784 tmp |= E1000_CTRL_EXT_IRCA;
785
786 wr32(E1000_CTRL_EXT, tmp);
Alexander Duyck047e0032009-10-27 15:49:27 +0000787
788 /* enable msix_other interrupt */
789 array_wr32(E1000_MSIXBM(0), vector++,
790 E1000_EIMS_OTHER);
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700791 adapter->eims_other = E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800792
Alexander Duyck2d064c02008-07-08 15:10:12 -0700793 break;
794
795 case e1000_82576:
Alexander Duyck55cac242009-11-19 12:42:21 +0000796 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000797 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000798 case e1000_i210:
799 case e1000_i211:
Alexander Duyck047e0032009-10-27 15:49:27 +0000800 /* Turn on MSI-X capability first, or our settings
801 * won't stick. And it will take days to debug. */
802 wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
803 E1000_GPIE_PBA | E1000_GPIE_EIAME |
804 E1000_GPIE_NSICR);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700805
Alexander Duyck047e0032009-10-27 15:49:27 +0000806 /* enable msix_other interrupt */
807 adapter->eims_other = 1 << vector;
808 tmp = (vector++ | E1000_IVAR_VALID) << 8;
809
810 wr32(E1000_IVAR_MISC, tmp);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700811 break;
812 default:
813 /* do nothing, since nothing else supports MSI-X */
814 break;
815 } /* switch (hw->mac.type) */
Alexander Duyck047e0032009-10-27 15:49:27 +0000816
817 adapter->eims_enable_mask |= adapter->eims_other;
818
Alexander Duyck26b39272010-02-17 01:00:41 +0000819 for (i = 0; i < adapter->num_q_vectors; i++)
820 igb_assign_vector(adapter->q_vector[i], vector++);
Alexander Duyck047e0032009-10-27 15:49:27 +0000821
Auke Kok9d5c8242008-01-24 02:22:38 -0800822 wrfl();
823}
824
825/**
826 * igb_request_msix - Initialize MSI-X interrupts
827 *
828 * igb_request_msix allocates MSI-X vectors and requests interrupts from the
829 * kernel.
830 **/
831static int igb_request_msix(struct igb_adapter *adapter)
832{
833 struct net_device *netdev = adapter->netdev;
Alexander Duyck047e0032009-10-27 15:49:27 +0000834 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -0800835 int i, err = 0, vector = 0;
836
Auke Kok9d5c8242008-01-24 02:22:38 -0800837 err = request_irq(adapter->msix_entries[vector].vector,
Joe Perchesa0607fd2009-11-18 23:29:17 -0800838 igb_msix_other, 0, netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800839 if (err)
840 goto out;
Alexander Duyck047e0032009-10-27 15:49:27 +0000841 vector++;
842
843 for (i = 0; i < adapter->num_q_vectors; i++) {
844 struct igb_q_vector *q_vector = adapter->q_vector[i];
845
846 q_vector->itr_register = hw->hw_addr + E1000_EITR(vector);
847
Alexander Duyck0ba82992011-08-26 07:45:47 +0000848 if (q_vector->rx.ring && q_vector->tx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000849 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000850 q_vector->rx.ring->queue_index);
851 else if (q_vector->tx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000852 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000853 q_vector->tx.ring->queue_index);
854 else if (q_vector->rx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000855 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000856 q_vector->rx.ring->queue_index);
Alexander Duyck047e0032009-10-27 15:49:27 +0000857 else
858 sprintf(q_vector->name, "%s-unused", netdev->name);
859
860 err = request_irq(adapter->msix_entries[vector].vector,
Joe Perchesa0607fd2009-11-18 23:29:17 -0800861 igb_msix_ring, 0, q_vector->name,
Alexander Duyck047e0032009-10-27 15:49:27 +0000862 q_vector);
863 if (err)
864 goto out;
865 vector++;
866 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800867
Auke Kok9d5c8242008-01-24 02:22:38 -0800868 igb_configure_msix(adapter);
869 return 0;
870out:
871 return err;
872}
873
874static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
875{
876 if (adapter->msix_entries) {
877 pci_disable_msix(adapter->pdev);
878 kfree(adapter->msix_entries);
879 adapter->msix_entries = NULL;
Alexander Duyck047e0032009-10-27 15:49:27 +0000880 } else if (adapter->flags & IGB_FLAG_HAS_MSI) {
Auke Kok9d5c8242008-01-24 02:22:38 -0800881 pci_disable_msi(adapter->pdev);
Alexander Duyck047e0032009-10-27 15:49:27 +0000882 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800883}
884
Alexander Duyck047e0032009-10-27 15:49:27 +0000885/**
Alexander Duyck5536d212012-09-25 00:31:17 +0000886 * igb_free_q_vector - Free memory allocated for specific interrupt vector
887 * @adapter: board private structure to initialize
888 * @v_idx: Index of vector to be freed
889 *
890 * This function frees the memory allocated to the q_vector. In addition if
891 * NAPI is enabled it will delete any references to the NAPI struct prior
892 * to freeing the q_vector.
893 **/
894static void igb_free_q_vector(struct igb_adapter *adapter, int v_idx)
895{
896 struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
897
898 if (q_vector->tx.ring)
899 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
900
901 if (q_vector->rx.ring)
902 adapter->tx_ring[q_vector->rx.ring->queue_index] = NULL;
903
904 adapter->q_vector[v_idx] = NULL;
905 netif_napi_del(&q_vector->napi);
906
907 /*
908 * ixgbe_get_stats64() might access the rings on this vector,
909 * we must wait a grace period before freeing it.
910 */
911 kfree_rcu(q_vector, rcu);
912}
913
914/**
Alexander Duyck047e0032009-10-27 15:49:27 +0000915 * igb_free_q_vectors - Free memory allocated for interrupt vectors
916 * @adapter: board private structure to initialize
917 *
918 * This function frees the memory allocated to the q_vectors. In addition if
919 * NAPI is enabled it will delete any references to the NAPI struct prior
920 * to freeing the q_vector.
921 **/
922static void igb_free_q_vectors(struct igb_adapter *adapter)
923{
Alexander Duyck5536d212012-09-25 00:31:17 +0000924 int v_idx = adapter->num_q_vectors;
Alexander Duyck047e0032009-10-27 15:49:27 +0000925
Alexander Duyck5536d212012-09-25 00:31:17 +0000926 adapter->num_tx_queues = 0;
927 adapter->num_rx_queues = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000928 adapter->num_q_vectors = 0;
Alexander Duyck5536d212012-09-25 00:31:17 +0000929
930 while (v_idx--)
931 igb_free_q_vector(adapter, v_idx);
Alexander Duyck047e0032009-10-27 15:49:27 +0000932}
933
934/**
935 * igb_clear_interrupt_scheme - reset the device to a state of no interrupts
936 *
937 * This function resets the device so that it has 0 rx queues, tx queues, and
938 * MSI-X interrupts allocated.
939 */
940static void igb_clear_interrupt_scheme(struct igb_adapter *adapter)
941{
Alexander Duyck047e0032009-10-27 15:49:27 +0000942 igb_free_q_vectors(adapter);
943 igb_reset_interrupt_capability(adapter);
944}
Auke Kok9d5c8242008-01-24 02:22:38 -0800945
946/**
947 * igb_set_interrupt_capability - set MSI or MSI-X if supported
948 *
949 * Attempt to configure interrupts using the best available
950 * capabilities of the hardware and kernel.
951 **/
Stefan Assmann53c7d062012-12-04 06:00:12 +0000952static void igb_set_interrupt_capability(struct igb_adapter *adapter, bool msix)
Auke Kok9d5c8242008-01-24 02:22:38 -0800953{
954 int err;
955 int numvecs, i;
956
Stefan Assmann53c7d062012-12-04 06:00:12 +0000957 if (!msix)
958 goto msi_only;
959
Alexander Duyck83b71802009-02-06 23:15:45 +0000960 /* Number of supported queues. */
Alexander Duycka99955f2009-11-12 18:37:19 +0000961 adapter->num_rx_queues = adapter->rss_queues;
Greg Rose5fa85172010-07-01 13:38:16 +0000962 if (adapter->vfs_allocated_count)
963 adapter->num_tx_queues = 1;
964 else
965 adapter->num_tx_queues = adapter->rss_queues;
Alexander Duyck83b71802009-02-06 23:15:45 +0000966
Alexander Duyck047e0032009-10-27 15:49:27 +0000967 /* start with one vector for every rx queue */
968 numvecs = adapter->num_rx_queues;
969
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800970 /* if tx handler is separate add 1 for every tx queue */
Alexander Duycka99955f2009-11-12 18:37:19 +0000971 if (!(adapter->flags & IGB_FLAG_QUEUE_PAIRS))
972 numvecs += adapter->num_tx_queues;
Alexander Duyck047e0032009-10-27 15:49:27 +0000973
974 /* store the number of vectors reserved for queues */
975 adapter->num_q_vectors = numvecs;
976
977 /* add 1 vector for link status interrupts */
978 numvecs++;
Auke Kok9d5c8242008-01-24 02:22:38 -0800979 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
980 GFP_KERNEL);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000981
Auke Kok9d5c8242008-01-24 02:22:38 -0800982 if (!adapter->msix_entries)
983 goto msi_only;
984
985 for (i = 0; i < numvecs; i++)
986 adapter->msix_entries[i].entry = i;
987
988 err = pci_enable_msix(adapter->pdev,
989 adapter->msix_entries,
990 numvecs);
991 if (err == 0)
Alexander Duyck0c2cc022012-09-25 00:31:22 +0000992 return;
Auke Kok9d5c8242008-01-24 02:22:38 -0800993
994 igb_reset_interrupt_capability(adapter);
995
996 /* If we can't do MSI-X, try MSI */
997msi_only:
Alexander Duyck2a3abf62009-04-07 14:37:52 +0000998#ifdef CONFIG_PCI_IOV
999 /* disable SR-IOV for non MSI-X configurations */
1000 if (adapter->vf_data) {
1001 struct e1000_hw *hw = &adapter->hw;
1002 /* disable iov and allow time for transactions to clear */
1003 pci_disable_sriov(adapter->pdev);
1004 msleep(500);
1005
1006 kfree(adapter->vf_data);
1007 adapter->vf_data = NULL;
1008 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001009 wrfl();
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001010 msleep(100);
1011 dev_info(&adapter->pdev->dev, "IOV Disabled\n");
1012 }
1013#endif
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00001014 adapter->vfs_allocated_count = 0;
Alexander Duycka99955f2009-11-12 18:37:19 +00001015 adapter->rss_queues = 1;
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00001016 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
Auke Kok9d5c8242008-01-24 02:22:38 -08001017 adapter->num_rx_queues = 1;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001018 adapter->num_tx_queues = 1;
Alexander Duyck047e0032009-10-27 15:49:27 +00001019 adapter->num_q_vectors = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08001020 if (!pci_enable_msi(adapter->pdev))
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001021 adapter->flags |= IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -08001022}
1023
Alexander Duyck5536d212012-09-25 00:31:17 +00001024static void igb_add_ring(struct igb_ring *ring,
1025 struct igb_ring_container *head)
1026{
1027 head->ring = ring;
1028 head->count++;
1029}
1030
1031/**
1032 * igb_alloc_q_vector - Allocate memory for a single interrupt vector
1033 * @adapter: board private structure to initialize
1034 * @v_count: q_vectors allocated on adapter, used for ring interleaving
1035 * @v_idx: index of vector in adapter struct
1036 * @txr_count: total number of Tx rings to allocate
1037 * @txr_idx: index of first Tx ring to allocate
1038 * @rxr_count: total number of Rx rings to allocate
1039 * @rxr_idx: index of first Rx ring to allocate
1040 *
1041 * We allocate one q_vector. If allocation fails we return -ENOMEM.
1042 **/
1043static int igb_alloc_q_vector(struct igb_adapter *adapter,
1044 int v_count, int v_idx,
1045 int txr_count, int txr_idx,
1046 int rxr_count, int rxr_idx)
1047{
1048 struct igb_q_vector *q_vector;
1049 struct igb_ring *ring;
1050 int ring_count, size;
1051
1052 /* igb only supports 1 Tx and/or 1 Rx queue per vector */
1053 if (txr_count > 1 || rxr_count > 1)
1054 return -ENOMEM;
1055
1056 ring_count = txr_count + rxr_count;
1057 size = sizeof(struct igb_q_vector) +
1058 (sizeof(struct igb_ring) * ring_count);
1059
1060 /* allocate q_vector and rings */
1061 q_vector = kzalloc(size, GFP_KERNEL);
1062 if (!q_vector)
1063 return -ENOMEM;
1064
1065 /* initialize NAPI */
1066 netif_napi_add(adapter->netdev, &q_vector->napi,
1067 igb_poll, 64);
1068
1069 /* tie q_vector and adapter together */
1070 adapter->q_vector[v_idx] = q_vector;
1071 q_vector->adapter = adapter;
1072
1073 /* initialize work limits */
1074 q_vector->tx.work_limit = adapter->tx_work_limit;
1075
1076 /* initialize ITR configuration */
1077 q_vector->itr_register = adapter->hw.hw_addr + E1000_EITR(0);
1078 q_vector->itr_val = IGB_START_ITR;
1079
1080 /* initialize pointer to rings */
1081 ring = q_vector->ring;
1082
1083 if (txr_count) {
1084 /* assign generic ring traits */
1085 ring->dev = &adapter->pdev->dev;
1086 ring->netdev = adapter->netdev;
1087
1088 /* configure backlink on ring */
1089 ring->q_vector = q_vector;
1090
1091 /* update q_vector Tx values */
1092 igb_add_ring(ring, &q_vector->tx);
1093
1094 /* For 82575, context index must be unique per ring. */
1095 if (adapter->hw.mac.type == e1000_82575)
1096 set_bit(IGB_RING_FLAG_TX_CTX_IDX, &ring->flags);
1097
1098 /* apply Tx specific ring traits */
1099 ring->count = adapter->tx_ring_count;
1100 ring->queue_index = txr_idx;
1101
1102 /* assign ring to adapter */
1103 adapter->tx_ring[txr_idx] = ring;
1104
1105 /* push pointer to next ring */
1106 ring++;
1107 }
1108
1109 if (rxr_count) {
1110 /* assign generic ring traits */
1111 ring->dev = &adapter->pdev->dev;
1112 ring->netdev = adapter->netdev;
1113
1114 /* configure backlink on ring */
1115 ring->q_vector = q_vector;
1116
1117 /* update q_vector Rx values */
1118 igb_add_ring(ring, &q_vector->rx);
1119
1120 /* set flag indicating ring supports SCTP checksum offload */
1121 if (adapter->hw.mac.type >= e1000_82576)
1122 set_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags);
1123
1124 /*
1125 * On i350, i210, and i211, loopback VLAN packets
1126 * have the tag byte-swapped.
1127 * */
1128 if (adapter->hw.mac.type >= e1000_i350)
1129 set_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &ring->flags);
1130
1131 /* apply Rx specific ring traits */
1132 ring->count = adapter->rx_ring_count;
1133 ring->queue_index = rxr_idx;
1134
1135 /* assign ring to adapter */
1136 adapter->rx_ring[rxr_idx] = ring;
1137 }
1138
1139 return 0;
1140}
1141
1142
Auke Kok9d5c8242008-01-24 02:22:38 -08001143/**
Alexander Duyck047e0032009-10-27 15:49:27 +00001144 * igb_alloc_q_vectors - Allocate memory for interrupt vectors
1145 * @adapter: board private structure to initialize
1146 *
1147 * We allocate one q_vector per queue interrupt. If allocation fails we
1148 * return -ENOMEM.
1149 **/
1150static int igb_alloc_q_vectors(struct igb_adapter *adapter)
1151{
Alexander Duyck5536d212012-09-25 00:31:17 +00001152 int q_vectors = adapter->num_q_vectors;
1153 int rxr_remaining = adapter->num_rx_queues;
1154 int txr_remaining = adapter->num_tx_queues;
1155 int rxr_idx = 0, txr_idx = 0, v_idx = 0;
1156 int err;
Alexander Duyck047e0032009-10-27 15:49:27 +00001157
Alexander Duyck5536d212012-09-25 00:31:17 +00001158 if (q_vectors >= (rxr_remaining + txr_remaining)) {
1159 for (; rxr_remaining; v_idx++) {
1160 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1161 0, 0, 1, rxr_idx);
1162
1163 if (err)
1164 goto err_out;
1165
1166 /* update counts and index */
1167 rxr_remaining--;
1168 rxr_idx++;
1169 }
1170 }
1171
1172 for (; v_idx < q_vectors; v_idx++) {
1173 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
1174 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
1175 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1176 tqpv, txr_idx, rqpv, rxr_idx);
1177
1178 if (err)
Alexander Duyck047e0032009-10-27 15:49:27 +00001179 goto err_out;
Alexander Duyck5536d212012-09-25 00:31:17 +00001180
1181 /* update counts and index */
1182 rxr_remaining -= rqpv;
1183 txr_remaining -= tqpv;
1184 rxr_idx++;
1185 txr_idx++;
Alexander Duyck047e0032009-10-27 15:49:27 +00001186 }
Alexander Duyck81c2fc22011-08-26 07:45:20 +00001187
Alexander Duyck047e0032009-10-27 15:49:27 +00001188 return 0;
1189
1190err_out:
Alexander Duyck5536d212012-09-25 00:31:17 +00001191 adapter->num_tx_queues = 0;
1192 adapter->num_rx_queues = 0;
1193 adapter->num_q_vectors = 0;
1194
1195 while (v_idx--)
1196 igb_free_q_vector(adapter, v_idx);
1197
Alexander Duyck047e0032009-10-27 15:49:27 +00001198 return -ENOMEM;
1199}
1200
Alexander Duyck047e0032009-10-27 15:49:27 +00001201/**
1202 * igb_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
1203 *
1204 * This function initializes the interrupts and allocates all of the queues.
1205 **/
Stefan Assmann53c7d062012-12-04 06:00:12 +00001206static int igb_init_interrupt_scheme(struct igb_adapter *adapter, bool msix)
Alexander Duyck047e0032009-10-27 15:49:27 +00001207{
1208 struct pci_dev *pdev = adapter->pdev;
1209 int err;
1210
Stefan Assmann53c7d062012-12-04 06:00:12 +00001211 igb_set_interrupt_capability(adapter, msix);
Alexander Duyck047e0032009-10-27 15:49:27 +00001212
1213 err = igb_alloc_q_vectors(adapter);
1214 if (err) {
1215 dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
1216 goto err_alloc_q_vectors;
1217 }
1218
Alexander Duyck5536d212012-09-25 00:31:17 +00001219 igb_cache_ring_register(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00001220
1221 return 0;
Alexander Duyck5536d212012-09-25 00:31:17 +00001222
Alexander Duyck047e0032009-10-27 15:49:27 +00001223err_alloc_q_vectors:
1224 igb_reset_interrupt_capability(adapter);
1225 return err;
1226}
1227
1228/**
Auke Kok9d5c8242008-01-24 02:22:38 -08001229 * igb_request_irq - initialize interrupts
1230 *
1231 * Attempts to configure interrupts using the best available
1232 * capabilities of the hardware and kernel.
1233 **/
1234static int igb_request_irq(struct igb_adapter *adapter)
1235{
1236 struct net_device *netdev = adapter->netdev;
Alexander Duyck047e0032009-10-27 15:49:27 +00001237 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08001238 int err = 0;
1239
1240 if (adapter->msix_entries) {
1241 err = igb_request_msix(adapter);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001242 if (!err)
Auke Kok9d5c8242008-01-24 02:22:38 -08001243 goto request_done;
Auke Kok9d5c8242008-01-24 02:22:38 -08001244 /* fall back to MSI */
Alexander Duyck5536d212012-09-25 00:31:17 +00001245 igb_free_all_tx_resources(adapter);
1246 igb_free_all_rx_resources(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001247
Alexander Duyck047e0032009-10-27 15:49:27 +00001248 igb_clear_interrupt_scheme(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001249 err = igb_init_interrupt_scheme(adapter, false);
1250 if (err)
Alexander Duyck047e0032009-10-27 15:49:27 +00001251 goto request_done;
Stefan Assmann53c7d062012-12-04 06:00:12 +00001252
Alexander Duyck047e0032009-10-27 15:49:27 +00001253 igb_setup_all_tx_resources(adapter);
1254 igb_setup_all_rx_resources(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001255 igb_configure(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001256 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001257
Alexander Duyckc74d5882011-08-26 07:46:45 +00001258 igb_assign_vector(adapter->q_vector[0], 0);
1259
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001260 if (adapter->flags & IGB_FLAG_HAS_MSI) {
Alexander Duyckc74d5882011-08-26 07:46:45 +00001261 err = request_irq(pdev->irq, igb_intr_msi, 0,
Alexander Duyck047e0032009-10-27 15:49:27 +00001262 netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001263 if (!err)
1264 goto request_done;
Alexander Duyck047e0032009-10-27 15:49:27 +00001265
Auke Kok9d5c8242008-01-24 02:22:38 -08001266 /* fall back to legacy interrupts */
1267 igb_reset_interrupt_capability(adapter);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001268 adapter->flags &= ~IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -08001269 }
1270
Alexander Duyckc74d5882011-08-26 07:46:45 +00001271 err = request_irq(pdev->irq, igb_intr, IRQF_SHARED,
Alexander Duyck047e0032009-10-27 15:49:27 +00001272 netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001273
Andy Gospodarek6cb5e572008-02-15 14:05:25 -08001274 if (err)
Alexander Duyckc74d5882011-08-26 07:46:45 +00001275 dev_err(&pdev->dev, "Error %d getting interrupt\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08001276 err);
Auke Kok9d5c8242008-01-24 02:22:38 -08001277
1278request_done:
1279 return err;
1280}
1281
1282static void igb_free_irq(struct igb_adapter *adapter)
1283{
Auke Kok9d5c8242008-01-24 02:22:38 -08001284 if (adapter->msix_entries) {
1285 int vector = 0, i;
1286
Alexander Duyck047e0032009-10-27 15:49:27 +00001287 free_irq(adapter->msix_entries[vector++].vector, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001288
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001289 for (i = 0; i < adapter->num_q_vectors; i++)
Alexander Duyck047e0032009-10-27 15:49:27 +00001290 free_irq(adapter->msix_entries[vector++].vector,
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001291 adapter->q_vector[i]);
Alexander Duyck047e0032009-10-27 15:49:27 +00001292 } else {
1293 free_irq(adapter->pdev->irq, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001294 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001295}
1296
1297/**
1298 * igb_irq_disable - Mask off interrupt generation on the NIC
1299 * @adapter: board private structure
1300 **/
1301static void igb_irq_disable(struct igb_adapter *adapter)
1302{
1303 struct e1000_hw *hw = &adapter->hw;
1304
Alexander Duyck25568a52009-10-27 23:49:59 +00001305 /*
1306 * we need to be careful when disabling interrupts. The VFs are also
1307 * mapped into these registers and so clearing the bits can cause
1308 * issues on the VF drivers so we only need to clear what we set
1309 */
Auke Kok9d5c8242008-01-24 02:22:38 -08001310 if (adapter->msix_entries) {
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001311 u32 regval = rd32(E1000_EIAM);
1312 wr32(E1000_EIAM, regval & ~adapter->eims_enable_mask);
1313 wr32(E1000_EIMC, adapter->eims_enable_mask);
1314 regval = rd32(E1000_EIAC);
1315 wr32(E1000_EIAC, regval & ~adapter->eims_enable_mask);
Auke Kok9d5c8242008-01-24 02:22:38 -08001316 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001317
1318 wr32(E1000_IAM, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08001319 wr32(E1000_IMC, ~0);
1320 wrfl();
Emil Tantilov81a61852010-08-02 14:40:52 +00001321 if (adapter->msix_entries) {
1322 int i;
1323 for (i = 0; i < adapter->num_q_vectors; i++)
1324 synchronize_irq(adapter->msix_entries[i].vector);
1325 } else {
1326 synchronize_irq(adapter->pdev->irq);
1327 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001328}
1329
1330/**
1331 * igb_irq_enable - Enable default interrupt generation settings
1332 * @adapter: board private structure
1333 **/
1334static void igb_irq_enable(struct igb_adapter *adapter)
1335{
1336 struct e1000_hw *hw = &adapter->hw;
1337
1338 if (adapter->msix_entries) {
Alexander Duyck06218a82011-08-26 07:46:55 +00001339 u32 ims = E1000_IMS_LSC | E1000_IMS_DOUTSYNC | E1000_IMS_DRSTA;
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001340 u32 regval = rd32(E1000_EIAC);
1341 wr32(E1000_EIAC, regval | adapter->eims_enable_mask);
1342 regval = rd32(E1000_EIAM);
1343 wr32(E1000_EIAM, regval | adapter->eims_enable_mask);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001344 wr32(E1000_EIMS, adapter->eims_enable_mask);
Alexander Duyck25568a52009-10-27 23:49:59 +00001345 if (adapter->vfs_allocated_count) {
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001346 wr32(E1000_MBVFIMR, 0xFF);
Alexander Duyck25568a52009-10-27 23:49:59 +00001347 ims |= E1000_IMS_VMMB;
1348 }
1349 wr32(E1000_IMS, ims);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001350 } else {
Alexander Duyck55cac242009-11-19 12:42:21 +00001351 wr32(E1000_IMS, IMS_ENABLE_MASK |
1352 E1000_IMS_DRSTA);
1353 wr32(E1000_IAM, IMS_ENABLE_MASK |
1354 E1000_IMS_DRSTA);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001355 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001356}
1357
1358static void igb_update_mng_vlan(struct igb_adapter *adapter)
1359{
Alexander Duyck51466232009-10-27 23:47:35 +00001360 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08001361 u16 vid = adapter->hw.mng_cookie.vlan_id;
1362 u16 old_vid = adapter->mng_vlan_id;
Auke Kok9d5c8242008-01-24 02:22:38 -08001363
Alexander Duyck51466232009-10-27 23:47:35 +00001364 if (hw->mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
1365 /* add VID to filter table */
1366 igb_vfta_set(hw, vid, true);
1367 adapter->mng_vlan_id = vid;
1368 } else {
1369 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
1370 }
1371
1372 if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
1373 (vid != old_vid) &&
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001374 !test_bit(old_vid, adapter->active_vlans)) {
Alexander Duyck51466232009-10-27 23:47:35 +00001375 /* remove VID from filter table */
1376 igb_vfta_set(hw, old_vid, false);
Auke Kok9d5c8242008-01-24 02:22:38 -08001377 }
1378}
1379
1380/**
1381 * igb_release_hw_control - release control of the h/w to f/w
1382 * @adapter: address of board private structure
1383 *
1384 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
1385 * For ASF and Pass Through versions of f/w this means that the
1386 * driver is no longer loaded.
1387 *
1388 **/
1389static void igb_release_hw_control(struct igb_adapter *adapter)
1390{
1391 struct e1000_hw *hw = &adapter->hw;
1392 u32 ctrl_ext;
1393
1394 /* Let firmware take over control of h/w */
1395 ctrl_ext = rd32(E1000_CTRL_EXT);
1396 wr32(E1000_CTRL_EXT,
1397 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
1398}
1399
Auke Kok9d5c8242008-01-24 02:22:38 -08001400/**
1401 * igb_get_hw_control - get control of the h/w from f/w
1402 * @adapter: address of board private structure
1403 *
1404 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
1405 * For ASF and Pass Through versions of f/w this means that
1406 * the driver is loaded.
1407 *
1408 **/
1409static void igb_get_hw_control(struct igb_adapter *adapter)
1410{
1411 struct e1000_hw *hw = &adapter->hw;
1412 u32 ctrl_ext;
1413
1414 /* Let firmware know the driver has taken over */
1415 ctrl_ext = rd32(E1000_CTRL_EXT);
1416 wr32(E1000_CTRL_EXT,
1417 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
1418}
1419
Auke Kok9d5c8242008-01-24 02:22:38 -08001420/**
1421 * igb_configure - configure the hardware for RX and TX
1422 * @adapter: private board structure
1423 **/
1424static void igb_configure(struct igb_adapter *adapter)
1425{
1426 struct net_device *netdev = adapter->netdev;
1427 int i;
1428
1429 igb_get_hw_control(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001430 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001431
1432 igb_restore_vlan(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001433
Alexander Duyck85b430b2009-10-27 15:50:29 +00001434 igb_setup_tctl(adapter);
Alexander Duyck06cf2662009-10-27 15:53:25 +00001435 igb_setup_mrqc(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001436 igb_setup_rctl(adapter);
Alexander Duyck85b430b2009-10-27 15:50:29 +00001437
1438 igb_configure_tx(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001439 igb_configure_rx(adapter);
Alexander Duyck662d7202008-06-27 11:00:29 -07001440
1441 igb_rx_fifo_flush_82575(&adapter->hw);
1442
Alexander Duyckc493ea42009-03-20 00:16:50 +00001443 /* call igb_desc_unused which always leaves
Auke Kok9d5c8242008-01-24 02:22:38 -08001444 * at least 1 descriptor unused to make sure
1445 * next_to_use != next_to_clean */
1446 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00001447 struct igb_ring *ring = adapter->rx_ring[i];
Alexander Duyckcd392f52011-08-26 07:43:59 +00001448 igb_alloc_rx_buffers(ring, igb_desc_unused(ring));
Auke Kok9d5c8242008-01-24 02:22:38 -08001449 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001450}
1451
Nick Nunley88a268c2010-02-17 01:01:59 +00001452/**
1453 * igb_power_up_link - Power up the phy/serdes link
1454 * @adapter: address of board private structure
1455 **/
1456void igb_power_up_link(struct igb_adapter *adapter)
1457{
Akeem G. Abodunrin76886592012-07-17 04:51:18 +00001458 igb_reset_phy(&adapter->hw);
1459
Nick Nunley88a268c2010-02-17 01:01:59 +00001460 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1461 igb_power_up_phy_copper(&adapter->hw);
1462 else
1463 igb_power_up_serdes_link_82575(&adapter->hw);
1464}
1465
1466/**
1467 * igb_power_down_link - Power down the phy/serdes link
1468 * @adapter: address of board private structure
1469 */
1470static void igb_power_down_link(struct igb_adapter *adapter)
1471{
1472 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1473 igb_power_down_phy_copper_82575(&adapter->hw);
1474 else
1475 igb_shutdown_serdes_link_82575(&adapter->hw);
1476}
Auke Kok9d5c8242008-01-24 02:22:38 -08001477
1478/**
1479 * igb_up - Open the interface and prepare it to handle traffic
1480 * @adapter: board private structure
1481 **/
Auke Kok9d5c8242008-01-24 02:22:38 -08001482int igb_up(struct igb_adapter *adapter)
1483{
1484 struct e1000_hw *hw = &adapter->hw;
1485 int i;
1486
1487 /* hardware has been reset, we need to reload some things */
1488 igb_configure(adapter);
1489
1490 clear_bit(__IGB_DOWN, &adapter->state);
1491
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001492 for (i = 0; i < adapter->num_q_vectors; i++)
1493 napi_enable(&(adapter->q_vector[i]->napi));
1494
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001495 if (adapter->msix_entries)
Auke Kok9d5c8242008-01-24 02:22:38 -08001496 igb_configure_msix(adapter);
Alexander Duyckfeeb2722010-02-03 21:59:51 +00001497 else
1498 igb_assign_vector(adapter->q_vector[0], 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08001499
1500 /* Clear any pending interrupts. */
1501 rd32(E1000_ICR);
1502 igb_irq_enable(adapter);
1503
Alexander Duyckd4960302009-10-27 15:53:45 +00001504 /* notify VFs that reset has been completed */
1505 if (adapter->vfs_allocated_count) {
1506 u32 reg_data = rd32(E1000_CTRL_EXT);
1507 reg_data |= E1000_CTRL_EXT_PFRSTD;
1508 wr32(E1000_CTRL_EXT, reg_data);
1509 }
1510
Jesse Brandeburg4cb9be72009-04-21 18:42:05 +00001511 netif_tx_start_all_queues(adapter->netdev);
1512
Alexander Duyck25568a52009-10-27 23:49:59 +00001513 /* start the watchdog. */
1514 hw->mac.get_link_status = 1;
1515 schedule_work(&adapter->watchdog_task);
1516
Auke Kok9d5c8242008-01-24 02:22:38 -08001517 return 0;
1518}
1519
1520void igb_down(struct igb_adapter *adapter)
1521{
Auke Kok9d5c8242008-01-24 02:22:38 -08001522 struct net_device *netdev = adapter->netdev;
Alexander Duyck330a6d62009-10-27 23:51:35 +00001523 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08001524 u32 tctl, rctl;
1525 int i;
1526
1527 /* signal that we're down so the interrupt handler does not
1528 * reschedule our watchdog timer */
1529 set_bit(__IGB_DOWN, &adapter->state);
1530
1531 /* disable receives in the hardware */
1532 rctl = rd32(E1000_RCTL);
1533 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
1534 /* flush and sleep below */
1535
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001536 netif_tx_stop_all_queues(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001537
1538 /* disable transmits in the hardware */
1539 tctl = rd32(E1000_TCTL);
1540 tctl &= ~E1000_TCTL_EN;
1541 wr32(E1000_TCTL, tctl);
1542 /* flush both disables and wait for them to finish */
1543 wrfl();
1544 msleep(10);
1545
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001546 for (i = 0; i < adapter->num_q_vectors; i++)
1547 napi_disable(&(adapter->q_vector[i]->napi));
Auke Kok9d5c8242008-01-24 02:22:38 -08001548
Auke Kok9d5c8242008-01-24 02:22:38 -08001549 igb_irq_disable(adapter);
1550
1551 del_timer_sync(&adapter->watchdog_timer);
1552 del_timer_sync(&adapter->phy_info_timer);
1553
Auke Kok9d5c8242008-01-24 02:22:38 -08001554 netif_carrier_off(netdev);
Alexander Duyck04fe6352009-02-06 23:22:32 +00001555
1556 /* record the stats before reset*/
Eric Dumazet12dcd862010-10-15 17:27:10 +00001557 spin_lock(&adapter->stats64_lock);
1558 igb_update_stats(adapter, &adapter->stats64);
1559 spin_unlock(&adapter->stats64_lock);
Alexander Duyck04fe6352009-02-06 23:22:32 +00001560
Auke Kok9d5c8242008-01-24 02:22:38 -08001561 adapter->link_speed = 0;
1562 adapter->link_duplex = 0;
1563
Jeff Kirsher30236822008-06-24 17:01:15 -07001564 if (!pci_channel_offline(adapter->pdev))
1565 igb_reset(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001566 igb_clean_all_tx_rings(adapter);
1567 igb_clean_all_rx_rings(adapter);
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00001568#ifdef CONFIG_IGB_DCA
1569
1570 /* since we reset the hardware DCA settings were cleared */
1571 igb_setup_dca(adapter);
1572#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08001573}
1574
1575void igb_reinit_locked(struct igb_adapter *adapter)
1576{
1577 WARN_ON(in_interrupt());
1578 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
1579 msleep(1);
1580 igb_down(adapter);
1581 igb_up(adapter);
1582 clear_bit(__IGB_RESETTING, &adapter->state);
1583}
1584
1585void igb_reset(struct igb_adapter *adapter)
1586{
Alexander Duyck090b1792009-10-27 23:51:55 +00001587 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08001588 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001589 struct e1000_mac_info *mac = &hw->mac;
1590 struct e1000_fc_info *fc = &hw->fc;
Matthew Vickd48507f2012-11-08 04:03:58 +00001591 u32 pba = 0, tx_space, min_tx_space, min_rx_space, hwm;
Auke Kok9d5c8242008-01-24 02:22:38 -08001592
1593 /* Repartition Pba for greater than 9k mtu
1594 * To take effect CTRL.RST is required.
1595 */
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001596 switch (mac->type) {
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +00001597 case e1000_i350:
Alexander Duyck55cac242009-11-19 12:42:21 +00001598 case e1000_82580:
1599 pba = rd32(E1000_RXPBS);
1600 pba = igb_rxpbs_adjust_82580(pba);
1601 break;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001602 case e1000_82576:
Alexander Duyckd249be52009-10-27 23:46:38 +00001603 pba = rd32(E1000_RXPBS);
1604 pba &= E1000_RXPBS_SIZE_MASK_82576;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001605 break;
1606 case e1000_82575:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001607 case e1000_i210:
1608 case e1000_i211:
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001609 default:
1610 pba = E1000_PBA_34K;
1611 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001612 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001613
Alexander Duyck2d064c02008-07-08 15:10:12 -07001614 if ((adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) &&
1615 (mac->type < e1000_82576)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001616 /* adjust PBA for jumbo frames */
1617 wr32(E1000_PBA, pba);
1618
1619 /* To maintain wire speed transmits, the Tx FIFO should be
1620 * large enough to accommodate two full transmit packets,
1621 * rounded up to the next 1KB and expressed in KB. Likewise,
1622 * the Rx FIFO should be large enough to accommodate at least
1623 * one full receive packet and is similarly rounded up and
1624 * expressed in KB. */
1625 pba = rd32(E1000_PBA);
1626 /* upper 16 bits has Tx packet buffer allocation size in KB */
1627 tx_space = pba >> 16;
1628 /* lower 16 bits has Rx packet buffer allocation size in KB */
1629 pba &= 0xffff;
1630 /* the tx fifo also stores 16 bytes of information about the tx
1631 * but don't include ethernet FCS because hardware appends it */
1632 min_tx_space = (adapter->max_frame_size +
Alexander Duyck85e8d002009-02-16 00:00:20 -08001633 sizeof(union e1000_adv_tx_desc) -
Auke Kok9d5c8242008-01-24 02:22:38 -08001634 ETH_FCS_LEN) * 2;
1635 min_tx_space = ALIGN(min_tx_space, 1024);
1636 min_tx_space >>= 10;
1637 /* software strips receive CRC, so leave room for it */
1638 min_rx_space = adapter->max_frame_size;
1639 min_rx_space = ALIGN(min_rx_space, 1024);
1640 min_rx_space >>= 10;
1641
1642 /* If current Tx allocation is less than the min Tx FIFO size,
1643 * and the min Tx FIFO size is less than the current Rx FIFO
1644 * allocation, take space away from current Rx allocation */
1645 if (tx_space < min_tx_space &&
1646 ((min_tx_space - tx_space) < pba)) {
1647 pba = pba - (min_tx_space - tx_space);
1648
1649 /* if short on rx space, rx wins and must trump tx
1650 * adjustment */
1651 if (pba < min_rx_space)
1652 pba = min_rx_space;
1653 }
Alexander Duyck2d064c02008-07-08 15:10:12 -07001654 wr32(E1000_PBA, pba);
Auke Kok9d5c8242008-01-24 02:22:38 -08001655 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001656
1657 /* flow control settings */
1658 /* The high water mark must be low enough to fit one full frame
1659 * (or the size used for early receive) above it in the Rx FIFO.
1660 * Set it to the lower of:
1661 * - 90% of the Rx FIFO size, or
1662 * - the full Rx FIFO size minus one full frame */
1663 hwm = min(((pba << 10) * 9 / 10),
Alexander Duyck2d064c02008-07-08 15:10:12 -07001664 ((pba << 10) - 2 * adapter->max_frame_size));
Auke Kok9d5c8242008-01-24 02:22:38 -08001665
Matthew Vickd48507f2012-11-08 04:03:58 +00001666 fc->high_water = hwm & 0xFFFFFFF0; /* 16-byte granularity */
Alexander Duyckd405ea32009-12-23 13:21:27 +00001667 fc->low_water = fc->high_water - 16;
Auke Kok9d5c8242008-01-24 02:22:38 -08001668 fc->pause_time = 0xFFFF;
1669 fc->send_xon = 1;
Alexander Duyck0cce1192009-07-23 18:10:24 +00001670 fc->current_mode = fc->requested_mode;
Auke Kok9d5c8242008-01-24 02:22:38 -08001671
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001672 /* disable receive for all VFs and wait one second */
1673 if (adapter->vfs_allocated_count) {
1674 int i;
1675 for (i = 0 ; i < adapter->vfs_allocated_count; i++)
Greg Rose8fa7e0f2010-11-06 05:43:21 +00001676 adapter->vf_data[i].flags &= IGB_VF_FLAG_PF_SET_MAC;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001677
1678 /* ping all the active vfs to let them know we are going down */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00001679 igb_ping_all_vfs(adapter);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001680
1681 /* disable transmits and receives */
1682 wr32(E1000_VFRE, 0);
1683 wr32(E1000_VFTE, 0);
1684 }
1685
Auke Kok9d5c8242008-01-24 02:22:38 -08001686 /* Allow time for pending master requests to run */
Alexander Duyck330a6d62009-10-27 23:51:35 +00001687 hw->mac.ops.reset_hw(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001688 wr32(E1000_WUC, 0);
1689
Alexander Duyck330a6d62009-10-27 23:51:35 +00001690 if (hw->mac.ops.init_hw(hw))
Alexander Duyck090b1792009-10-27 23:51:55 +00001691 dev_err(&pdev->dev, "Hardware Error\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08001692
Matthew Vicka27416b2012-04-18 02:57:44 +00001693 /*
1694 * Flow control settings reset on hardware reset, so guarantee flow
1695 * control is off when forcing speed.
1696 */
1697 if (!hw->mac.autoneg)
1698 igb_force_mac_fc(hw);
1699
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00001700 igb_init_dmac(adapter, pba);
Nick Nunley88a268c2010-02-17 01:01:59 +00001701 if (!netif_running(adapter->netdev))
1702 igb_power_down_link(adapter);
1703
Auke Kok9d5c8242008-01-24 02:22:38 -08001704 igb_update_mng_vlan(adapter);
1705
1706 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
1707 wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
1708
Matthew Vick1f6e8172012-08-18 07:26:33 +00001709 /* Re-enable PTP, where applicable. */
1710 igb_ptp_reset(adapter);
Matthew Vick1f6e8172012-08-18 07:26:33 +00001711
Alexander Duyck330a6d62009-10-27 23:51:35 +00001712 igb_get_phy_info(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001713}
1714
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001715static netdev_features_t igb_fix_features(struct net_device *netdev,
1716 netdev_features_t features)
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001717{
1718 /*
1719 * Since there is no support for separate rx/tx vlan accel
1720 * enable/disable make sure tx flag is always in same state as rx.
1721 */
1722 if (features & NETIF_F_HW_VLAN_RX)
1723 features |= NETIF_F_HW_VLAN_TX;
1724 else
1725 features &= ~NETIF_F_HW_VLAN_TX;
1726
1727 return features;
1728}
1729
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001730static int igb_set_features(struct net_device *netdev,
1731 netdev_features_t features)
Michał Mirosławac52caa2011-06-08 08:38:01 +00001732{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001733 netdev_features_t changed = netdev->features ^ features;
Ben Greear89eaefb2012-03-06 09:41:58 +00001734 struct igb_adapter *adapter = netdev_priv(netdev);
Michał Mirosławac52caa2011-06-08 08:38:01 +00001735
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001736 if (changed & NETIF_F_HW_VLAN_RX)
1737 igb_vlan_mode(netdev, features);
1738
Ben Greear89eaefb2012-03-06 09:41:58 +00001739 if (!(changed & NETIF_F_RXALL))
1740 return 0;
1741
1742 netdev->features = features;
1743
1744 if (netif_running(netdev))
1745 igb_reinit_locked(adapter);
1746 else
1747 igb_reset(adapter);
1748
Michał Mirosławac52caa2011-06-08 08:38:01 +00001749 return 0;
1750}
1751
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001752static const struct net_device_ops igb_netdev_ops = {
Alexander Duyck559e9c42009-10-27 23:52:50 +00001753 .ndo_open = igb_open,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001754 .ndo_stop = igb_close,
Alexander Duyckcd392f52011-08-26 07:43:59 +00001755 .ndo_start_xmit = igb_xmit_frame,
Eric Dumazet12dcd862010-10-15 17:27:10 +00001756 .ndo_get_stats64 = igb_get_stats64,
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001757 .ndo_set_rx_mode = igb_set_rx_mode,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001758 .ndo_set_mac_address = igb_set_mac,
1759 .ndo_change_mtu = igb_change_mtu,
1760 .ndo_do_ioctl = igb_ioctl,
1761 .ndo_tx_timeout = igb_tx_timeout,
1762 .ndo_validate_addr = eth_validate_addr,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001763 .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
1764 .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
Williams, Mitch A8151d292010-02-10 01:44:24 +00001765 .ndo_set_vf_mac = igb_ndo_set_vf_mac,
1766 .ndo_set_vf_vlan = igb_ndo_set_vf_vlan,
1767 .ndo_set_vf_tx_rate = igb_ndo_set_vf_bw,
1768 .ndo_get_vf_config = igb_ndo_get_vf_config,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001769#ifdef CONFIG_NET_POLL_CONTROLLER
1770 .ndo_poll_controller = igb_netpoll,
1771#endif
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001772 .ndo_fix_features = igb_fix_features,
1773 .ndo_set_features = igb_set_features,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001774};
1775
Taku Izumi42bfd33a2008-06-20 12:10:30 +09001776/**
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001777 * igb_set_fw_version - Configure version string for ethtool
1778 * @adapter: adapter struct
1779 *
1780 **/
1781void igb_set_fw_version(struct igb_adapter *adapter)
1782{
1783 struct e1000_hw *hw = &adapter->hw;
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001784 struct e1000_fw_version fw;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001785
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001786 igb_get_fw_version(hw, &fw);
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001787
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001788 switch (hw->mac.type) {
1789 case e1000_i211:
1790 snprintf(adapter->fw_version, sizeof(adapter->fw_version),
1791 "%2d.%2d-%d",
1792 fw.invm_major, fw.invm_minor, fw.invm_img_type);
1793 break;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001794
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001795 default:
1796 /* if option is rom valid, display its version too */
1797 if (fw.or_valid) {
1798 snprintf(adapter->fw_version,
1799 sizeof(adapter->fw_version),
1800 "%d.%d, 0x%08x, %d.%d.%d",
1801 fw.eep_major, fw.eep_minor, fw.etrack_id,
1802 fw.or_major, fw.or_build, fw.or_patch);
1803 /* no option rom */
1804 } else {
1805 snprintf(adapter->fw_version,
1806 sizeof(adapter->fw_version),
1807 "%d.%d, 0x%08x",
1808 fw.eep_major, fw.eep_minor, fw.etrack_id);
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001809 }
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001810 break;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001811 }
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001812 return;
1813}
1814
1815/**
Auke Kok9d5c8242008-01-24 02:22:38 -08001816 * igb_probe - Device Initialization Routine
1817 * @pdev: PCI device information struct
1818 * @ent: entry in igb_pci_tbl
1819 *
1820 * Returns 0 on success, negative on failure
1821 *
1822 * igb_probe initializes an adapter identified by a pci_dev structure.
1823 * The OS initialization, configuring of the adapter private structure,
1824 * and a hardware reset occur.
1825 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05001826static int igb_probe(struct pci_dev *pdev,
Auke Kok9d5c8242008-01-24 02:22:38 -08001827 const struct pci_device_id *ent)
1828{
1829 struct net_device *netdev;
1830 struct igb_adapter *adapter;
1831 struct e1000_hw *hw;
Alexander Duyck4337e992009-10-27 23:48:31 +00001832 u16 eeprom_data = 0;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00001833 s32 ret_val;
Alexander Duyck4337e992009-10-27 23:48:31 +00001834 static int global_quad_port_a; /* global quad port a indication */
Auke Kok9d5c8242008-01-24 02:22:38 -08001835 const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
1836 unsigned long mmio_start, mmio_len;
David S. Miller2d6a5e92009-03-17 15:01:30 -07001837 int err, pci_using_dac;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00001838 u8 part_str[E1000_PBANUM_LENGTH];
Auke Kok9d5c8242008-01-24 02:22:38 -08001839
Andy Gospodarekbded64a2010-07-21 06:40:31 +00001840 /* Catch broken hardware that put the wrong VF device ID in
1841 * the PCIe SR-IOV capability.
1842 */
1843 if (pdev->is_virtfn) {
1844 WARN(1, KERN_ERR "%s (%hx:%hx) should not be a VF!\n",
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001845 pci_name(pdev), pdev->vendor, pdev->device);
Andy Gospodarekbded64a2010-07-21 06:40:31 +00001846 return -EINVAL;
1847 }
1848
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001849 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001850 if (err)
1851 return err;
1852
1853 pci_using_dac = 0;
Alexander Duyck59d71982010-04-27 13:09:25 +00001854 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08001855 if (!err) {
Alexander Duyck59d71982010-04-27 13:09:25 +00001856 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08001857 if (!err)
1858 pci_using_dac = 1;
1859 } else {
Alexander Duyck59d71982010-04-27 13:09:25 +00001860 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08001861 if (err) {
Alexander Duyck59d71982010-04-27 13:09:25 +00001862 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08001863 if (err) {
1864 dev_err(&pdev->dev, "No usable DMA "
1865 "configuration, aborting\n");
1866 goto err_dma;
1867 }
1868 }
1869 }
1870
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001871 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
1872 IORESOURCE_MEM),
1873 igb_driver_name);
Auke Kok9d5c8242008-01-24 02:22:38 -08001874 if (err)
1875 goto err_pci_reg;
1876
Frans Pop19d5afd2009-10-02 10:04:12 -07001877 pci_enable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08001878
Auke Kok9d5c8242008-01-24 02:22:38 -08001879 pci_set_master(pdev);
Auke Kokc682fc22008-04-23 11:09:34 -07001880 pci_save_state(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001881
1882 err = -ENOMEM;
Alexander Duyck1bfaf072009-02-19 20:39:23 -08001883 netdev = alloc_etherdev_mq(sizeof(struct igb_adapter),
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00001884 IGB_MAX_TX_QUEUES);
Auke Kok9d5c8242008-01-24 02:22:38 -08001885 if (!netdev)
1886 goto err_alloc_etherdev;
1887
1888 SET_NETDEV_DEV(netdev, &pdev->dev);
1889
1890 pci_set_drvdata(pdev, netdev);
1891 adapter = netdev_priv(netdev);
1892 adapter->netdev = netdev;
1893 adapter->pdev = pdev;
1894 hw = &adapter->hw;
1895 hw->back = adapter;
stephen hemmingerb3f4d592012-03-13 06:04:20 +00001896 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
Auke Kok9d5c8242008-01-24 02:22:38 -08001897
1898 mmio_start = pci_resource_start(pdev, 0);
1899 mmio_len = pci_resource_len(pdev, 0);
1900
1901 err = -EIO;
Alexander Duyck28b07592009-02-06 23:20:31 +00001902 hw->hw_addr = ioremap(mmio_start, mmio_len);
1903 if (!hw->hw_addr)
Auke Kok9d5c8242008-01-24 02:22:38 -08001904 goto err_ioremap;
1905
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001906 netdev->netdev_ops = &igb_netdev_ops;
Auke Kok9d5c8242008-01-24 02:22:38 -08001907 igb_set_ethtool_ops(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001908 netdev->watchdog_timeo = 5 * HZ;
Auke Kok9d5c8242008-01-24 02:22:38 -08001909
1910 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
1911
1912 netdev->mem_start = mmio_start;
1913 netdev->mem_end = mmio_start + mmio_len;
1914
Auke Kok9d5c8242008-01-24 02:22:38 -08001915 /* PCI config space info */
1916 hw->vendor_id = pdev->vendor;
1917 hw->device_id = pdev->device;
1918 hw->revision_id = pdev->revision;
1919 hw->subsystem_vendor_id = pdev->subsystem_vendor;
1920 hw->subsystem_device_id = pdev->subsystem_device;
1921
Auke Kok9d5c8242008-01-24 02:22:38 -08001922 /* Copy the default MAC, PHY and NVM function pointers */
1923 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
1924 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
1925 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
1926 /* Initialize skew-specific constants */
1927 err = ei->get_invariants(hw);
1928 if (err)
Alexander Duyck450c87c2009-02-06 23:22:11 +00001929 goto err_sw_init;
Auke Kok9d5c8242008-01-24 02:22:38 -08001930
Alexander Duyck450c87c2009-02-06 23:22:11 +00001931 /* setup the private structure */
Auke Kok9d5c8242008-01-24 02:22:38 -08001932 err = igb_sw_init(adapter);
1933 if (err)
1934 goto err_sw_init;
1935
1936 igb_get_bus_info_pcie(hw);
1937
1938 hw->phy.autoneg_wait_to_complete = false;
Auke Kok9d5c8242008-01-24 02:22:38 -08001939
1940 /* Copper options */
1941 if (hw->phy.media_type == e1000_media_type_copper) {
1942 hw->phy.mdix = AUTO_ALL_MODES;
1943 hw->phy.disable_polarity_correction = false;
1944 hw->phy.ms_type = e1000_ms_hw_default;
1945 }
1946
1947 if (igb_check_reset_block(hw))
1948 dev_info(&pdev->dev,
1949 "PHY reset is blocked due to SOL/IDER session.\n");
1950
Alexander Duyck077887c2011-08-26 07:46:29 +00001951 /*
1952 * features is initialized to 0 in allocation, it might have bits
1953 * set by igb_sw_init so we should use an or instead of an
1954 * assignment.
1955 */
1956 netdev->features |= NETIF_F_SG |
1957 NETIF_F_IP_CSUM |
1958 NETIF_F_IPV6_CSUM |
1959 NETIF_F_TSO |
1960 NETIF_F_TSO6 |
1961 NETIF_F_RXHASH |
1962 NETIF_F_RXCSUM |
1963 NETIF_F_HW_VLAN_RX |
1964 NETIF_F_HW_VLAN_TX;
Michał Mirosławac52caa2011-06-08 08:38:01 +00001965
Alexander Duyck077887c2011-08-26 07:46:29 +00001966 /* copy netdev features into list of user selectable features */
1967 netdev->hw_features |= netdev->features;
Ben Greear89eaefb2012-03-06 09:41:58 +00001968 netdev->hw_features |= NETIF_F_RXALL;
Auke Kok9d5c8242008-01-24 02:22:38 -08001969
Alexander Duyck077887c2011-08-26 07:46:29 +00001970 /* set this bit last since it cannot be part of hw_features */
1971 netdev->features |= NETIF_F_HW_VLAN_FILTER;
1972
1973 netdev->vlan_features |= NETIF_F_TSO |
1974 NETIF_F_TSO6 |
1975 NETIF_F_IP_CSUM |
1976 NETIF_F_IPV6_CSUM |
1977 NETIF_F_SG;
Jeff Kirsher48f29ff2008-06-05 04:06:27 -07001978
Ben Greear6b8f0922012-03-06 09:41:53 +00001979 netdev->priv_flags |= IFF_SUPP_NOFCS;
1980
Yi Zou7b872a52010-09-22 17:57:58 +00001981 if (pci_using_dac) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001982 netdev->features |= NETIF_F_HIGHDMA;
Yi Zou7b872a52010-09-22 17:57:58 +00001983 netdev->vlan_features |= NETIF_F_HIGHDMA;
1984 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001985
Michał Mirosławac52caa2011-06-08 08:38:01 +00001986 if (hw->mac.type >= e1000_82576) {
1987 netdev->hw_features |= NETIF_F_SCTP_CSUM;
Jesse Brandeburgb9473562009-04-27 22:36:13 +00001988 netdev->features |= NETIF_F_SCTP_CSUM;
Michał Mirosławac52caa2011-06-08 08:38:01 +00001989 }
Jesse Brandeburgb9473562009-04-27 22:36:13 +00001990
Jiri Pirko01789342011-08-16 06:29:00 +00001991 netdev->priv_flags |= IFF_UNICAST_FLT;
1992
Alexander Duyck330a6d62009-10-27 23:51:35 +00001993 adapter->en_mng_pt = igb_enable_mng_pass_thru(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001994
1995 /* before reading the NVM, reset the controller to put the device in a
1996 * known good starting state */
1997 hw->mac.ops.reset_hw(hw);
1998
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001999 /*
2000 * make sure the NVM is good , i211 parts have special NVM that
2001 * doesn't contain a checksum
2002 */
2003 if (hw->mac.type != e1000_i211) {
2004 if (hw->nvm.ops.validate(hw) < 0) {
2005 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
2006 err = -EIO;
2007 goto err_eeprom;
2008 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002009 }
2010
2011 /* copy the MAC address out of the NVM */
2012 if (hw->mac.ops.read_mac_addr(hw))
2013 dev_err(&pdev->dev, "NVM Read Error\n");
2014
2015 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
2016 memcpy(netdev->perm_addr, hw->mac.addr, netdev->addr_len);
2017
2018 if (!is_valid_ether_addr(netdev->perm_addr)) {
2019 dev_err(&pdev->dev, "Invalid MAC Address\n");
2020 err = -EIO;
2021 goto err_eeprom;
2022 }
2023
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00002024 /* get firmware version for ethtool -i */
2025 igb_set_fw_version(adapter);
2026
Joe Perchesc061b182010-08-23 18:20:03 +00002027 setup_timer(&adapter->watchdog_timer, igb_watchdog,
Alexander Duyck0e340482009-03-20 00:17:08 +00002028 (unsigned long) adapter);
Joe Perchesc061b182010-08-23 18:20:03 +00002029 setup_timer(&adapter->phy_info_timer, igb_update_phy_info,
Alexander Duyck0e340482009-03-20 00:17:08 +00002030 (unsigned long) adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002031
2032 INIT_WORK(&adapter->reset_task, igb_reset_task);
2033 INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
2034
Alexander Duyck450c87c2009-02-06 23:22:11 +00002035 /* Initialize link properties that are user-changeable */
Auke Kok9d5c8242008-01-24 02:22:38 -08002036 adapter->fc_autoneg = true;
2037 hw->mac.autoneg = true;
2038 hw->phy.autoneg_advertised = 0x2f;
2039
Alexander Duyck0cce1192009-07-23 18:10:24 +00002040 hw->fc.requested_mode = e1000_fc_default;
2041 hw->fc.current_mode = e1000_fc_default;
Auke Kok9d5c8242008-01-24 02:22:38 -08002042
Auke Kok9d5c8242008-01-24 02:22:38 -08002043 igb_validate_mdi_setting(hw);
2044
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002045 /* By default, support wake on port A */
Alexander Duycka2cf8b62009-03-13 20:41:17 +00002046 if (hw->bus.func == 0)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002047 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2048
2049 /* Check the NVM for wake support on non-port A ports */
2050 if (hw->mac.type >= e1000_82580)
Alexander Duyck55cac242009-11-19 12:42:21 +00002051 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A +
2052 NVM_82580_LAN_FUNC_OFFSET(hw->bus.func), 1,
2053 &eeprom_data);
Alexander Duycka2cf8b62009-03-13 20:41:17 +00002054 else if (hw->bus.func == 1)
2055 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
Auke Kok9d5c8242008-01-24 02:22:38 -08002056
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002057 if (eeprom_data & IGB_EEPROM_APME)
2058 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002059
2060 /* now that we have the eeprom settings, apply the special cases where
2061 * the eeprom may be wrong or the board simply won't support wake on
2062 * lan on a particular port */
2063 switch (pdev->device) {
2064 case E1000_DEV_ID_82575GB_QUAD_COPPER:
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002065 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002066 break;
2067 case E1000_DEV_ID_82575EB_FIBER_SERDES:
Alexander Duyck2d064c02008-07-08 15:10:12 -07002068 case E1000_DEV_ID_82576_FIBER:
2069 case E1000_DEV_ID_82576_SERDES:
Auke Kok9d5c8242008-01-24 02:22:38 -08002070 /* Wake events only supported on port A for dual fiber
2071 * regardless of eeprom setting */
2072 if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002073 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002074 break;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002075 case E1000_DEV_ID_82576_QUAD_COPPER:
Stefan Assmannd5aa2252010-04-09 09:51:34 +00002076 case E1000_DEV_ID_82576_QUAD_COPPER_ET2:
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002077 /* if quad port adapter, disable WoL on all but port A */
2078 if (global_quad_port_a != 0)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002079 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002080 else
2081 adapter->flags |= IGB_FLAG_QUAD_PORT_A;
2082 /* Reset for multiple quad port adapters */
2083 if (++global_quad_port_a == 4)
2084 global_quad_port_a = 0;
2085 break;
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002086 default:
2087 /* If the device can't wake, don't set software support */
2088 if (!device_can_wakeup(&adapter->pdev->dev))
2089 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002090 }
2091
2092 /* initialize the wol settings based on the eeprom settings */
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002093 if (adapter->flags & IGB_FLAG_WOL_SUPPORTED)
2094 adapter->wol |= E1000_WUFC_MAG;
2095
2096 /* Some vendors want WoL disabled by default, but still supported */
2097 if ((hw->mac.type == e1000_i350) &&
2098 (pdev->subsystem_vendor == PCI_VENDOR_ID_HP)) {
2099 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2100 adapter->wol = 0;
2101 }
2102
2103 device_set_wakeup_enable(&adapter->pdev->dev,
2104 adapter->flags & IGB_FLAG_WOL_SUPPORTED);
Auke Kok9d5c8242008-01-24 02:22:38 -08002105
2106 /* reset the hardware with the new settings */
2107 igb_reset(adapter);
2108
2109 /* let the f/w know that the h/w is now under the control of the
2110 * driver. */
2111 igb_get_hw_control(adapter);
2112
Auke Kok9d5c8242008-01-24 02:22:38 -08002113 strcpy(netdev->name, "eth%d");
2114 err = register_netdev(netdev);
2115 if (err)
2116 goto err_register;
2117
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00002118 /* carrier off reporting is important to ethtool even BEFORE open */
2119 netif_carrier_off(netdev);
2120
Jeff Kirsher421e02f2008-10-17 11:08:31 -07002121#ifdef CONFIG_IGB_DCA
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08002122 if (dca_add_requester(&pdev->dev) == 0) {
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002123 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002124 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002125 igb_setup_dca(adapter);
2126 }
Alexander Duyckc5b9bd52009-10-27 23:46:01 +00002127
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002128#endif
Matthew Vick3c89f6d2012-08-10 05:40:43 +00002129
Anders Berggren673b8b72011-02-04 07:32:32 +00002130 /* do hw tstamp init after resetting */
Richard Cochran7ebae812012-03-16 10:55:37 +00002131 igb_ptp_init(adapter);
Anders Berggren673b8b72011-02-04 07:32:32 +00002132
Auke Kok9d5c8242008-01-24 02:22:38 -08002133 dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
2134 /* print bus type/speed/width info */
Johannes Berg7c510e42008-10-27 17:47:26 -07002135 dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08002136 netdev->name,
Alexander Duyck559e9c42009-10-27 23:52:50 +00002137 ((hw->bus.speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
Alexander Duyckff846f52010-04-27 01:02:40 +00002138 (hw->bus.speed == e1000_bus_speed_5000) ? "5.0Gb/s" :
Alexander Duyck559e9c42009-10-27 23:52:50 +00002139 "unknown"),
Alexander Duyck59c3de82009-03-31 20:38:00 +00002140 ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
2141 (hw->bus.width == e1000_bus_width_pcie_x2) ? "Width x2" :
2142 (hw->bus.width == e1000_bus_width_pcie_x1) ? "Width x1" :
2143 "unknown"),
Johannes Berg7c510e42008-10-27 17:47:26 -07002144 netdev->dev_addr);
Auke Kok9d5c8242008-01-24 02:22:38 -08002145
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00002146 ret_val = igb_read_part_string(hw, part_str, E1000_PBANUM_LENGTH);
2147 if (ret_val)
2148 strcpy(part_str, "Unknown");
2149 dev_info(&pdev->dev, "%s: PBA No: %s\n", netdev->name, part_str);
Auke Kok9d5c8242008-01-24 02:22:38 -08002150 dev_info(&pdev->dev,
2151 "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
2152 adapter->msix_entries ? "MSI-X" :
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002153 (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
Auke Kok9d5c8242008-01-24 02:22:38 -08002154 adapter->num_rx_queues, adapter->num_tx_queues);
Carolyn Wyborny09b068d2011-03-11 20:42:13 -08002155 switch (hw->mac.type) {
2156 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002157 case e1000_i210:
2158 case e1000_i211:
Carolyn Wyborny09b068d2011-03-11 20:42:13 -08002159 igb_set_eee_i350(hw);
2160 break;
2161 default:
2162 break;
2163 }
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002164
2165 pm_runtime_put_noidle(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002166 return 0;
2167
2168err_register:
2169 igb_release_hw_control(adapter);
2170err_eeprom:
2171 if (!igb_check_reset_block(hw))
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08002172 igb_reset_phy(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002173
2174 if (hw->flash_address)
2175 iounmap(hw->flash_address);
Auke Kok9d5c8242008-01-24 02:22:38 -08002176err_sw_init:
Alexander Duyck047e0032009-10-27 15:49:27 +00002177 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002178 iounmap(hw->hw_addr);
2179err_ioremap:
2180 free_netdev(netdev);
2181err_alloc_etherdev:
Alexander Duyck559e9c42009-10-27 23:52:50 +00002182 pci_release_selected_regions(pdev,
2183 pci_select_bars(pdev, IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08002184err_pci_reg:
2185err_dma:
2186 pci_disable_device(pdev);
2187 return err;
2188}
2189
2190/**
2191 * igb_remove - Device Removal Routine
2192 * @pdev: PCI device information struct
2193 *
2194 * igb_remove is called by the PCI subsystem to alert the driver
2195 * that it should release a PCI device. The could be caused by a
2196 * Hot-Plug event, or because the driver is going to be removed from
2197 * memory.
2198 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002199static void igb_remove(struct pci_dev *pdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08002200{
2201 struct net_device *netdev = pci_get_drvdata(pdev);
2202 struct igb_adapter *adapter = netdev_priv(netdev);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002203 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08002204
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002205 pm_runtime_get_noresume(&pdev->dev);
Matthew Vicka79f4f82012-08-10 05:40:44 +00002206 igb_ptp_stop(adapter);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002207
Tejun Heo760141a2010-12-12 16:45:14 +01002208 /*
2209 * The watchdog timer may be rescheduled, so explicitly
2210 * disable watchdog from being rescheduled.
2211 */
Auke Kok9d5c8242008-01-24 02:22:38 -08002212 set_bit(__IGB_DOWN, &adapter->state);
2213 del_timer_sync(&adapter->watchdog_timer);
2214 del_timer_sync(&adapter->phy_info_timer);
2215
Tejun Heo760141a2010-12-12 16:45:14 +01002216 cancel_work_sync(&adapter->reset_task);
2217 cancel_work_sync(&adapter->watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08002218
Jeff Kirsher421e02f2008-10-17 11:08:31 -07002219#ifdef CONFIG_IGB_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002220 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002221 dev_info(&pdev->dev, "DCA disabled\n");
2222 dca_remove_requester(&pdev->dev);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002223 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08002224 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002225 }
2226#endif
2227
Auke Kok9d5c8242008-01-24 02:22:38 -08002228 /* Release control of h/w to f/w. If f/w is AMT enabled, this
2229 * would have already happened in close and is redundant. */
2230 igb_release_hw_control(adapter);
2231
2232 unregister_netdev(netdev);
2233
Alexander Duyck047e0032009-10-27 15:49:27 +00002234 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002235
Alexander Duyck37680112009-02-19 20:40:30 -08002236#ifdef CONFIG_PCI_IOV
2237 /* reclaim resources allocated to VFs */
2238 if (adapter->vf_data) {
2239 /* disable iov and allow time for transactions to clear */
Stefan Assmannf5571472012-08-18 04:06:11 +00002240 if (igb_vfs_are_assigned(adapter)) {
2241 dev_info(&pdev->dev, "Unloading driver while VFs are assigned - VFs will not be deallocated\n");
2242 } else {
Greg Rose0224d662011-10-14 02:57:14 +00002243 pci_disable_sriov(pdev);
2244 msleep(500);
Greg Rose0224d662011-10-14 02:57:14 +00002245 }
Alexander Duyck37680112009-02-19 20:40:30 -08002246
2247 kfree(adapter->vf_data);
2248 adapter->vf_data = NULL;
2249 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00002250 wrfl();
Alexander Duyck37680112009-02-19 20:40:30 -08002251 msleep(100);
2252 dev_info(&pdev->dev, "IOV Disabled\n");
2253 }
2254#endif
Alexander Duyck559e9c42009-10-27 23:52:50 +00002255
Alexander Duyck28b07592009-02-06 23:20:31 +00002256 iounmap(hw->hw_addr);
2257 if (hw->flash_address)
2258 iounmap(hw->flash_address);
Alexander Duyck559e9c42009-10-27 23:52:50 +00002259 pci_release_selected_regions(pdev,
2260 pci_select_bars(pdev, IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08002261
Carolyn Wyborny1128c752011-10-14 00:13:49 +00002262 kfree(adapter->shadow_vfta);
Auke Kok9d5c8242008-01-24 02:22:38 -08002263 free_netdev(netdev);
2264
Frans Pop19d5afd2009-10-02 10:04:12 -07002265 pci_disable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08002266
Auke Kok9d5c8242008-01-24 02:22:38 -08002267 pci_disable_device(pdev);
2268}
2269
2270/**
Alexander Duycka6b623e2009-10-27 23:47:53 +00002271 * igb_probe_vfs - Initialize vf data storage and add VFs to pci config space
2272 * @adapter: board private structure to initialize
2273 *
2274 * This function initializes the vf specific data storage and then attempts to
2275 * allocate the VFs. The reason for ordering it this way is because it is much
2276 * mor expensive time wise to disable SR-IOV than it is to allocate and free
2277 * the memory for the VFs.
2278 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002279static void igb_probe_vfs(struct igb_adapter *adapter)
Alexander Duycka6b623e2009-10-27 23:47:53 +00002280{
2281#ifdef CONFIG_PCI_IOV
2282 struct pci_dev *pdev = adapter->pdev;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002283 struct e1000_hw *hw = &adapter->hw;
Stefan Assmannf5571472012-08-18 04:06:11 +00002284 int old_vfs = pci_num_vf(adapter->pdev);
Greg Rose0224d662011-10-14 02:57:14 +00002285 int i;
Alexander Duycka6b623e2009-10-27 23:47:53 +00002286
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002287 /* Virtualization features not supported on i210 family. */
2288 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211))
2289 return;
2290
Greg Rose0224d662011-10-14 02:57:14 +00002291 if (old_vfs) {
2292 dev_info(&pdev->dev, "%d pre-allocated VFs found - override "
2293 "max_vfs setting of %d\n", old_vfs, max_vfs);
2294 adapter->vfs_allocated_count = old_vfs;
Alexander Duycka6b623e2009-10-27 23:47:53 +00002295 }
2296
Greg Rose0224d662011-10-14 02:57:14 +00002297 if (!adapter->vfs_allocated_count)
2298 return;
2299
2300 adapter->vf_data = kcalloc(adapter->vfs_allocated_count,
2301 sizeof(struct vf_data_storage), GFP_KERNEL);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002302
Greg Rose0224d662011-10-14 02:57:14 +00002303 /* if allocation failed then we do not support SR-IOV */
2304 if (!adapter->vf_data) {
Alexander Duycka6b623e2009-10-27 23:47:53 +00002305 adapter->vfs_allocated_count = 0;
Greg Rose0224d662011-10-14 02:57:14 +00002306 dev_err(&pdev->dev, "Unable to allocate memory for VF "
2307 "Data Storage\n");
2308 goto out;
Alexander Duycka6b623e2009-10-27 23:47:53 +00002309 }
Greg Rose0224d662011-10-14 02:57:14 +00002310
2311 if (!old_vfs) {
2312 if (pci_enable_sriov(pdev, adapter->vfs_allocated_count))
2313 goto err_out;
2314 }
2315 dev_info(&pdev->dev, "%d VFs allocated\n",
2316 adapter->vfs_allocated_count);
2317 for (i = 0; i < adapter->vfs_allocated_count; i++)
2318 igb_vf_configure(adapter, i);
2319
2320 /* DMA Coalescing is not supported in IOV mode. */
2321 adapter->flags &= ~IGB_FLAG_DMAC;
2322 goto out;
2323err_out:
2324 kfree(adapter->vf_data);
2325 adapter->vf_data = NULL;
2326 adapter->vfs_allocated_count = 0;
2327out:
2328 return;
Alexander Duycka6b623e2009-10-27 23:47:53 +00002329#endif /* CONFIG_PCI_IOV */
2330}
2331
Alexander Duyck115f4592009-11-12 18:37:00 +00002332/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002333 * igb_sw_init - Initialize general software structures (struct igb_adapter)
2334 * @adapter: board private structure to initialize
2335 *
2336 * igb_sw_init initializes the Adapter private data structure.
2337 * Fields are initialized based on PCI device information and
2338 * OS network device settings (MTU size).
2339 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002340static int igb_sw_init(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002341{
2342 struct e1000_hw *hw = &adapter->hw;
2343 struct net_device *netdev = adapter->netdev;
2344 struct pci_dev *pdev = adapter->pdev;
Matthew Vick374a5422012-05-18 04:54:58 +00002345 u32 max_rss_queues;
Auke Kok9d5c8242008-01-24 02:22:38 -08002346
2347 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
2348
Alexander Duyck13fde972011-10-05 13:35:24 +00002349 /* set default ring sizes */
Alexander Duyck68fd9912008-11-20 00:48:10 -08002350 adapter->tx_ring_count = IGB_DEFAULT_TXD;
2351 adapter->rx_ring_count = IGB_DEFAULT_RXD;
Alexander Duyck13fde972011-10-05 13:35:24 +00002352
2353 /* set default ITR values */
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00002354 adapter->rx_itr_setting = IGB_DEFAULT_ITR;
2355 adapter->tx_itr_setting = IGB_DEFAULT_ITR;
2356
Alexander Duyck13fde972011-10-05 13:35:24 +00002357 /* set default work limits */
2358 adapter->tx_work_limit = IGB_DEFAULT_TX_WORK;
2359
Alexander Duyck153285f2011-08-26 07:43:32 +00002360 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
2361 VLAN_HLEN;
Auke Kok9d5c8242008-01-24 02:22:38 -08002362 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
2363
Eric Dumazet12dcd862010-10-15 17:27:10 +00002364 spin_lock_init(&adapter->stats64_lock);
Alexander Duycka6b623e2009-10-27 23:47:53 +00002365#ifdef CONFIG_PCI_IOV
Carolyn Wyborny6b78bb12011-01-20 06:40:45 +00002366 switch (hw->mac.type) {
2367 case e1000_82576:
2368 case e1000_i350:
Stefan Assmann9b082d72011-02-24 20:03:31 +00002369 if (max_vfs > 7) {
2370 dev_warn(&pdev->dev,
2371 "Maximum of 7 VFs per PF, using max\n");
2372 adapter->vfs_allocated_count = 7;
2373 } else
2374 adapter->vfs_allocated_count = max_vfs;
Carolyn Wyborny6b78bb12011-01-20 06:40:45 +00002375 break;
2376 default:
2377 break;
2378 }
Alexander Duycka6b623e2009-10-27 23:47:53 +00002379#endif /* CONFIG_PCI_IOV */
Matthew Vick374a5422012-05-18 04:54:58 +00002380
2381 /* Determine the maximum number of RSS queues supported. */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002382 switch (hw->mac.type) {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002383 case e1000_i211:
Matthew Vick374a5422012-05-18 04:54:58 +00002384 max_rss_queues = IGB_MAX_RX_QUEUES_I211;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002385 break;
Matthew Vick374a5422012-05-18 04:54:58 +00002386 case e1000_82575:
2387 case e1000_i210:
2388 max_rss_queues = IGB_MAX_RX_QUEUES_82575;
2389 break;
2390 case e1000_i350:
2391 /* I350 cannot do RSS and SR-IOV at the same time */
2392 if (!!adapter->vfs_allocated_count) {
2393 max_rss_queues = 1;
2394 break;
2395 }
2396 /* fall through */
2397 case e1000_82576:
2398 if (!!adapter->vfs_allocated_count) {
2399 max_rss_queues = 2;
2400 break;
2401 }
2402 /* fall through */
2403 case e1000_82580:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002404 default:
Matthew Vick374a5422012-05-18 04:54:58 +00002405 max_rss_queues = IGB_MAX_RX_QUEUES;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002406 break;
2407 }
Alexander Duycka99955f2009-11-12 18:37:19 +00002408
Matthew Vick374a5422012-05-18 04:54:58 +00002409 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
2410
2411 /* Determine if we need to pair queues. */
2412 switch (hw->mac.type) {
2413 case e1000_82575:
2414 case e1000_i211:
2415 /* Device supports enough interrupts without queue pairing. */
2416 break;
2417 case e1000_82576:
2418 /*
2419 * If VFs are going to be allocated with RSS queues then we
2420 * should pair the queues in order to conserve interrupts due
2421 * to limited supply.
2422 */
2423 if ((adapter->rss_queues > 1) &&
2424 (adapter->vfs_allocated_count > 6))
2425 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2426 /* fall through */
2427 case e1000_82580:
2428 case e1000_i350:
2429 case e1000_i210:
2430 default:
2431 /*
2432 * If rss_queues > half of max_rss_queues, pair the queues in
2433 * order to conserve interrupts due to limited supply.
2434 */
2435 if (adapter->rss_queues > (max_rss_queues / 2))
2436 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2437 break;
2438 }
Alexander Duycka99955f2009-11-12 18:37:19 +00002439
Carolyn Wyborny1128c752011-10-14 00:13:49 +00002440 /* Setup and initialize a copy of the hw vlan table array */
2441 adapter->shadow_vfta = kzalloc(sizeof(u32) *
2442 E1000_VLAN_FILTER_TBL_SIZE,
2443 GFP_ATOMIC);
2444
Alexander Duycka6b623e2009-10-27 23:47:53 +00002445 /* This call may decrease the number of queues */
Stefan Assmann53c7d062012-12-04 06:00:12 +00002446 if (igb_init_interrupt_scheme(adapter, true)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002447 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
2448 return -ENOMEM;
2449 }
2450
Alexander Duycka6b623e2009-10-27 23:47:53 +00002451 igb_probe_vfs(adapter);
2452
Auke Kok9d5c8242008-01-24 02:22:38 -08002453 /* Explicitly disable IRQ since the NIC can be in any state. */
2454 igb_irq_disable(adapter);
2455
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002456 if (hw->mac.type >= e1000_i350)
Carolyn Wyborny831ec0b2011-03-11 20:43:54 -08002457 adapter->flags &= ~IGB_FLAG_DMAC;
2458
Auke Kok9d5c8242008-01-24 02:22:38 -08002459 set_bit(__IGB_DOWN, &adapter->state);
2460 return 0;
2461}
2462
2463/**
2464 * igb_open - Called when a network interface is made active
2465 * @netdev: network interface device structure
2466 *
2467 * Returns 0 on success, negative value on failure
2468 *
2469 * The open entry point is called when a network interface is made
2470 * active by the system (IFF_UP). At this point all resources needed
2471 * for transmit and receive operations are allocated, the interrupt
2472 * handler is registered with the OS, the watchdog timer is started,
2473 * and the stack is notified that the interface is ready.
2474 **/
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002475static int __igb_open(struct net_device *netdev, bool resuming)
Auke Kok9d5c8242008-01-24 02:22:38 -08002476{
2477 struct igb_adapter *adapter = netdev_priv(netdev);
2478 struct e1000_hw *hw = &adapter->hw;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002479 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002480 int err;
2481 int i;
2482
2483 /* disallow open during test */
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002484 if (test_bit(__IGB_TESTING, &adapter->state)) {
2485 WARN_ON(resuming);
Auke Kok9d5c8242008-01-24 02:22:38 -08002486 return -EBUSY;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002487 }
2488
2489 if (!resuming)
2490 pm_runtime_get_sync(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002491
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00002492 netif_carrier_off(netdev);
2493
Auke Kok9d5c8242008-01-24 02:22:38 -08002494 /* allocate transmit descriptors */
2495 err = igb_setup_all_tx_resources(adapter);
2496 if (err)
2497 goto err_setup_tx;
2498
2499 /* allocate receive descriptors */
2500 err = igb_setup_all_rx_resources(adapter);
2501 if (err)
2502 goto err_setup_rx;
2503
Nick Nunley88a268c2010-02-17 01:01:59 +00002504 igb_power_up_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002505
Auke Kok9d5c8242008-01-24 02:22:38 -08002506 /* before we allocate an interrupt, we must be ready to handle it.
2507 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
2508 * as soon as we call pci_request_irq, so we have to setup our
2509 * clean_rx handler before we do so. */
2510 igb_configure(adapter);
2511
2512 err = igb_request_irq(adapter);
2513 if (err)
2514 goto err_req_irq;
2515
Alexander Duyck0c2cc022012-09-25 00:31:22 +00002516 /* Notify the stack of the actual queue counts. */
2517 err = netif_set_real_num_tx_queues(adapter->netdev,
2518 adapter->num_tx_queues);
2519 if (err)
2520 goto err_set_queues;
2521
2522 err = netif_set_real_num_rx_queues(adapter->netdev,
2523 adapter->num_rx_queues);
2524 if (err)
2525 goto err_set_queues;
2526
Auke Kok9d5c8242008-01-24 02:22:38 -08002527 /* From here on the code is the same as igb_up() */
2528 clear_bit(__IGB_DOWN, &adapter->state);
2529
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00002530 for (i = 0; i < adapter->num_q_vectors; i++)
2531 napi_enable(&(adapter->q_vector[i]->napi));
Auke Kok9d5c8242008-01-24 02:22:38 -08002532
2533 /* Clear any pending interrupts. */
2534 rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07002535
2536 igb_irq_enable(adapter);
2537
Alexander Duyckd4960302009-10-27 15:53:45 +00002538 /* notify VFs that reset has been completed */
2539 if (adapter->vfs_allocated_count) {
2540 u32 reg_data = rd32(E1000_CTRL_EXT);
2541 reg_data |= E1000_CTRL_EXT_PFRSTD;
2542 wr32(E1000_CTRL_EXT, reg_data);
2543 }
2544
Jeff Kirsherd55b53f2008-07-18 04:33:03 -07002545 netif_tx_start_all_queues(netdev);
2546
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002547 if (!resuming)
2548 pm_runtime_put(&pdev->dev);
2549
Alexander Duyck25568a52009-10-27 23:49:59 +00002550 /* start the watchdog. */
2551 hw->mac.get_link_status = 1;
2552 schedule_work(&adapter->watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08002553
2554 return 0;
2555
Alexander Duyck0c2cc022012-09-25 00:31:22 +00002556err_set_queues:
2557 igb_free_irq(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002558err_req_irq:
2559 igb_release_hw_control(adapter);
Nick Nunley88a268c2010-02-17 01:01:59 +00002560 igb_power_down_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002561 igb_free_all_rx_resources(adapter);
2562err_setup_rx:
2563 igb_free_all_tx_resources(adapter);
2564err_setup_tx:
2565 igb_reset(adapter);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002566 if (!resuming)
2567 pm_runtime_put(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002568
2569 return err;
2570}
2571
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002572static int igb_open(struct net_device *netdev)
2573{
2574 return __igb_open(netdev, false);
2575}
2576
Auke Kok9d5c8242008-01-24 02:22:38 -08002577/**
2578 * igb_close - Disables a network interface
2579 * @netdev: network interface device structure
2580 *
2581 * Returns 0, this is not allowed to fail
2582 *
2583 * The close entry point is called when an interface is de-activated
2584 * by the OS. The hardware is still under the driver's control, but
2585 * needs to be disabled. A global MAC reset is issued to stop the
2586 * hardware, and all transmit and receive resources are freed.
2587 **/
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002588static int __igb_close(struct net_device *netdev, bool suspending)
Auke Kok9d5c8242008-01-24 02:22:38 -08002589{
2590 struct igb_adapter *adapter = netdev_priv(netdev);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002591 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002592
2593 WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
Auke Kok9d5c8242008-01-24 02:22:38 -08002594
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002595 if (!suspending)
2596 pm_runtime_get_sync(&pdev->dev);
2597
2598 igb_down(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002599 igb_free_irq(adapter);
2600
2601 igb_free_all_tx_resources(adapter);
2602 igb_free_all_rx_resources(adapter);
2603
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002604 if (!suspending)
2605 pm_runtime_put_sync(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002606 return 0;
2607}
2608
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002609static int igb_close(struct net_device *netdev)
2610{
2611 return __igb_close(netdev, false);
2612}
2613
Auke Kok9d5c8242008-01-24 02:22:38 -08002614/**
2615 * igb_setup_tx_resources - allocate Tx resources (Descriptors)
Auke Kok9d5c8242008-01-24 02:22:38 -08002616 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2617 *
2618 * Return 0 on success, negative on failure
2619 **/
Alexander Duyck80785292009-10-27 15:51:47 +00002620int igb_setup_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002621{
Alexander Duyck59d71982010-04-27 13:09:25 +00002622 struct device *dev = tx_ring->dev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002623 int size;
2624
Alexander Duyck06034642011-08-26 07:44:22 +00002625 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002626
2627 tx_ring->tx_buffer_info = vzalloc(size);
Alexander Duyck06034642011-08-26 07:44:22 +00002628 if (!tx_ring->tx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08002629 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -08002630
2631 /* round up to nearest 4K */
Alexander Duyck85e8d002009-02-16 00:00:20 -08002632 tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08002633 tx_ring->size = ALIGN(tx_ring->size, 4096);
2634
Alexander Duyck5536d212012-09-25 00:31:17 +00002635 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
2636 &tx_ring->dma, GFP_KERNEL);
Auke Kok9d5c8242008-01-24 02:22:38 -08002637 if (!tx_ring->desc)
2638 goto err;
2639
Auke Kok9d5c8242008-01-24 02:22:38 -08002640 tx_ring->next_to_use = 0;
2641 tx_ring->next_to_clean = 0;
Alexander Duyck81c2fc22011-08-26 07:45:20 +00002642
Auke Kok9d5c8242008-01-24 02:22:38 -08002643 return 0;
2644
2645err:
Alexander Duyck06034642011-08-26 07:44:22 +00002646 vfree(tx_ring->tx_buffer_info);
Alexander Duyckf33005a2012-09-13 06:27:55 +00002647 tx_ring->tx_buffer_info = NULL;
2648 dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08002649 return -ENOMEM;
2650}
2651
2652/**
2653 * igb_setup_all_tx_resources - wrapper to allocate Tx resources
2654 * (Descriptors) for all queues
2655 * @adapter: board private structure
2656 *
2657 * Return 0 on success, negative on failure
2658 **/
2659static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
2660{
Alexander Duyck439705e2009-10-27 23:49:20 +00002661 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002662 int i, err = 0;
2663
2664 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00002665 err = igb_setup_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002666 if (err) {
Alexander Duyck439705e2009-10-27 23:49:20 +00002667 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08002668 "Allocation for Tx Queue %u failed\n", i);
2669 for (i--; i >= 0; i--)
Alexander Duyck3025a442010-02-17 01:02:39 +00002670 igb_free_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002671 break;
2672 }
2673 }
2674
2675 return err;
2676}
2677
2678/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002679 * igb_setup_tctl - configure the transmit control registers
2680 * @adapter: Board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08002681 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002682void igb_setup_tctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002683{
Auke Kok9d5c8242008-01-24 02:22:38 -08002684 struct e1000_hw *hw = &adapter->hw;
2685 u32 tctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08002686
Alexander Duyck85b430b2009-10-27 15:50:29 +00002687 /* disable queue 0 which is enabled by default on 82575 and 82576 */
2688 wr32(E1000_TXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08002689
2690 /* Program the Transmit Control Register */
Auke Kok9d5c8242008-01-24 02:22:38 -08002691 tctl = rd32(E1000_TCTL);
2692 tctl &= ~E1000_TCTL_CT;
2693 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
2694 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2695
2696 igb_config_collision_dist(hw);
2697
Auke Kok9d5c8242008-01-24 02:22:38 -08002698 /* Enable transmits */
2699 tctl |= E1000_TCTL_EN;
2700
2701 wr32(E1000_TCTL, tctl);
2702}
2703
2704/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002705 * igb_configure_tx_ring - Configure transmit ring after Reset
2706 * @adapter: board private structure
2707 * @ring: tx ring to configure
2708 *
2709 * Configure a transmit ring after a reset.
2710 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002711void igb_configure_tx_ring(struct igb_adapter *adapter,
2712 struct igb_ring *ring)
Alexander Duyck85b430b2009-10-27 15:50:29 +00002713{
2714 struct e1000_hw *hw = &adapter->hw;
Alexander Duycka74420e2011-08-26 07:43:27 +00002715 u32 txdctl = 0;
Alexander Duyck85b430b2009-10-27 15:50:29 +00002716 u64 tdba = ring->dma;
2717 int reg_idx = ring->reg_idx;
2718
2719 /* disable the queue */
Alexander Duycka74420e2011-08-26 07:43:27 +00002720 wr32(E1000_TXDCTL(reg_idx), 0);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002721 wrfl();
2722 mdelay(10);
2723
2724 wr32(E1000_TDLEN(reg_idx),
2725 ring->count * sizeof(union e1000_adv_tx_desc));
2726 wr32(E1000_TDBAL(reg_idx),
2727 tdba & 0x00000000ffffffffULL);
2728 wr32(E1000_TDBAH(reg_idx), tdba >> 32);
2729
Alexander Duyckfce99e32009-10-27 15:51:27 +00002730 ring->tail = hw->hw_addr + E1000_TDT(reg_idx);
Alexander Duycka74420e2011-08-26 07:43:27 +00002731 wr32(E1000_TDH(reg_idx), 0);
Alexander Duyckfce99e32009-10-27 15:51:27 +00002732 writel(0, ring->tail);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002733
2734 txdctl |= IGB_TX_PTHRESH;
2735 txdctl |= IGB_TX_HTHRESH << 8;
2736 txdctl |= IGB_TX_WTHRESH << 16;
2737
2738 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
2739 wr32(E1000_TXDCTL(reg_idx), txdctl);
2740}
2741
2742/**
2743 * igb_configure_tx - Configure transmit Unit after Reset
2744 * @adapter: board private structure
2745 *
2746 * Configure the Tx unit of the MAC after a reset.
2747 **/
2748static void igb_configure_tx(struct igb_adapter *adapter)
2749{
2750 int i;
2751
2752 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00002753 igb_configure_tx_ring(adapter, adapter->tx_ring[i]);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002754}
2755
2756/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002757 * igb_setup_rx_resources - allocate Rx resources (Descriptors)
Auke Kok9d5c8242008-01-24 02:22:38 -08002758 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2759 *
2760 * Returns 0 on success, negative on failure
2761 **/
Alexander Duyck80785292009-10-27 15:51:47 +00002762int igb_setup_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002763{
Alexander Duyck59d71982010-04-27 13:09:25 +00002764 struct device *dev = rx_ring->dev;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002765 int size;
Auke Kok9d5c8242008-01-24 02:22:38 -08002766
Alexander Duyck06034642011-08-26 07:44:22 +00002767 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002768
2769 rx_ring->rx_buffer_info = vzalloc(size);
Alexander Duyck06034642011-08-26 07:44:22 +00002770 if (!rx_ring->rx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08002771 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -08002772
Auke Kok9d5c8242008-01-24 02:22:38 -08002773 /* Round up to nearest 4K */
Alexander Duyckf33005a2012-09-13 06:27:55 +00002774 rx_ring->size = rx_ring->count * sizeof(union e1000_adv_rx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08002775 rx_ring->size = ALIGN(rx_ring->size, 4096);
2776
Alexander Duyck5536d212012-09-25 00:31:17 +00002777 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
2778 &rx_ring->dma, GFP_KERNEL);
Auke Kok9d5c8242008-01-24 02:22:38 -08002779 if (!rx_ring->desc)
2780 goto err;
2781
Alexander Duyckcbc8e552012-09-25 00:31:02 +00002782 rx_ring->next_to_alloc = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002783 rx_ring->next_to_clean = 0;
2784 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002785
Auke Kok9d5c8242008-01-24 02:22:38 -08002786 return 0;
2787
2788err:
Alexander Duyck06034642011-08-26 07:44:22 +00002789 vfree(rx_ring->rx_buffer_info);
2790 rx_ring->rx_buffer_info = NULL;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002791 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08002792 return -ENOMEM;
2793}
2794
2795/**
2796 * igb_setup_all_rx_resources - wrapper to allocate Rx resources
2797 * (Descriptors) for all queues
2798 * @adapter: board private structure
2799 *
2800 * Return 0 on success, negative on failure
2801 **/
2802static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
2803{
Alexander Duyck439705e2009-10-27 23:49:20 +00002804 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002805 int i, err = 0;
2806
2807 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00002808 err = igb_setup_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002809 if (err) {
Alexander Duyck439705e2009-10-27 23:49:20 +00002810 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08002811 "Allocation for Rx Queue %u failed\n", i);
2812 for (i--; i >= 0; i--)
Alexander Duyck3025a442010-02-17 01:02:39 +00002813 igb_free_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002814 break;
2815 }
2816 }
2817
2818 return err;
2819}
2820
2821/**
Alexander Duyck06cf2662009-10-27 15:53:25 +00002822 * igb_setup_mrqc - configure the multiple receive queue control registers
2823 * @adapter: Board private structure
2824 **/
2825static void igb_setup_mrqc(struct igb_adapter *adapter)
2826{
2827 struct e1000_hw *hw = &adapter->hw;
2828 u32 mrqc, rxcsum;
Alexander Duyck797fd4b2012-09-13 06:28:11 +00002829 u32 j, num_rx_queues, shift = 0;
Alexander Duycka57fe232012-09-13 06:28:16 +00002830 static const u32 rsskey[10] = { 0xDA565A6D, 0xC20E5B25, 0x3D256741,
2831 0xB08FA343, 0xCB2BCAD0, 0xB4307BAE,
2832 0xA32DCB77, 0x0CF23080, 0x3BB7426A,
2833 0xFA01ACBE };
Alexander Duyck06cf2662009-10-27 15:53:25 +00002834
2835 /* Fill out hash function seeds */
Alexander Duycka57fe232012-09-13 06:28:16 +00002836 for (j = 0; j < 10; j++)
2837 wr32(E1000_RSSRK(j), rsskey[j]);
Alexander Duyck06cf2662009-10-27 15:53:25 +00002838
Alexander Duycka99955f2009-11-12 18:37:19 +00002839 num_rx_queues = adapter->rss_queues;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002840
Alexander Duyck797fd4b2012-09-13 06:28:11 +00002841 switch (hw->mac.type) {
2842 case e1000_82575:
2843 shift = 6;
2844 break;
2845 case e1000_82576:
2846 /* 82576 supports 2 RSS queues for SR-IOV */
2847 if (adapter->vfs_allocated_count) {
Alexander Duyck06cf2662009-10-27 15:53:25 +00002848 shift = 3;
2849 num_rx_queues = 2;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002850 }
Alexander Duyck797fd4b2012-09-13 06:28:11 +00002851 break;
2852 default:
2853 break;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002854 }
2855
Alexander Duyck797fd4b2012-09-13 06:28:11 +00002856 /*
2857 * Populate the indirection table 4 entries at a time. To do this
2858 * we are generating the results for n and n+2 and then interleaving
2859 * those with the results with n+1 and n+3.
2860 */
2861 for (j = 0; j < 32; j++) {
2862 /* first pass generates n and n+2 */
2863 u32 base = ((j * 0x00040004) + 0x00020000) * num_rx_queues;
2864 u32 reta = (base & 0x07800780) >> (7 - shift);
2865
2866 /* second pass generates n+1 and n+3 */
2867 base += 0x00010001 * num_rx_queues;
2868 reta |= (base & 0x07800780) << (1 + shift);
2869
2870 wr32(E1000_RETA(j), reta);
Alexander Duyck06cf2662009-10-27 15:53:25 +00002871 }
2872
2873 /*
2874 * Disable raw packet checksumming so that RSS hash is placed in
2875 * descriptor on writeback. No need to enable TCP/UDP/IP checksum
2876 * offloads as they are enabled by default
2877 */
2878 rxcsum = rd32(E1000_RXCSUM);
2879 rxcsum |= E1000_RXCSUM_PCSD;
2880
2881 if (adapter->hw.mac.type >= e1000_82576)
2882 /* Enable Receive Checksum Offload for SCTP */
2883 rxcsum |= E1000_RXCSUM_CRCOFL;
2884
2885 /* Don't need to set TUOFL or IPOFL, they default to 1 */
2886 wr32(E1000_RXCSUM, rxcsum);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002887
Akeem G. Abodunrin039454a2012-11-13 04:03:21 +00002888 /* Generate RSS hash based on packet types, TCP/UDP
2889 * port numbers and/or IPv4/v6 src and dst addresses
2890 */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002891 mrqc = E1000_MRQC_RSS_FIELD_IPV4 |
2892 E1000_MRQC_RSS_FIELD_IPV4_TCP |
2893 E1000_MRQC_RSS_FIELD_IPV6 |
2894 E1000_MRQC_RSS_FIELD_IPV6_TCP |
2895 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002896
Akeem G. Abodunrin039454a2012-11-13 04:03:21 +00002897 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV4_UDP)
2898 mrqc |= E1000_MRQC_RSS_FIELD_IPV4_UDP;
2899 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV6_UDP)
2900 mrqc |= E1000_MRQC_RSS_FIELD_IPV6_UDP;
2901
Alexander Duyck06cf2662009-10-27 15:53:25 +00002902 /* If VMDq is enabled then we set the appropriate mode for that, else
2903 * we default to RSS so that an RSS hash is calculated per packet even
2904 * if we are only using one queue */
2905 if (adapter->vfs_allocated_count) {
2906 if (hw->mac.type > e1000_82575) {
2907 /* Set the default pool for the PF's first queue */
2908 u32 vtctl = rd32(E1000_VT_CTL);
2909 vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
2910 E1000_VT_CTL_DISABLE_DEF_POOL);
2911 vtctl |= adapter->vfs_allocated_count <<
2912 E1000_VT_CTL_DEFAULT_POOL_SHIFT;
2913 wr32(E1000_VT_CTL, vtctl);
2914 }
Alexander Duycka99955f2009-11-12 18:37:19 +00002915 if (adapter->rss_queues > 1)
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002916 mrqc |= E1000_MRQC_ENABLE_VMDQ_RSS_2Q;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002917 else
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002918 mrqc |= E1000_MRQC_ENABLE_VMDQ;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002919 } else {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002920 if (hw->mac.type != e1000_i211)
2921 mrqc |= E1000_MRQC_ENABLE_RSS_4Q;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002922 }
2923 igb_vmm_control(adapter);
2924
Alexander Duyck06cf2662009-10-27 15:53:25 +00002925 wr32(E1000_MRQC, mrqc);
2926}
2927
2928/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002929 * igb_setup_rctl - configure the receive control registers
2930 * @adapter: Board private structure
2931 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002932void igb_setup_rctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002933{
2934 struct e1000_hw *hw = &adapter->hw;
2935 u32 rctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08002936
2937 rctl = rd32(E1000_RCTL);
2938
2939 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
Alexander Duyck69d728b2008-11-25 01:04:03 -08002940 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
Auke Kok9d5c8242008-01-24 02:22:38 -08002941
Alexander Duyck69d728b2008-11-25 01:04:03 -08002942 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
Alexander Duyck28b07592009-02-06 23:20:31 +00002943 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
Auke Kok9d5c8242008-01-24 02:22:38 -08002944
Auke Kok87cb7e82008-07-08 15:08:29 -07002945 /*
2946 * enable stripping of CRC. It's unlikely this will break BMC
2947 * redirection as it did with e1000. Newer features require
2948 * that the HW strips the CRC.
Alexander Duyck73cd78f2009-02-12 18:16:59 +00002949 */
Auke Kok87cb7e82008-07-08 15:08:29 -07002950 rctl |= E1000_RCTL_SECRC;
Auke Kok9d5c8242008-01-24 02:22:38 -08002951
Alexander Duyck559e9c42009-10-27 23:52:50 +00002952 /* disable store bad packets and clear size bits. */
Alexander Duyckec54d7d2009-01-31 00:52:57 -08002953 rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
Auke Kok9d5c8242008-01-24 02:22:38 -08002954
Alexander Duyck6ec43fe2009-10-27 15:50:48 +00002955 /* enable LPE to prevent packets larger than max_frame_size */
2956 rctl |= E1000_RCTL_LPE;
Auke Kok9d5c8242008-01-24 02:22:38 -08002957
Alexander Duyck952f72a2009-10-27 15:51:07 +00002958 /* disable queue 0 to prevent tail write w/o re-config */
2959 wr32(E1000_RXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08002960
Alexander Duycke1739522009-02-19 20:39:44 -08002961 /* Attention!!! For SR-IOV PF driver operations you must enable
2962 * queue drop for all VF and PF queues to prevent head of line blocking
2963 * if an un-trusted VF does not provide descriptors to hardware.
2964 */
2965 if (adapter->vfs_allocated_count) {
Alexander Duycke1739522009-02-19 20:39:44 -08002966 /* set all queue drop enable bits */
2967 wr32(E1000_QDE, ALL_QUEUES);
Alexander Duycke1739522009-02-19 20:39:44 -08002968 }
2969
Ben Greear89eaefb2012-03-06 09:41:58 +00002970 /* This is useful for sniffing bad packets. */
2971 if (adapter->netdev->features & NETIF_F_RXALL) {
2972 /* UPE and MPE will be handled by normal PROMISC logic
2973 * in e1000e_set_rx_mode */
2974 rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
2975 E1000_RCTL_BAM | /* RX All Bcast Pkts */
2976 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
2977
2978 rctl &= ~(E1000_RCTL_VFE | /* Disable VLAN filter */
2979 E1000_RCTL_DPF | /* Allow filtered pause */
2980 E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
2981 /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
2982 * and that breaks VLANs.
2983 */
2984 }
2985
Auke Kok9d5c8242008-01-24 02:22:38 -08002986 wr32(E1000_RCTL, rctl);
2987}
2988
Alexander Duyck7d5753f2009-10-27 23:47:16 +00002989static inline int igb_set_vf_rlpml(struct igb_adapter *adapter, int size,
2990 int vfn)
2991{
2992 struct e1000_hw *hw = &adapter->hw;
2993 u32 vmolr;
2994
2995 /* if it isn't the PF check to see if VFs are enabled and
2996 * increase the size to support vlan tags */
2997 if (vfn < adapter->vfs_allocated_count &&
2998 adapter->vf_data[vfn].vlans_enabled)
2999 size += VLAN_TAG_SIZE;
3000
3001 vmolr = rd32(E1000_VMOLR(vfn));
3002 vmolr &= ~E1000_VMOLR_RLPML_MASK;
3003 vmolr |= size | E1000_VMOLR_LPE;
3004 wr32(E1000_VMOLR(vfn), vmolr);
3005
3006 return 0;
3007}
3008
Auke Kok9d5c8242008-01-24 02:22:38 -08003009/**
Alexander Duycke1739522009-02-19 20:39:44 -08003010 * igb_rlpml_set - set maximum receive packet size
3011 * @adapter: board private structure
3012 *
3013 * Configure maximum receivable packet size.
3014 **/
3015static void igb_rlpml_set(struct igb_adapter *adapter)
3016{
Alexander Duyck153285f2011-08-26 07:43:32 +00003017 u32 max_frame_size = adapter->max_frame_size;
Alexander Duycke1739522009-02-19 20:39:44 -08003018 struct e1000_hw *hw = &adapter->hw;
3019 u16 pf_id = adapter->vfs_allocated_count;
3020
Alexander Duycke1739522009-02-19 20:39:44 -08003021 if (pf_id) {
3022 igb_set_vf_rlpml(adapter, max_frame_size, pf_id);
Alexander Duyck153285f2011-08-26 07:43:32 +00003023 /*
3024 * If we're in VMDQ or SR-IOV mode, then set global RLPML
3025 * to our max jumbo frame size, in case we need to enable
3026 * jumbo frames on one of the rings later.
3027 * This will not pass over-length frames into the default
3028 * queue because it's gated by the VMOLR.RLPML.
3029 */
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003030 max_frame_size = MAX_JUMBO_FRAME_SIZE;
Alexander Duycke1739522009-02-19 20:39:44 -08003031 }
3032
3033 wr32(E1000_RLPML, max_frame_size);
3034}
3035
Williams, Mitch A8151d292010-02-10 01:44:24 +00003036static inline void igb_set_vmolr(struct igb_adapter *adapter,
3037 int vfn, bool aupe)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003038{
3039 struct e1000_hw *hw = &adapter->hw;
3040 u32 vmolr;
3041
3042 /*
3043 * This register exists only on 82576 and newer so if we are older then
3044 * we should exit and do nothing
3045 */
3046 if (hw->mac.type < e1000_82576)
3047 return;
3048
3049 vmolr = rd32(E1000_VMOLR(vfn));
Williams, Mitch A8151d292010-02-10 01:44:24 +00003050 vmolr |= E1000_VMOLR_STRVLAN; /* Strip vlan tags */
3051 if (aupe)
3052 vmolr |= E1000_VMOLR_AUPE; /* Accept untagged packets */
3053 else
3054 vmolr &= ~(E1000_VMOLR_AUPE); /* Tagged packets ONLY */
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003055
3056 /* clear all bits that might not be set */
3057 vmolr &= ~(E1000_VMOLR_BAM | E1000_VMOLR_RSSE);
3058
Alexander Duycka99955f2009-11-12 18:37:19 +00003059 if (adapter->rss_queues > 1 && vfn == adapter->vfs_allocated_count)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003060 vmolr |= E1000_VMOLR_RSSE; /* enable RSS */
3061 /*
3062 * for VMDq only allow the VFs and pool 0 to accept broadcast and
3063 * multicast packets
3064 */
3065 if (vfn <= adapter->vfs_allocated_count)
3066 vmolr |= E1000_VMOLR_BAM; /* Accept broadcast */
3067
3068 wr32(E1000_VMOLR(vfn), vmolr);
3069}
3070
Alexander Duycke1739522009-02-19 20:39:44 -08003071/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00003072 * igb_configure_rx_ring - Configure a receive ring after Reset
3073 * @adapter: board private structure
3074 * @ring: receive ring to be configured
3075 *
3076 * Configure the Rx unit of the MAC after a reset.
3077 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00003078void igb_configure_rx_ring(struct igb_adapter *adapter,
3079 struct igb_ring *ring)
Alexander Duyck85b430b2009-10-27 15:50:29 +00003080{
3081 struct e1000_hw *hw = &adapter->hw;
3082 u64 rdba = ring->dma;
3083 int reg_idx = ring->reg_idx;
Alexander Duycka74420e2011-08-26 07:43:27 +00003084 u32 srrctl = 0, rxdctl = 0;
Alexander Duyck85b430b2009-10-27 15:50:29 +00003085
3086 /* disable the queue */
Alexander Duycka74420e2011-08-26 07:43:27 +00003087 wr32(E1000_RXDCTL(reg_idx), 0);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003088
3089 /* Set DMA base address registers */
3090 wr32(E1000_RDBAL(reg_idx),
3091 rdba & 0x00000000ffffffffULL);
3092 wr32(E1000_RDBAH(reg_idx), rdba >> 32);
3093 wr32(E1000_RDLEN(reg_idx),
3094 ring->count * sizeof(union e1000_adv_rx_desc));
3095
3096 /* initialize head and tail */
Alexander Duyckfce99e32009-10-27 15:51:27 +00003097 ring->tail = hw->hw_addr + E1000_RDT(reg_idx);
Alexander Duycka74420e2011-08-26 07:43:27 +00003098 wr32(E1000_RDH(reg_idx), 0);
Alexander Duyckfce99e32009-10-27 15:51:27 +00003099 writel(0, ring->tail);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003100
Alexander Duyck952f72a2009-10-27 15:51:07 +00003101 /* set descriptor configuration */
Alexander Duyck44390ca2011-08-26 07:43:38 +00003102 srrctl = IGB_RX_HDR_LEN << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
Alexander Duyckde78d1f2012-09-25 00:31:12 +00003103 srrctl |= IGB_RX_BUFSZ >> E1000_SRRCTL_BSIZEPKT_SHIFT;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003104 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
Alexander Duyck06218a82011-08-26 07:46:55 +00003105 if (hw->mac.type >= e1000_82580)
Nick Nunley757b77e2010-03-26 11:36:47 +00003106 srrctl |= E1000_SRRCTL_TIMESTAMP;
Nick Nunleye6bdb6f2010-02-17 01:03:38 +00003107 /* Only set Drop Enable if we are supporting multiple queues */
3108 if (adapter->vfs_allocated_count || adapter->num_rx_queues > 1)
3109 srrctl |= E1000_SRRCTL_DROP_EN;
Alexander Duyck952f72a2009-10-27 15:51:07 +00003110
3111 wr32(E1000_SRRCTL(reg_idx), srrctl);
3112
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003113 /* set filtering for VMDQ pools */
Williams, Mitch A8151d292010-02-10 01:44:24 +00003114 igb_set_vmolr(adapter, reg_idx & 0x7, true);
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003115
Alexander Duyck85b430b2009-10-27 15:50:29 +00003116 rxdctl |= IGB_RX_PTHRESH;
3117 rxdctl |= IGB_RX_HTHRESH << 8;
3118 rxdctl |= IGB_RX_WTHRESH << 16;
Alexander Duycka74420e2011-08-26 07:43:27 +00003119
3120 /* enable receive descriptor fetching */
3121 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
Alexander Duyck85b430b2009-10-27 15:50:29 +00003122 wr32(E1000_RXDCTL(reg_idx), rxdctl);
3123}
3124
3125/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003126 * igb_configure_rx - Configure receive Unit after Reset
3127 * @adapter: board private structure
3128 *
3129 * Configure the Rx unit of the MAC after a reset.
3130 **/
3131static void igb_configure_rx(struct igb_adapter *adapter)
3132{
Hannes Eder91075842009-02-18 19:36:04 -08003133 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003134
Alexander Duyck68d480c2009-10-05 06:33:08 +00003135 /* set UTA to appropriate mode */
3136 igb_set_uta(adapter);
3137
Alexander Duyck26ad9172009-10-05 06:32:49 +00003138 /* set the correct pool for the PF default MAC address in entry 0 */
3139 igb_rar_set_qsel(adapter, adapter->hw.mac.addr, 0,
3140 adapter->vfs_allocated_count);
3141
Alexander Duyck06cf2662009-10-27 15:53:25 +00003142 /* Setup the HW Rx Head and Tail Descriptor Pointers and
3143 * the Base and Length of the Rx Descriptor Ring */
3144 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003145 igb_configure_rx_ring(adapter, adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003146}
3147
3148/**
3149 * igb_free_tx_resources - Free Tx Resources per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08003150 * @tx_ring: Tx descriptor ring for a specific queue
3151 *
3152 * Free all transmit software resources
3153 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08003154void igb_free_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003155{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003156 igb_clean_tx_ring(tx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003157
Alexander Duyck06034642011-08-26 07:44:22 +00003158 vfree(tx_ring->tx_buffer_info);
3159 tx_ring->tx_buffer_info = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003160
Alexander Duyck439705e2009-10-27 23:49:20 +00003161 /* if not set, then don't free */
3162 if (!tx_ring->desc)
3163 return;
3164
Alexander Duyck59d71982010-04-27 13:09:25 +00003165 dma_free_coherent(tx_ring->dev, tx_ring->size,
3166 tx_ring->desc, tx_ring->dma);
Auke Kok9d5c8242008-01-24 02:22:38 -08003167
3168 tx_ring->desc = NULL;
3169}
3170
3171/**
3172 * igb_free_all_tx_resources - Free Tx Resources for All Queues
3173 * @adapter: board private structure
3174 *
3175 * Free all transmit software resources
3176 **/
3177static void igb_free_all_tx_resources(struct igb_adapter *adapter)
3178{
3179 int i;
3180
3181 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003182 igb_free_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003183}
3184
Alexander Duyckebe42d12011-08-26 07:45:09 +00003185void igb_unmap_and_free_tx_resource(struct igb_ring *ring,
3186 struct igb_tx_buffer *tx_buffer)
Auke Kok9d5c8242008-01-24 02:22:38 -08003187{
Alexander Duyckebe42d12011-08-26 07:45:09 +00003188 if (tx_buffer->skb) {
3189 dev_kfree_skb_any(tx_buffer->skb);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003190 if (dma_unmap_len(tx_buffer, len))
Alexander Duyckebe42d12011-08-26 07:45:09 +00003191 dma_unmap_single(ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003192 dma_unmap_addr(tx_buffer, dma),
3193 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00003194 DMA_TO_DEVICE);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003195 } else if (dma_unmap_len(tx_buffer, len)) {
Alexander Duyckebe42d12011-08-26 07:45:09 +00003196 dma_unmap_page(ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003197 dma_unmap_addr(tx_buffer, dma),
3198 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00003199 DMA_TO_DEVICE);
Alexander Duyck6366ad32009-12-02 16:47:18 +00003200 }
Alexander Duyckebe42d12011-08-26 07:45:09 +00003201 tx_buffer->next_to_watch = NULL;
3202 tx_buffer->skb = NULL;
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003203 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duyckebe42d12011-08-26 07:45:09 +00003204 /* buffer_info must be completely set up in the transmit path */
Auke Kok9d5c8242008-01-24 02:22:38 -08003205}
3206
3207/**
3208 * igb_clean_tx_ring - Free Tx Buffers
Auke Kok9d5c8242008-01-24 02:22:38 -08003209 * @tx_ring: ring to be cleaned
3210 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07003211static void igb_clean_tx_ring(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003212{
Alexander Duyck06034642011-08-26 07:44:22 +00003213 struct igb_tx_buffer *buffer_info;
Auke Kok9d5c8242008-01-24 02:22:38 -08003214 unsigned long size;
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00003215 u16 i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003216
Alexander Duyck06034642011-08-26 07:44:22 +00003217 if (!tx_ring->tx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003218 return;
3219 /* Free all the Tx ring sk_buffs */
3220
3221 for (i = 0; i < tx_ring->count; i++) {
Alexander Duyck06034642011-08-26 07:44:22 +00003222 buffer_info = &tx_ring->tx_buffer_info[i];
Alexander Duyck80785292009-10-27 15:51:47 +00003223 igb_unmap_and_free_tx_resource(tx_ring, buffer_info);
Auke Kok9d5c8242008-01-24 02:22:38 -08003224 }
3225
John Fastabenddad8a3b2012-04-23 12:22:39 +00003226 netdev_tx_reset_queue(txring_txq(tx_ring));
3227
Alexander Duyck06034642011-08-26 07:44:22 +00003228 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
3229 memset(tx_ring->tx_buffer_info, 0, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08003230
3231 /* Zero out the descriptor ring */
Auke Kok9d5c8242008-01-24 02:22:38 -08003232 memset(tx_ring->desc, 0, tx_ring->size);
3233
3234 tx_ring->next_to_use = 0;
3235 tx_ring->next_to_clean = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003236}
3237
3238/**
3239 * igb_clean_all_tx_rings - Free Tx Buffers for all queues
3240 * @adapter: board private structure
3241 **/
3242static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
3243{
3244 int i;
3245
3246 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003247 igb_clean_tx_ring(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003248}
3249
3250/**
3251 * igb_free_rx_resources - Free Rx Resources
Auke Kok9d5c8242008-01-24 02:22:38 -08003252 * @rx_ring: ring to clean the resources from
3253 *
3254 * Free all receive software resources
3255 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08003256void igb_free_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003257{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003258 igb_clean_rx_ring(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003259
Alexander Duyck06034642011-08-26 07:44:22 +00003260 vfree(rx_ring->rx_buffer_info);
3261 rx_ring->rx_buffer_info = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003262
Alexander Duyck439705e2009-10-27 23:49:20 +00003263 /* if not set, then don't free */
3264 if (!rx_ring->desc)
3265 return;
3266
Alexander Duyck59d71982010-04-27 13:09:25 +00003267 dma_free_coherent(rx_ring->dev, rx_ring->size,
3268 rx_ring->desc, rx_ring->dma);
Auke Kok9d5c8242008-01-24 02:22:38 -08003269
3270 rx_ring->desc = NULL;
3271}
3272
3273/**
3274 * igb_free_all_rx_resources - Free Rx Resources for All Queues
3275 * @adapter: board private structure
3276 *
3277 * Free all receive software resources
3278 **/
3279static void igb_free_all_rx_resources(struct igb_adapter *adapter)
3280{
3281 int i;
3282
3283 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003284 igb_free_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003285}
3286
3287/**
3288 * igb_clean_rx_ring - Free Rx Buffers per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08003289 * @rx_ring: ring to free buffers from
3290 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07003291static void igb_clean_rx_ring(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003292{
Auke Kok9d5c8242008-01-24 02:22:38 -08003293 unsigned long size;
Alexander Duyckc023cd82011-08-26 07:43:43 +00003294 u16 i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003295
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003296 if (rx_ring->skb)
3297 dev_kfree_skb(rx_ring->skb);
3298 rx_ring->skb = NULL;
3299
Alexander Duyck06034642011-08-26 07:44:22 +00003300 if (!rx_ring->rx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003301 return;
Alexander Duyck439705e2009-10-27 23:49:20 +00003302
Auke Kok9d5c8242008-01-24 02:22:38 -08003303 /* Free all the Rx ring sk_buffs */
3304 for (i = 0; i < rx_ring->count; i++) {
Alexander Duyck06034642011-08-26 07:44:22 +00003305 struct igb_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08003306
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003307 if (!buffer_info->page)
3308 continue;
3309
3310 dma_unmap_page(rx_ring->dev,
3311 buffer_info->dma,
3312 PAGE_SIZE,
3313 DMA_FROM_DEVICE);
3314 __free_page(buffer_info->page);
3315
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003316 buffer_info->page = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003317 }
3318
Alexander Duyck06034642011-08-26 07:44:22 +00003319 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
3320 memset(rx_ring->rx_buffer_info, 0, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08003321
3322 /* Zero out the descriptor ring */
3323 memset(rx_ring->desc, 0, rx_ring->size);
3324
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003325 rx_ring->next_to_alloc = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003326 rx_ring->next_to_clean = 0;
3327 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003328}
3329
3330/**
3331 * igb_clean_all_rx_rings - Free Rx Buffers for all queues
3332 * @adapter: board private structure
3333 **/
3334static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
3335{
3336 int i;
3337
3338 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003339 igb_clean_rx_ring(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003340}
3341
3342/**
3343 * igb_set_mac - Change the Ethernet Address of the NIC
3344 * @netdev: network interface device structure
3345 * @p: pointer to an address structure
3346 *
3347 * Returns 0 on success, negative on failure
3348 **/
3349static int igb_set_mac(struct net_device *netdev, void *p)
3350{
3351 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck28b07592009-02-06 23:20:31 +00003352 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08003353 struct sockaddr *addr = p;
3354
3355 if (!is_valid_ether_addr(addr->sa_data))
3356 return -EADDRNOTAVAIL;
3357
3358 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Alexander Duyck28b07592009-02-06 23:20:31 +00003359 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
Auke Kok9d5c8242008-01-24 02:22:38 -08003360
Alexander Duyck26ad9172009-10-05 06:32:49 +00003361 /* set the correct pool for the new PF MAC address in entry 0 */
3362 igb_rar_set_qsel(adapter, hw->mac.addr, 0,
3363 adapter->vfs_allocated_count);
Alexander Duycke1739522009-02-19 20:39:44 -08003364
Auke Kok9d5c8242008-01-24 02:22:38 -08003365 return 0;
3366}
3367
3368/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00003369 * igb_write_mc_addr_list - write multicast addresses to MTA
3370 * @netdev: network interface device structure
3371 *
3372 * Writes multicast address list to the MTA hash table.
3373 * Returns: -ENOMEM on failure
3374 * 0 on no addresses written
3375 * X on writing X addresses to MTA
3376 **/
3377static int igb_write_mc_addr_list(struct net_device *netdev)
3378{
3379 struct igb_adapter *adapter = netdev_priv(netdev);
3380 struct e1000_hw *hw = &adapter->hw;
Jiri Pirko22bedad32010-04-01 21:22:57 +00003381 struct netdev_hw_addr *ha;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003382 u8 *mta_list;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003383 int i;
3384
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003385 if (netdev_mc_empty(netdev)) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003386 /* nothing to program, so clear mc list */
3387 igb_update_mc_addr_list(hw, NULL, 0);
3388 igb_restore_vf_multicasts(adapter);
3389 return 0;
3390 }
3391
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003392 mta_list = kzalloc(netdev_mc_count(netdev) * 6, GFP_ATOMIC);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003393 if (!mta_list)
3394 return -ENOMEM;
3395
Alexander Duyck68d480c2009-10-05 06:33:08 +00003396 /* The shared function expects a packed array of only addresses. */
Jiri Pirko48e2f182010-02-22 09:22:26 +00003397 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00003398 netdev_for_each_mc_addr(ha, netdev)
3399 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003400
Alexander Duyck68d480c2009-10-05 06:33:08 +00003401 igb_update_mc_addr_list(hw, mta_list, i);
3402 kfree(mta_list);
3403
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003404 return netdev_mc_count(netdev);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003405}
3406
3407/**
3408 * igb_write_uc_addr_list - write unicast addresses to RAR table
3409 * @netdev: network interface device structure
3410 *
3411 * Writes unicast address list to the RAR table.
3412 * Returns: -ENOMEM on failure/insufficient address space
3413 * 0 on no addresses written
3414 * X on writing X addresses to the RAR table
3415 **/
3416static int igb_write_uc_addr_list(struct net_device *netdev)
3417{
3418 struct igb_adapter *adapter = netdev_priv(netdev);
3419 struct e1000_hw *hw = &adapter->hw;
3420 unsigned int vfn = adapter->vfs_allocated_count;
3421 unsigned int rar_entries = hw->mac.rar_entry_count - (vfn + 1);
3422 int count = 0;
3423
3424 /* return ENOMEM indicating insufficient memory for addresses */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003425 if (netdev_uc_count(netdev) > rar_entries)
Alexander Duyck68d480c2009-10-05 06:33:08 +00003426 return -ENOMEM;
3427
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003428 if (!netdev_uc_empty(netdev) && rar_entries) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003429 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003430
3431 netdev_for_each_uc_addr(ha, netdev) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003432 if (!rar_entries)
3433 break;
3434 igb_rar_set_qsel(adapter, ha->addr,
3435 rar_entries--,
3436 vfn);
3437 count++;
3438 }
3439 }
3440 /* write the addresses in reverse order to avoid write combining */
3441 for (; rar_entries > 0 ; rar_entries--) {
3442 wr32(E1000_RAH(rar_entries), 0);
3443 wr32(E1000_RAL(rar_entries), 0);
3444 }
3445 wrfl();
3446
3447 return count;
3448}
3449
3450/**
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003451 * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
Auke Kok9d5c8242008-01-24 02:22:38 -08003452 * @netdev: network interface device structure
3453 *
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003454 * The set_rx_mode entry point is called whenever the unicast or multicast
3455 * address lists or the network interface flags are updated. This routine is
3456 * responsible for configuring the hardware for proper unicast, multicast,
Auke Kok9d5c8242008-01-24 02:22:38 -08003457 * promiscuous mode, and all-multi behavior.
3458 **/
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003459static void igb_set_rx_mode(struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08003460{
3461 struct igb_adapter *adapter = netdev_priv(netdev);
3462 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003463 unsigned int vfn = adapter->vfs_allocated_count;
3464 u32 rctl, vmolr = 0;
3465 int count;
Auke Kok9d5c8242008-01-24 02:22:38 -08003466
3467 /* Check for Promiscuous and All Multicast modes */
Auke Kok9d5c8242008-01-24 02:22:38 -08003468 rctl = rd32(E1000_RCTL);
3469
Alexander Duyck68d480c2009-10-05 06:33:08 +00003470 /* clear the effected bits */
3471 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_VFE);
3472
Patrick McHardy746b9f02008-07-16 20:15:45 -07003473 if (netdev->flags & IFF_PROMISC) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003474 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003475 vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_MPME);
Patrick McHardy746b9f02008-07-16 20:15:45 -07003476 } else {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003477 if (netdev->flags & IFF_ALLMULTI) {
Patrick McHardy746b9f02008-07-16 20:15:45 -07003478 rctl |= E1000_RCTL_MPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003479 vmolr |= E1000_VMOLR_MPME;
3480 } else {
3481 /*
3482 * Write addresses to the MTA, if the attempt fails
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003483 * then we should just turn on promiscuous mode so
Alexander Duyck68d480c2009-10-05 06:33:08 +00003484 * that we can at least receive multicast traffic
3485 */
3486 count = igb_write_mc_addr_list(netdev);
3487 if (count < 0) {
3488 rctl |= E1000_RCTL_MPE;
3489 vmolr |= E1000_VMOLR_MPME;
3490 } else if (count) {
3491 vmolr |= E1000_VMOLR_ROMPE;
3492 }
3493 }
3494 /*
3495 * Write addresses to available RAR registers, if there is not
3496 * sufficient space to store all the addresses then enable
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003497 * unicast promiscuous mode
Alexander Duyck68d480c2009-10-05 06:33:08 +00003498 */
3499 count = igb_write_uc_addr_list(netdev);
3500 if (count < 0) {
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003501 rctl |= E1000_RCTL_UPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003502 vmolr |= E1000_VMOLR_ROPE;
3503 }
Patrick McHardy78ed11a2008-07-16 20:16:14 -07003504 rctl |= E1000_RCTL_VFE;
Patrick McHardy746b9f02008-07-16 20:15:45 -07003505 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003506 wr32(E1000_RCTL, rctl);
3507
Alexander Duyck68d480c2009-10-05 06:33:08 +00003508 /*
3509 * In order to support SR-IOV and eventually VMDq it is necessary to set
3510 * the VMOLR to enable the appropriate modes. Without this workaround
3511 * we will have issues with VLAN tag stripping not being done for frames
3512 * that are only arriving because we are the default pool
3513 */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003514 if ((hw->mac.type < e1000_82576) || (hw->mac.type > e1000_i350))
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003515 return;
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003516
Alexander Duyck68d480c2009-10-05 06:33:08 +00003517 vmolr |= rd32(E1000_VMOLR(vfn)) &
3518 ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE);
3519 wr32(E1000_VMOLR(vfn), vmolr);
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003520 igb_restore_vf_multicasts(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003521}
3522
Greg Rose13800462010-11-06 02:08:26 +00003523static void igb_check_wvbr(struct igb_adapter *adapter)
3524{
3525 struct e1000_hw *hw = &adapter->hw;
3526 u32 wvbr = 0;
3527
3528 switch (hw->mac.type) {
3529 case e1000_82576:
3530 case e1000_i350:
3531 if (!(wvbr = rd32(E1000_WVBR)))
3532 return;
3533 break;
3534 default:
3535 break;
3536 }
3537
3538 adapter->wvbr |= wvbr;
3539}
3540
3541#define IGB_STAGGERED_QUEUE_OFFSET 8
3542
3543static void igb_spoof_check(struct igb_adapter *adapter)
3544{
3545 int j;
3546
3547 if (!adapter->wvbr)
3548 return;
3549
3550 for(j = 0; j < adapter->vfs_allocated_count; j++) {
3551 if (adapter->wvbr & (1 << j) ||
3552 adapter->wvbr & (1 << (j + IGB_STAGGERED_QUEUE_OFFSET))) {
3553 dev_warn(&adapter->pdev->dev,
3554 "Spoof event(s) detected on VF %d\n", j);
3555 adapter->wvbr &=
3556 ~((1 << j) |
3557 (1 << (j + IGB_STAGGERED_QUEUE_OFFSET)));
3558 }
3559 }
3560}
3561
Auke Kok9d5c8242008-01-24 02:22:38 -08003562/* Need to wait a few seconds after link up to get diagnostic information from
3563 * the phy */
3564static void igb_update_phy_info(unsigned long data)
3565{
3566 struct igb_adapter *adapter = (struct igb_adapter *) data;
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08003567 igb_get_phy_info(&adapter->hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08003568}
3569
3570/**
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003571 * igb_has_link - check shared code for link and determine up/down
3572 * @adapter: pointer to driver private info
3573 **/
Nick Nunley31455352010-02-17 01:01:21 +00003574bool igb_has_link(struct igb_adapter *adapter)
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003575{
3576 struct e1000_hw *hw = &adapter->hw;
3577 bool link_active = false;
3578 s32 ret_val = 0;
3579
3580 /* get_link_status is set on LSC (link status) interrupt or
3581 * rx sequence error interrupt. get_link_status will stay
3582 * false until the e1000_check_for_link establishes link
3583 * for copper adapters ONLY
3584 */
3585 switch (hw->phy.media_type) {
3586 case e1000_media_type_copper:
3587 if (hw->mac.get_link_status) {
3588 ret_val = hw->mac.ops.check_for_link(hw);
3589 link_active = !hw->mac.get_link_status;
3590 } else {
3591 link_active = true;
3592 }
3593 break;
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003594 case e1000_media_type_internal_serdes:
3595 ret_val = hw->mac.ops.check_for_link(hw);
3596 link_active = hw->mac.serdes_has_link;
3597 break;
3598 default:
3599 case e1000_media_type_unknown:
3600 break;
3601 }
3602
3603 return link_active;
3604}
3605
Stefan Assmann563988d2011-04-05 04:27:15 +00003606static bool igb_thermal_sensor_event(struct e1000_hw *hw, u32 event)
3607{
3608 bool ret = false;
3609 u32 ctrl_ext, thstat;
3610
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003611 /* check for thermal sensor event on i350 copper only */
Stefan Assmann563988d2011-04-05 04:27:15 +00003612 if (hw->mac.type == e1000_i350) {
3613 thstat = rd32(E1000_THSTAT);
3614 ctrl_ext = rd32(E1000_CTRL_EXT);
3615
3616 if ((hw->phy.media_type == e1000_media_type_copper) &&
3617 !(ctrl_ext & E1000_CTRL_EXT_LINK_MODE_SGMII)) {
3618 ret = !!(thstat & event);
3619 }
3620 }
3621
3622 return ret;
3623}
3624
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003625/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003626 * igb_watchdog - Timer Call-back
3627 * @data: pointer to adapter cast into an unsigned long
3628 **/
3629static void igb_watchdog(unsigned long data)
3630{
3631 struct igb_adapter *adapter = (struct igb_adapter *)data;
3632 /* Do the rest outside of interrupt context */
3633 schedule_work(&adapter->watchdog_task);
3634}
3635
3636static void igb_watchdog_task(struct work_struct *work)
3637{
3638 struct igb_adapter *adapter = container_of(work,
Alexander Duyck559e9c42009-10-27 23:52:50 +00003639 struct igb_adapter,
3640 watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08003641 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08003642 struct net_device *netdev = adapter->netdev;
Stefan Assmann563988d2011-04-05 04:27:15 +00003643 u32 link;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003644 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003645
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003646 link = igb_has_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003647 if (link) {
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003648 /* Cancel scheduled suspend requests. */
3649 pm_runtime_resume(netdev->dev.parent);
3650
Auke Kok9d5c8242008-01-24 02:22:38 -08003651 if (!netif_carrier_ok(netdev)) {
3652 u32 ctrl;
Alexander Duyck330a6d62009-10-27 23:51:35 +00003653 hw->mac.ops.get_speed_and_duplex(hw,
3654 &adapter->link_speed,
3655 &adapter->link_duplex);
Auke Kok9d5c8242008-01-24 02:22:38 -08003656
3657 ctrl = rd32(E1000_CTRL);
Alexander Duyck527d47c2008-11-27 00:21:39 -08003658 /* Links status message must follow this format */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003659 printk(KERN_INFO "igb: %s NIC Link is Up %d Mbps %s "
3660 "Duplex, Flow Control: %s\n",
Alexander Duyck559e9c42009-10-27 23:52:50 +00003661 netdev->name,
3662 adapter->link_speed,
3663 adapter->link_duplex == FULL_DUPLEX ?
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003664 "Full" : "Half",
3665 (ctrl & E1000_CTRL_TFCE) &&
3666 (ctrl & E1000_CTRL_RFCE) ? "RX/TX" :
3667 (ctrl & E1000_CTRL_RFCE) ? "RX" :
3668 (ctrl & E1000_CTRL_TFCE) ? "TX" : "None");
Auke Kok9d5c8242008-01-24 02:22:38 -08003669
Stefan Assmann563988d2011-04-05 04:27:15 +00003670 /* check for thermal sensor event */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003671 if (igb_thermal_sensor_event(hw,
3672 E1000_THSTAT_LINK_THROTTLE)) {
3673 netdev_info(netdev, "The network adapter link "
3674 "speed was downshifted because it "
3675 "overheated\n");
Carolyn Wyborny7ef5ed12011-03-12 08:59:47 +00003676 }
Stefan Assmann563988d2011-04-05 04:27:15 +00003677
Emil Tantilovd07f3e32010-03-23 18:34:57 +00003678 /* adjust timeout factor according to speed/duplex */
Auke Kok9d5c8242008-01-24 02:22:38 -08003679 adapter->tx_timeout_factor = 1;
3680 switch (adapter->link_speed) {
3681 case SPEED_10:
Auke Kok9d5c8242008-01-24 02:22:38 -08003682 adapter->tx_timeout_factor = 14;
3683 break;
3684 case SPEED_100:
Auke Kok9d5c8242008-01-24 02:22:38 -08003685 /* maybe add some timeout factor ? */
3686 break;
3687 }
3688
3689 netif_carrier_on(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08003690
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003691 igb_ping_all_vfs(adapter);
Lior Levy17dc5662011-02-08 02:28:46 +00003692 igb_check_vf_rate_limit(adapter);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003693
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003694 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08003695 if (!test_bit(__IGB_DOWN, &adapter->state))
3696 mod_timer(&adapter->phy_info_timer,
3697 round_jiffies(jiffies + 2 * HZ));
3698 }
3699 } else {
3700 if (netif_carrier_ok(netdev)) {
3701 adapter->link_speed = 0;
3702 adapter->link_duplex = 0;
Stefan Assmann563988d2011-04-05 04:27:15 +00003703
3704 /* check for thermal sensor event */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003705 if (igb_thermal_sensor_event(hw,
3706 E1000_THSTAT_PWR_DOWN)) {
3707 netdev_err(netdev, "The network adapter was "
3708 "stopped because it overheated\n");
Carolyn Wyborny7ef5ed12011-03-12 08:59:47 +00003709 }
Stefan Assmann563988d2011-04-05 04:27:15 +00003710
Alexander Duyck527d47c2008-11-27 00:21:39 -08003711 /* Links status message must follow this format */
3712 printk(KERN_INFO "igb: %s NIC Link is Down\n",
3713 netdev->name);
Auke Kok9d5c8242008-01-24 02:22:38 -08003714 netif_carrier_off(netdev);
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003715
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003716 igb_ping_all_vfs(adapter);
3717
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003718 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08003719 if (!test_bit(__IGB_DOWN, &adapter->state))
3720 mod_timer(&adapter->phy_info_timer,
3721 round_jiffies(jiffies + 2 * HZ));
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003722
3723 pm_schedule_suspend(netdev->dev.parent,
3724 MSEC_PER_SEC * 5);
Auke Kok9d5c8242008-01-24 02:22:38 -08003725 }
3726 }
3727
Eric Dumazet12dcd862010-10-15 17:27:10 +00003728 spin_lock(&adapter->stats64_lock);
3729 igb_update_stats(adapter, &adapter->stats64);
3730 spin_unlock(&adapter->stats64_lock);
Auke Kok9d5c8242008-01-24 02:22:38 -08003731
Alexander Duyckdbabb062009-11-12 18:38:16 +00003732 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00003733 struct igb_ring *tx_ring = adapter->tx_ring[i];
Alexander Duyckdbabb062009-11-12 18:38:16 +00003734 if (!netif_carrier_ok(netdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003735 /* We've lost link, so the controller stops DMA,
3736 * but we've got queued Tx work that's never going
3737 * to get done, so reset controller to flush Tx.
3738 * (Do the reset outside of interrupt context). */
Alexander Duyckdbabb062009-11-12 18:38:16 +00003739 if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) {
3740 adapter->tx_timeout_count++;
3741 schedule_work(&adapter->reset_task);
3742 /* return immediately since reset is imminent */
3743 return;
3744 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003745 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003746
Alexander Duyckdbabb062009-11-12 18:38:16 +00003747 /* Force detection of hung controller every watchdog period */
Alexander Duyck6d095fa2011-08-26 07:46:19 +00003748 set_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
Alexander Duyckdbabb062009-11-12 18:38:16 +00003749 }
Alexander Duyckf7ba2052009-10-27 23:48:51 +00003750
Auke Kok9d5c8242008-01-24 02:22:38 -08003751 /* Cause software interrupt to ensure rx ring is cleaned */
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003752 if (adapter->msix_entries) {
Alexander Duyck047e0032009-10-27 15:49:27 +00003753 u32 eics = 0;
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00003754 for (i = 0; i < adapter->num_q_vectors; i++)
3755 eics |= adapter->q_vector[i]->eims_value;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003756 wr32(E1000_EICS, eics);
3757 } else {
3758 wr32(E1000_ICS, E1000_ICS_RXDMT0);
3759 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003760
Greg Rose13800462010-11-06 02:08:26 +00003761 igb_spoof_check(adapter);
3762
Auke Kok9d5c8242008-01-24 02:22:38 -08003763 /* Reset the timer */
3764 if (!test_bit(__IGB_DOWN, &adapter->state))
3765 mod_timer(&adapter->watchdog_timer,
3766 round_jiffies(jiffies + 2 * HZ));
3767}
3768
3769enum latency_range {
3770 lowest_latency = 0,
3771 low_latency = 1,
3772 bulk_latency = 2,
3773 latency_invalid = 255
3774};
3775
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003776/**
3777 * igb_update_ring_itr - update the dynamic ITR value based on packet size
3778 *
3779 * Stores a new ITR value based on strictly on packet size. This
3780 * algorithm is less sophisticated than that used in igb_update_itr,
3781 * due to the difficulty of synchronizing statistics across multiple
Stefan Weileef35c22010-08-06 21:11:15 +02003782 * receive rings. The divisors and thresholds used by this function
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003783 * were determined based on theoretical maximum wire speed and testing
3784 * data, in order to minimize response time while increasing bulk
3785 * throughput.
3786 * This functionality is controlled by the InterruptThrottleRate module
3787 * parameter (see igb_param.c)
3788 * NOTE: This function is called only when operating in a multiqueue
3789 * receive environment.
Alexander Duyck047e0032009-10-27 15:49:27 +00003790 * @q_vector: pointer to q_vector
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003791 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00003792static void igb_update_ring_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08003793{
Alexander Duyck047e0032009-10-27 15:49:27 +00003794 int new_val = q_vector->itr_val;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003795 int avg_wire_size = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +00003796 struct igb_adapter *adapter = q_vector->adapter;
Eric Dumazet12dcd862010-10-15 17:27:10 +00003797 unsigned int packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08003798
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003799 /* For non-gigabit speeds, just fix the interrupt rate at 4000
3800 * ints/sec - ITR timer value of 120 ticks.
3801 */
3802 if (adapter->link_speed != SPEED_1000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003803 new_val = IGB_4K_ITR;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003804 goto set_itr_val;
3805 }
Alexander Duyck047e0032009-10-27 15:49:27 +00003806
Alexander Duyck0ba82992011-08-26 07:45:47 +00003807 packets = q_vector->rx.total_packets;
3808 if (packets)
3809 avg_wire_size = q_vector->rx.total_bytes / packets;
Eric Dumazet12dcd862010-10-15 17:27:10 +00003810
Alexander Duyck0ba82992011-08-26 07:45:47 +00003811 packets = q_vector->tx.total_packets;
3812 if (packets)
3813 avg_wire_size = max_t(u32, avg_wire_size,
3814 q_vector->tx.total_bytes / packets);
Alexander Duyck047e0032009-10-27 15:49:27 +00003815
3816 /* if avg_wire_size isn't set no work was done */
3817 if (!avg_wire_size)
3818 goto clear_counts;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003819
3820 /* Add 24 bytes to size to account for CRC, preamble, and gap */
3821 avg_wire_size += 24;
3822
3823 /* Don't starve jumbo frames */
3824 avg_wire_size = min(avg_wire_size, 3000);
3825
3826 /* Give a little boost to mid-size frames */
3827 if ((avg_wire_size > 300) && (avg_wire_size < 1200))
3828 new_val = avg_wire_size / 3;
3829 else
3830 new_val = avg_wire_size / 2;
3831
Alexander Duyck0ba82992011-08-26 07:45:47 +00003832 /* conservative mode (itr 3) eliminates the lowest_latency setting */
3833 if (new_val < IGB_20K_ITR &&
3834 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
3835 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
3836 new_val = IGB_20K_ITR;
Nick Nunleyabe1c362010-02-17 01:03:19 +00003837
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003838set_itr_val:
Alexander Duyck047e0032009-10-27 15:49:27 +00003839 if (new_val != q_vector->itr_val) {
3840 q_vector->itr_val = new_val;
3841 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08003842 }
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003843clear_counts:
Alexander Duyck0ba82992011-08-26 07:45:47 +00003844 q_vector->rx.total_bytes = 0;
3845 q_vector->rx.total_packets = 0;
3846 q_vector->tx.total_bytes = 0;
3847 q_vector->tx.total_packets = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003848}
3849
3850/**
3851 * igb_update_itr - update the dynamic ITR value based on statistics
3852 * Stores a new ITR value based on packets and byte
3853 * counts during the last interrupt. The advantage of per interrupt
3854 * computation is faster updates and more accurate ITR for the current
3855 * traffic pattern. Constants in this function were computed
3856 * based on theoretical maximum wire speed and thresholds were set based
3857 * on testing data as well as attempting to minimize response time
3858 * while increasing bulk throughput.
3859 * this functionality is controlled by the InterruptThrottleRate module
3860 * parameter (see igb_param.c)
3861 * NOTE: These calculations are only valid when operating in a single-
3862 * queue environment.
Alexander Duyck0ba82992011-08-26 07:45:47 +00003863 * @q_vector: pointer to q_vector
3864 * @ring_container: ring info to update the itr for
Auke Kok9d5c8242008-01-24 02:22:38 -08003865 **/
Alexander Duyck0ba82992011-08-26 07:45:47 +00003866static void igb_update_itr(struct igb_q_vector *q_vector,
3867 struct igb_ring_container *ring_container)
Auke Kok9d5c8242008-01-24 02:22:38 -08003868{
Alexander Duyck0ba82992011-08-26 07:45:47 +00003869 unsigned int packets = ring_container->total_packets;
3870 unsigned int bytes = ring_container->total_bytes;
3871 u8 itrval = ring_container->itr;
Auke Kok9d5c8242008-01-24 02:22:38 -08003872
Alexander Duyck0ba82992011-08-26 07:45:47 +00003873 /* no packets, exit with status unchanged */
Auke Kok9d5c8242008-01-24 02:22:38 -08003874 if (packets == 0)
Alexander Duyck0ba82992011-08-26 07:45:47 +00003875 return;
Auke Kok9d5c8242008-01-24 02:22:38 -08003876
Alexander Duyck0ba82992011-08-26 07:45:47 +00003877 switch (itrval) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003878 case lowest_latency:
3879 /* handle TSO and jumbo frames */
3880 if (bytes/packets > 8000)
Alexander Duyck0ba82992011-08-26 07:45:47 +00003881 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003882 else if ((packets < 5) && (bytes > 512))
Alexander Duyck0ba82992011-08-26 07:45:47 +00003883 itrval = low_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003884 break;
3885 case low_latency: /* 50 usec aka 20000 ints/s */
3886 if (bytes > 10000) {
3887 /* this if handles the TSO accounting */
3888 if (bytes/packets > 8000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003889 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003890 } else if ((packets < 10) || ((bytes/packets) > 1200)) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003891 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003892 } else if ((packets > 35)) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003893 itrval = lowest_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003894 }
3895 } else if (bytes/packets > 2000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003896 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003897 } else if (packets <= 2 && bytes < 512) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003898 itrval = lowest_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003899 }
3900 break;
3901 case bulk_latency: /* 250 usec aka 4000 ints/s */
3902 if (bytes > 25000) {
3903 if (packets > 35)
Alexander Duyck0ba82992011-08-26 07:45:47 +00003904 itrval = low_latency;
Alexander Duyck1e5c3d22009-02-12 18:17:21 +00003905 } else if (bytes < 1500) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003906 itrval = low_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003907 }
3908 break;
3909 }
3910
Alexander Duyck0ba82992011-08-26 07:45:47 +00003911 /* clear work counters since we have the values we need */
3912 ring_container->total_bytes = 0;
3913 ring_container->total_packets = 0;
3914
3915 /* write updated itr to ring container */
3916 ring_container->itr = itrval;
Auke Kok9d5c8242008-01-24 02:22:38 -08003917}
3918
Alexander Duyck0ba82992011-08-26 07:45:47 +00003919static void igb_set_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08003920{
Alexander Duyck0ba82992011-08-26 07:45:47 +00003921 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck047e0032009-10-27 15:49:27 +00003922 u32 new_itr = q_vector->itr_val;
Alexander Duyck0ba82992011-08-26 07:45:47 +00003923 u8 current_itr = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003924
3925 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
3926 if (adapter->link_speed != SPEED_1000) {
3927 current_itr = 0;
Alexander Duyck0ba82992011-08-26 07:45:47 +00003928 new_itr = IGB_4K_ITR;
Auke Kok9d5c8242008-01-24 02:22:38 -08003929 goto set_itr_now;
3930 }
3931
Alexander Duyck0ba82992011-08-26 07:45:47 +00003932 igb_update_itr(q_vector, &q_vector->tx);
3933 igb_update_itr(q_vector, &q_vector->rx);
Auke Kok9d5c8242008-01-24 02:22:38 -08003934
Alexander Duyck0ba82992011-08-26 07:45:47 +00003935 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
Auke Kok9d5c8242008-01-24 02:22:38 -08003936
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003937 /* conservative mode (itr 3) eliminates the lowest_latency setting */
Alexander Duyck0ba82992011-08-26 07:45:47 +00003938 if (current_itr == lowest_latency &&
3939 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
3940 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003941 current_itr = low_latency;
3942
Auke Kok9d5c8242008-01-24 02:22:38 -08003943 switch (current_itr) {
3944 /* counts and packets in update_itr are dependent on these numbers */
3945 case lowest_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00003946 new_itr = IGB_70K_ITR; /* 70,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08003947 break;
3948 case low_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00003949 new_itr = IGB_20K_ITR; /* 20,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08003950 break;
3951 case bulk_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00003952 new_itr = IGB_4K_ITR; /* 4,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08003953 break;
3954 default:
3955 break;
3956 }
3957
3958set_itr_now:
Alexander Duyck047e0032009-10-27 15:49:27 +00003959 if (new_itr != q_vector->itr_val) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003960 /* this attempts to bias the interrupt rate towards Bulk
3961 * by adding intermediate steps when interrupt rate is
3962 * increasing */
Alexander Duyck047e0032009-10-27 15:49:27 +00003963 new_itr = new_itr > q_vector->itr_val ?
3964 max((new_itr * q_vector->itr_val) /
3965 (new_itr + (q_vector->itr_val >> 2)),
Alexander Duyck0ba82992011-08-26 07:45:47 +00003966 new_itr) :
Auke Kok9d5c8242008-01-24 02:22:38 -08003967 new_itr;
3968 /* Don't write the value here; it resets the adapter's
3969 * internal timer, and causes us to delay far longer than
3970 * we should between interrupts. Instead, we write the ITR
3971 * value at the beginning of the next interrupt so the timing
3972 * ends up being correct.
3973 */
Alexander Duyck047e0032009-10-27 15:49:27 +00003974 q_vector->itr_val = new_itr;
3975 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08003976 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003977}
3978
Stephen Hemmingerc50b52a2012-01-18 22:13:26 +00003979static void igb_tx_ctxtdesc(struct igb_ring *tx_ring, u32 vlan_macip_lens,
3980 u32 type_tucmd, u32 mss_l4len_idx)
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00003981{
3982 struct e1000_adv_tx_context_desc *context_desc;
3983 u16 i = tx_ring->next_to_use;
3984
3985 context_desc = IGB_TX_CTXTDESC(tx_ring, i);
3986
3987 i++;
3988 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
3989
3990 /* set bits to identify this as an advanced context descriptor */
3991 type_tucmd |= E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
3992
3993 /* For 82575, context index must be unique per ring. */
Alexander Duyck866cff02011-08-26 07:45:36 +00003994 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00003995 mss_l4len_idx |= tx_ring->reg_idx << 4;
3996
3997 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
3998 context_desc->seqnum_seed = 0;
3999 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
4000 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
4001}
4002
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004003static int igb_tso(struct igb_ring *tx_ring,
4004 struct igb_tx_buffer *first,
4005 u8 *hdr_len)
Auke Kok9d5c8242008-01-24 02:22:38 -08004006{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004007 struct sk_buff *skb = first->skb;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004008 u32 vlan_macip_lens, type_tucmd;
4009 u32 mss_l4len_idx, l4len;
4010
Alexander Duycked6aa102012-11-13 04:03:22 +00004011 if (skb->ip_summed != CHECKSUM_PARTIAL)
4012 return 0;
4013
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004014 if (!skb_is_gso(skb))
4015 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004016
4017 if (skb_header_cloned(skb)) {
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004018 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004019 if (err)
4020 return err;
4021 }
4022
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004023 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
4024 type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP;
Auke Kok9d5c8242008-01-24 02:22:38 -08004025
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004026 if (first->protocol == __constant_htons(ETH_P_IP)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004027 struct iphdr *iph = ip_hdr(skb);
4028 iph->tot_len = 0;
4029 iph->check = 0;
4030 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
4031 iph->daddr, 0,
4032 IPPROTO_TCP,
4033 0);
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004034 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004035 first->tx_flags |= IGB_TX_FLAGS_TSO |
4036 IGB_TX_FLAGS_CSUM |
4037 IGB_TX_FLAGS_IPV4;
Sridhar Samudrala8e1e8a42010-01-23 02:02:21 -08004038 } else if (skb_is_gso_v6(skb)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004039 ipv6_hdr(skb)->payload_len = 0;
4040 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4041 &ipv6_hdr(skb)->daddr,
4042 0, IPPROTO_TCP, 0);
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004043 first->tx_flags |= IGB_TX_FLAGS_TSO |
4044 IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08004045 }
4046
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004047 /* compute header lengths */
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004048 l4len = tcp_hdrlen(skb);
4049 *hdr_len = skb_transport_offset(skb) + l4len;
Auke Kok9d5c8242008-01-24 02:22:38 -08004050
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004051 /* update gso size and bytecount with header size */
4052 first->gso_segs = skb_shinfo(skb)->gso_segs;
4053 first->bytecount += (first->gso_segs - 1) * *hdr_len;
4054
Auke Kok9d5c8242008-01-24 02:22:38 -08004055 /* MSS L4LEN IDX */
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004056 mss_l4len_idx = l4len << E1000_ADVTXD_L4LEN_SHIFT;
4057 mss_l4len_idx |= skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT;
Auke Kok9d5c8242008-01-24 02:22:38 -08004058
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004059 /* VLAN MACLEN IPLEN */
4060 vlan_macip_lens = skb_network_header_len(skb);
4061 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004062 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
Auke Kok9d5c8242008-01-24 02:22:38 -08004063
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004064 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004065
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004066 return 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08004067}
4068
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004069static void igb_tx_csum(struct igb_ring *tx_ring, struct igb_tx_buffer *first)
Auke Kok9d5c8242008-01-24 02:22:38 -08004070{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004071 struct sk_buff *skb = first->skb;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004072 u32 vlan_macip_lens = 0;
4073 u32 mss_l4len_idx = 0;
4074 u32 type_tucmd = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004075
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004076 if (skb->ip_summed != CHECKSUM_PARTIAL) {
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004077 if (!(first->tx_flags & IGB_TX_FLAGS_VLAN))
4078 return;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004079 } else {
4080 u8 l4_hdr = 0;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004081 switch (first->protocol) {
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004082 case __constant_htons(ETH_P_IP):
4083 vlan_macip_lens |= skb_network_header_len(skb);
4084 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
4085 l4_hdr = ip_hdr(skb)->protocol;
4086 break;
4087 case __constant_htons(ETH_P_IPV6):
4088 vlan_macip_lens |= skb_network_header_len(skb);
4089 l4_hdr = ipv6_hdr(skb)->nexthdr;
4090 break;
4091 default:
4092 if (unlikely(net_ratelimit())) {
4093 dev_warn(tx_ring->dev,
4094 "partial checksum but proto=%x!\n",
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004095 first->protocol);
Arthur Jonesfa4a7ef2009-03-21 16:55:07 -07004096 }
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004097 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08004098 }
4099
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004100 switch (l4_hdr) {
4101 case IPPROTO_TCP:
4102 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
4103 mss_l4len_idx = tcp_hdrlen(skb) <<
4104 E1000_ADVTXD_L4LEN_SHIFT;
4105 break;
4106 case IPPROTO_SCTP:
4107 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
4108 mss_l4len_idx = sizeof(struct sctphdr) <<
4109 E1000_ADVTXD_L4LEN_SHIFT;
4110 break;
4111 case IPPROTO_UDP:
4112 mss_l4len_idx = sizeof(struct udphdr) <<
4113 E1000_ADVTXD_L4LEN_SHIFT;
4114 break;
4115 default:
4116 if (unlikely(net_ratelimit())) {
4117 dev_warn(tx_ring->dev,
4118 "partial checksum but l4 proto=%x!\n",
4119 l4_hdr);
4120 }
4121 break;
4122 }
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004123
4124 /* update TX checksum flag */
4125 first->tx_flags |= IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08004126 }
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004127
4128 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004129 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004130
4131 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004132}
4133
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004134#define IGB_SET_FLAG(_input, _flag, _result) \
4135 ((_flag <= _result) ? \
4136 ((u32)(_input & _flag) * (_result / _flag)) : \
4137 ((u32)(_input & _flag) / (_flag / _result)))
4138
4139static u32 igb_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
Alexander Duycke032afc2011-08-26 07:44:48 +00004140{
4141 /* set type for advanced descriptor with frame checksum insertion */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004142 u32 cmd_type = E1000_ADVTXD_DTYP_DATA |
4143 E1000_ADVTXD_DCMD_DEXT |
4144 E1000_ADVTXD_DCMD_IFCS;
Alexander Duycke032afc2011-08-26 07:44:48 +00004145
4146 /* set HW vlan bit if vlan is present */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004147 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_VLAN,
4148 (E1000_ADVTXD_DCMD_VLE));
Alexander Duycke032afc2011-08-26 07:44:48 +00004149
4150 /* set segmentation bits for TSO */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004151 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSO,
4152 (E1000_ADVTXD_DCMD_TSE));
4153
4154 /* set timestamp bit if present */
4155 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSTAMP,
4156 (E1000_ADVTXD_MAC_TSTAMP));
4157
4158 /* insert frame checksum */
4159 cmd_type ^= IGB_SET_FLAG(skb->no_fcs, 1, E1000_ADVTXD_DCMD_IFCS);
Alexander Duycke032afc2011-08-26 07:44:48 +00004160
4161 return cmd_type;
4162}
4163
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004164static void igb_tx_olinfo_status(struct igb_ring *tx_ring,
4165 union e1000_adv_tx_desc *tx_desc,
4166 u32 tx_flags, unsigned int paylen)
Alexander Duycke032afc2011-08-26 07:44:48 +00004167{
4168 u32 olinfo_status = paylen << E1000_ADVTXD_PAYLEN_SHIFT;
4169
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004170 /* 82575 requires a unique index per ring */
4171 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
Alexander Duycke032afc2011-08-26 07:44:48 +00004172 olinfo_status |= tx_ring->reg_idx << 4;
4173
4174 /* insert L4 checksum */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004175 olinfo_status |= IGB_SET_FLAG(tx_flags,
4176 IGB_TX_FLAGS_CSUM,
4177 (E1000_TXD_POPTS_TXSM << 8));
Alexander Duycke032afc2011-08-26 07:44:48 +00004178
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004179 /* insert IPv4 checksum */
4180 olinfo_status |= IGB_SET_FLAG(tx_flags,
4181 IGB_TX_FLAGS_IPV4,
4182 (E1000_TXD_POPTS_IXSM << 8));
Alexander Duycke032afc2011-08-26 07:44:48 +00004183
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004184 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
Alexander Duycke032afc2011-08-26 07:44:48 +00004185}
4186
Alexander Duyckebe42d12011-08-26 07:45:09 +00004187/*
4188 * The largest size we can write to the descriptor is 65535. In order to
4189 * maintain a power of two alignment we have to limit ourselves to 32K.
4190 */
4191#define IGB_MAX_TXD_PWR 15
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004192#define IGB_MAX_DATA_PER_TXD (1<<IGB_MAX_TXD_PWR)
Auke Kok9d5c8242008-01-24 02:22:38 -08004193
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004194static void igb_tx_map(struct igb_ring *tx_ring,
4195 struct igb_tx_buffer *first,
Alexander Duyckebe42d12011-08-26 07:45:09 +00004196 const u8 hdr_len)
Auke Kok9d5c8242008-01-24 02:22:38 -08004197{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004198 struct sk_buff *skb = first->skb;
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004199 struct igb_tx_buffer *tx_buffer;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004200 union e1000_adv_tx_desc *tx_desc;
Alexander Duyck80d07592012-11-13 04:03:24 +00004201 struct skb_frag_struct *frag;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004202 dma_addr_t dma;
Alexander Duyck80d07592012-11-13 04:03:24 +00004203 unsigned int data_len, size;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004204 u32 tx_flags = first->tx_flags;
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004205 u32 cmd_type = igb_tx_cmd_type(skb, tx_flags);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004206 u16 i = tx_ring->next_to_use;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004207
4208 tx_desc = IGB_TX_DESC(tx_ring, i);
4209
Alexander Duyck80d07592012-11-13 04:03:24 +00004210 igb_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
4211
4212 size = skb_headlen(skb);
4213 data_len = skb->data_len;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004214
4215 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08004216
Alexander Duyck80d07592012-11-13 04:03:24 +00004217 tx_buffer = first;
Alexander Duyck2bbfebe2011-08-26 07:44:59 +00004218
Alexander Duyck80d07592012-11-13 04:03:24 +00004219 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
4220 if (dma_mapping_error(tx_ring->dev, dma))
4221 goto dma_error;
4222
4223 /* record length, and DMA address */
4224 dma_unmap_len_set(tx_buffer, len, size);
4225 dma_unmap_addr_set(tx_buffer, dma, dma);
4226
4227 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4228
Alexander Duyckebe42d12011-08-26 07:45:09 +00004229 while (unlikely(size > IGB_MAX_DATA_PER_TXD)) {
4230 tx_desc->read.cmd_type_len =
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004231 cpu_to_le32(cmd_type ^ IGB_MAX_DATA_PER_TXD);
Auke Kok9d5c8242008-01-24 02:22:38 -08004232
Alexander Duyckebe42d12011-08-26 07:45:09 +00004233 i++;
4234 tx_desc++;
4235 if (i == tx_ring->count) {
4236 tx_desc = IGB_TX_DESC(tx_ring, 0);
4237 i = 0;
4238 }
Alexander Duyck80d07592012-11-13 04:03:24 +00004239 tx_desc->read.olinfo_status = 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004240
4241 dma += IGB_MAX_DATA_PER_TXD;
4242 size -= IGB_MAX_DATA_PER_TXD;
4243
Alexander Duyckebe42d12011-08-26 07:45:09 +00004244 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4245 }
4246
4247 if (likely(!data_len))
4248 break;
4249
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004250 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004251
Alexander Duyck65689fe2009-03-20 00:17:43 +00004252 i++;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004253 tx_desc++;
4254 if (i == tx_ring->count) {
4255 tx_desc = IGB_TX_DESC(tx_ring, 0);
Alexander Duyck65689fe2009-03-20 00:17:43 +00004256 i = 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004257 }
Alexander Duyck80d07592012-11-13 04:03:24 +00004258 tx_desc->read.olinfo_status = 0;
Alexander Duyck65689fe2009-03-20 00:17:43 +00004259
Eric Dumazet9e903e02011-10-18 21:00:24 +00004260 size = skb_frag_size(frag);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004261 data_len -= size;
4262
4263 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
Alexander Duyck80d07592012-11-13 04:03:24 +00004264 size, DMA_TO_DEVICE);
Alexander Duyck6366ad32009-12-02 16:47:18 +00004265
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004266 tx_buffer = &tx_ring->tx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08004267 }
4268
Alexander Duyckebe42d12011-08-26 07:45:09 +00004269 /* write last descriptor with RS and EOP bits */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004270 cmd_type |= size | IGB_TXD_DCMD;
4271 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
Alexander Duyck8542db02011-08-26 07:44:43 +00004272
Alexander Duyck80d07592012-11-13 04:03:24 +00004273 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
4274
Alexander Duyck8542db02011-08-26 07:44:43 +00004275 /* set the timestamp */
4276 first->time_stamp = jiffies;
4277
Alexander Duyckebe42d12011-08-26 07:45:09 +00004278 /*
4279 * Force memory writes to complete before letting h/w know there
4280 * are new descriptors to fetch. (Only applicable for weak-ordered
4281 * memory model archs, such as IA-64).
4282 *
4283 * We also need this memory barrier to make certain all of the
4284 * status bits have been updated before next_to_watch is written.
4285 */
Auke Kok9d5c8242008-01-24 02:22:38 -08004286 wmb();
4287
Alexander Duyckebe42d12011-08-26 07:45:09 +00004288 /* set next_to_watch value indicating a packet is present */
4289 first->next_to_watch = tx_desc;
4290
4291 i++;
4292 if (i == tx_ring->count)
4293 i = 0;
4294
Auke Kok9d5c8242008-01-24 02:22:38 -08004295 tx_ring->next_to_use = i;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004296
Alexander Duyckfce99e32009-10-27 15:51:27 +00004297 writel(i, tx_ring->tail);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004298
Auke Kok9d5c8242008-01-24 02:22:38 -08004299 /* we need this if more than one processor can write to our tail
4300 * at a time, it syncronizes IO on IA64/Altix systems */
4301 mmiowb();
Alexander Duyckebe42d12011-08-26 07:45:09 +00004302
4303 return;
4304
4305dma_error:
4306 dev_err(tx_ring->dev, "TX DMA map failed\n");
4307
4308 /* clear dma mappings for failed tx_buffer_info map */
4309 for (;;) {
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004310 tx_buffer = &tx_ring->tx_buffer_info[i];
4311 igb_unmap_and_free_tx_resource(tx_ring, tx_buffer);
4312 if (tx_buffer == first)
Alexander Duyckebe42d12011-08-26 07:45:09 +00004313 break;
4314 if (i == 0)
4315 i = tx_ring->count;
4316 i--;
4317 }
4318
4319 tx_ring->next_to_use = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08004320}
4321
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00004322static int __igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004323{
Alexander Duycke694e962009-10-27 15:53:06 +00004324 struct net_device *netdev = tx_ring->netdev;
4325
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004326 netif_stop_subqueue(netdev, tx_ring->queue_index);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004327
Auke Kok9d5c8242008-01-24 02:22:38 -08004328 /* Herbert's original patch had:
4329 * smp_mb__after_netif_stop_queue();
4330 * but since that doesn't exist yet, just open code it. */
4331 smp_mb();
4332
4333 /* We need to check again in a case another CPU has just
4334 * made room available. */
Alexander Duyckc493ea42009-03-20 00:16:50 +00004335 if (igb_desc_unused(tx_ring) < size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004336 return -EBUSY;
4337
4338 /* A reprieve! */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004339 netif_wake_subqueue(netdev, tx_ring->queue_index);
Eric Dumazet12dcd862010-10-15 17:27:10 +00004340
4341 u64_stats_update_begin(&tx_ring->tx_syncp2);
4342 tx_ring->tx_stats.restart_queue2++;
4343 u64_stats_update_end(&tx_ring->tx_syncp2);
4344
Auke Kok9d5c8242008-01-24 02:22:38 -08004345 return 0;
4346}
4347
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00004348static inline int igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004349{
Alexander Duyckc493ea42009-03-20 00:16:50 +00004350 if (igb_desc_unused(tx_ring) >= size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004351 return 0;
Alexander Duycke694e962009-10-27 15:53:06 +00004352 return __igb_maybe_stop_tx(tx_ring, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08004353}
4354
Alexander Duyckcd392f52011-08-26 07:43:59 +00004355netdev_tx_t igb_xmit_frame_ring(struct sk_buff *skb,
4356 struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08004357{
Matthew Vick1f6e8172012-08-18 07:26:33 +00004358 struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
Alexander Duyck8542db02011-08-26 07:44:43 +00004359 struct igb_tx_buffer *first;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004360 int tso;
Nick Nunley91d4ee32010-02-17 01:04:56 +00004361 u32 tx_flags = 0;
Alexander Duyck31f6adb2011-08-26 07:44:53 +00004362 __be16 protocol = vlan_get_protocol(skb);
Nick Nunley91d4ee32010-02-17 01:04:56 +00004363 u8 hdr_len = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004364
Auke Kok9d5c8242008-01-24 02:22:38 -08004365 /* need: 1 descriptor per page,
4366 * + 2 desc gap to keep tail from touching head,
4367 * + 1 desc for skb->data,
4368 * + 1 desc for context descriptor,
4369 * otherwise try next time */
Alexander Duycke694e962009-10-27 15:53:06 +00004370 if (igb_maybe_stop_tx(tx_ring, skb_shinfo(skb)->nr_frags + 4)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004371 /* this is a hard error */
Auke Kok9d5c8242008-01-24 02:22:38 -08004372 return NETDEV_TX_BUSY;
4373 }
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004374
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004375 /* record the location of the first descriptor for this packet */
4376 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
4377 first->skb = skb;
4378 first->bytecount = skb->len;
4379 first->gso_segs = 1;
4380
Matthew Vick1f6e8172012-08-18 07:26:33 +00004381 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4382 !(adapter->ptp_tx_skb))) {
Oliver Hartkopp2244d072010-08-17 08:59:14 +00004383 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004384 tx_flags |= IGB_TX_FLAGS_TSTAMP;
Matthew Vick1f6e8172012-08-18 07:26:33 +00004385
4386 adapter->ptp_tx_skb = skb_get(skb);
4387 if (adapter->hw.mac.type == e1000_82576)
4388 schedule_work(&adapter->ptp_tx_work);
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004389 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004390
Jesse Grosseab6d182010-10-20 13:56:03 +00004391 if (vlan_tx_tag_present(skb)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004392 tx_flags |= IGB_TX_FLAGS_VLAN;
4393 tx_flags |= (vlan_tx_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
4394 }
4395
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004396 /* record initial flags and protocol */
4397 first->tx_flags = tx_flags;
4398 first->protocol = protocol;
Alexander Duyckcdfd01fc2009-10-27 23:50:57 +00004399
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004400 tso = igb_tso(tx_ring, first, &hdr_len);
4401 if (tso < 0)
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004402 goto out_drop;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004403 else if (!tso)
4404 igb_tx_csum(tx_ring, first);
Auke Kok9d5c8242008-01-24 02:22:38 -08004405
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004406 igb_tx_map(tx_ring, first, hdr_len);
Alexander Duyck85ad76b2009-10-27 15:52:46 +00004407
4408 /* Make sure there is space in the ring for the next send. */
Alexander Duycke694e962009-10-27 15:53:06 +00004409 igb_maybe_stop_tx(tx_ring, MAX_SKB_FRAGS + 4);
Alexander Duyck85ad76b2009-10-27 15:52:46 +00004410
Auke Kok9d5c8242008-01-24 02:22:38 -08004411 return NETDEV_TX_OK;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004412
4413out_drop:
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004414 igb_unmap_and_free_tx_resource(tx_ring, first);
4415
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004416 return NETDEV_TX_OK;
Auke Kok9d5c8242008-01-24 02:22:38 -08004417}
4418
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004419static inline struct igb_ring *igb_tx_queue_mapping(struct igb_adapter *adapter,
4420 struct sk_buff *skb)
4421{
4422 unsigned int r_idx = skb->queue_mapping;
4423
4424 if (r_idx >= adapter->num_tx_queues)
4425 r_idx = r_idx % adapter->num_tx_queues;
4426
4427 return adapter->tx_ring[r_idx];
4428}
4429
Alexander Duyckcd392f52011-08-26 07:43:59 +00004430static netdev_tx_t igb_xmit_frame(struct sk_buff *skb,
4431 struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08004432{
4433 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckb1a436c2009-10-27 15:54:43 +00004434
4435 if (test_bit(__IGB_DOWN, &adapter->state)) {
4436 dev_kfree_skb_any(skb);
4437 return NETDEV_TX_OK;
4438 }
4439
4440 if (skb->len <= 0) {
4441 dev_kfree_skb_any(skb);
4442 return NETDEV_TX_OK;
4443 }
4444
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004445 /*
4446 * The minimum packet size with TCTL.PSP set is 17 so pad the skb
4447 * in order to meet this minimum size requirement.
4448 */
Tushar Daveea5ceea2012-09-14 03:43:43 +00004449 if (unlikely(skb->len < 17)) {
4450 if (skb_pad(skb, 17 - skb->len))
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004451 return NETDEV_TX_OK;
4452 skb->len = 17;
Tushar Daveea5ceea2012-09-14 03:43:43 +00004453 skb_set_tail_pointer(skb, 17);
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004454 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004455
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004456 return igb_xmit_frame_ring(skb, igb_tx_queue_mapping(adapter, skb));
Auke Kok9d5c8242008-01-24 02:22:38 -08004457}
4458
4459/**
4460 * igb_tx_timeout - Respond to a Tx Hang
4461 * @netdev: network interface device structure
4462 **/
4463static void igb_tx_timeout(struct net_device *netdev)
4464{
4465 struct igb_adapter *adapter = netdev_priv(netdev);
4466 struct e1000_hw *hw = &adapter->hw;
4467
4468 /* Do the reset outside of interrupt context */
4469 adapter->tx_timeout_count++;
Alexander Duyckf7ba2052009-10-27 23:48:51 +00004470
Alexander Duyck06218a82011-08-26 07:46:55 +00004471 if (hw->mac.type >= e1000_82580)
Alexander Duyck55cac242009-11-19 12:42:21 +00004472 hw->dev_spec._82575.global_device_reset = true;
4473
Auke Kok9d5c8242008-01-24 02:22:38 -08004474 schedule_work(&adapter->reset_task);
Alexander Duyck265de402009-02-06 23:22:52 +00004475 wr32(E1000_EICS,
4476 (adapter->eims_enable_mask & ~adapter->eims_other));
Auke Kok9d5c8242008-01-24 02:22:38 -08004477}
4478
4479static void igb_reset_task(struct work_struct *work)
4480{
4481 struct igb_adapter *adapter;
4482 adapter = container_of(work, struct igb_adapter, reset_task);
4483
Taku Izumic97ec422010-04-27 14:39:30 +00004484 igb_dump(adapter);
4485 netdev_err(adapter->netdev, "Reset adapter\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004486 igb_reinit_locked(adapter);
4487}
4488
4489/**
Eric Dumazet12dcd862010-10-15 17:27:10 +00004490 * igb_get_stats64 - Get System Network Statistics
Auke Kok9d5c8242008-01-24 02:22:38 -08004491 * @netdev: network interface device structure
Eric Dumazet12dcd862010-10-15 17:27:10 +00004492 * @stats: rtnl_link_stats64 pointer
Auke Kok9d5c8242008-01-24 02:22:38 -08004493 *
Auke Kok9d5c8242008-01-24 02:22:38 -08004494 **/
Eric Dumazet12dcd862010-10-15 17:27:10 +00004495static struct rtnl_link_stats64 *igb_get_stats64(struct net_device *netdev,
4496 struct rtnl_link_stats64 *stats)
Auke Kok9d5c8242008-01-24 02:22:38 -08004497{
Eric Dumazet12dcd862010-10-15 17:27:10 +00004498 struct igb_adapter *adapter = netdev_priv(netdev);
4499
4500 spin_lock(&adapter->stats64_lock);
4501 igb_update_stats(adapter, &adapter->stats64);
4502 memcpy(stats, &adapter->stats64, sizeof(*stats));
4503 spin_unlock(&adapter->stats64_lock);
4504
4505 return stats;
Auke Kok9d5c8242008-01-24 02:22:38 -08004506}
4507
4508/**
4509 * igb_change_mtu - Change the Maximum Transfer Unit
4510 * @netdev: network interface device structure
4511 * @new_mtu: new value for maximum frame size
4512 *
4513 * Returns 0 on success, negative on failure
4514 **/
4515static int igb_change_mtu(struct net_device *netdev, int new_mtu)
4516{
4517 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck090b1792009-10-27 23:51:55 +00004518 struct pci_dev *pdev = adapter->pdev;
Alexander Duyck153285f2011-08-26 07:43:32 +00004519 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Auke Kok9d5c8242008-01-24 02:22:38 -08004520
Alexander Duyckc809d222009-10-27 23:52:13 +00004521 if ((new_mtu < 68) || (max_frame > MAX_JUMBO_FRAME_SIZE)) {
Alexander Duyck090b1792009-10-27 23:51:55 +00004522 dev_err(&pdev->dev, "Invalid MTU setting\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004523 return -EINVAL;
4524 }
4525
Alexander Duyck153285f2011-08-26 07:43:32 +00004526#define MAX_STD_JUMBO_FRAME_SIZE 9238
Auke Kok9d5c8242008-01-24 02:22:38 -08004527 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
Alexander Duyck090b1792009-10-27 23:51:55 +00004528 dev_err(&pdev->dev, "MTU > 9216 not supported.\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004529 return -EINVAL;
4530 }
4531
4532 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
4533 msleep(1);
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004534
Auke Kok9d5c8242008-01-24 02:22:38 -08004535 /* igb_down has a dependency on max_frame_size */
4536 adapter->max_frame_size = max_frame;
Alexander Duyck559e9c42009-10-27 23:52:50 +00004537
Alexander Duyck4c844852009-10-27 15:52:07 +00004538 if (netif_running(netdev))
4539 igb_down(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08004540
Alexander Duyck090b1792009-10-27 23:51:55 +00004541 dev_info(&pdev->dev, "changing MTU from %d to %d\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08004542 netdev->mtu, new_mtu);
4543 netdev->mtu = new_mtu;
4544
4545 if (netif_running(netdev))
4546 igb_up(adapter);
4547 else
4548 igb_reset(adapter);
4549
4550 clear_bit(__IGB_RESETTING, &adapter->state);
4551
4552 return 0;
4553}
4554
4555/**
4556 * igb_update_stats - Update the board statistics counters
4557 * @adapter: board private structure
4558 **/
4559
Eric Dumazet12dcd862010-10-15 17:27:10 +00004560void igb_update_stats(struct igb_adapter *adapter,
4561 struct rtnl_link_stats64 *net_stats)
Auke Kok9d5c8242008-01-24 02:22:38 -08004562{
4563 struct e1000_hw *hw = &adapter->hw;
4564 struct pci_dev *pdev = adapter->pdev;
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004565 u32 reg, mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004566 u16 phy_tmp;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004567 int i;
4568 u64 bytes, packets;
Eric Dumazet12dcd862010-10-15 17:27:10 +00004569 unsigned int start;
4570 u64 _bytes, _packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08004571
4572#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
4573
4574 /*
4575 * Prevent stats update while adapter is being reset, or if the pci
4576 * connection is down.
4577 */
4578 if (adapter->link_speed == 0)
4579 return;
4580 if (pci_channel_offline(pdev))
4581 return;
4582
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004583 bytes = 0;
4584 packets = 0;
4585 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyckae1c07a62012-08-08 05:23:22 +00004586 u32 rqdpc = rd32(E1000_RQDPC(i));
Alexander Duyck3025a442010-02-17 01:02:39 +00004587 struct igb_ring *ring = adapter->rx_ring[i];
Eric Dumazet12dcd862010-10-15 17:27:10 +00004588
Alexander Duyckae1c07a62012-08-08 05:23:22 +00004589 if (rqdpc) {
4590 ring->rx_stats.drops += rqdpc;
4591 net_stats->rx_fifo_errors += rqdpc;
4592 }
Eric Dumazet12dcd862010-10-15 17:27:10 +00004593
4594 do {
4595 start = u64_stats_fetch_begin_bh(&ring->rx_syncp);
4596 _bytes = ring->rx_stats.bytes;
4597 _packets = ring->rx_stats.packets;
4598 } while (u64_stats_fetch_retry_bh(&ring->rx_syncp, start));
4599 bytes += _bytes;
4600 packets += _packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004601 }
4602
Alexander Duyck128e45e2009-11-12 18:37:38 +00004603 net_stats->rx_bytes = bytes;
4604 net_stats->rx_packets = packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004605
4606 bytes = 0;
4607 packets = 0;
4608 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00004609 struct igb_ring *ring = adapter->tx_ring[i];
Eric Dumazet12dcd862010-10-15 17:27:10 +00004610 do {
4611 start = u64_stats_fetch_begin_bh(&ring->tx_syncp);
4612 _bytes = ring->tx_stats.bytes;
4613 _packets = ring->tx_stats.packets;
4614 } while (u64_stats_fetch_retry_bh(&ring->tx_syncp, start));
4615 bytes += _bytes;
4616 packets += _packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004617 }
Alexander Duyck128e45e2009-11-12 18:37:38 +00004618 net_stats->tx_bytes = bytes;
4619 net_stats->tx_packets = packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004620
4621 /* read stats registers */
Auke Kok9d5c8242008-01-24 02:22:38 -08004622 adapter->stats.crcerrs += rd32(E1000_CRCERRS);
4623 adapter->stats.gprc += rd32(E1000_GPRC);
4624 adapter->stats.gorc += rd32(E1000_GORCL);
4625 rd32(E1000_GORCH); /* clear GORCL */
4626 adapter->stats.bprc += rd32(E1000_BPRC);
4627 adapter->stats.mprc += rd32(E1000_MPRC);
4628 adapter->stats.roc += rd32(E1000_ROC);
4629
4630 adapter->stats.prc64 += rd32(E1000_PRC64);
4631 adapter->stats.prc127 += rd32(E1000_PRC127);
4632 adapter->stats.prc255 += rd32(E1000_PRC255);
4633 adapter->stats.prc511 += rd32(E1000_PRC511);
4634 adapter->stats.prc1023 += rd32(E1000_PRC1023);
4635 adapter->stats.prc1522 += rd32(E1000_PRC1522);
4636 adapter->stats.symerrs += rd32(E1000_SYMERRS);
4637 adapter->stats.sec += rd32(E1000_SEC);
4638
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004639 mpc = rd32(E1000_MPC);
4640 adapter->stats.mpc += mpc;
4641 net_stats->rx_fifo_errors += mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004642 adapter->stats.scc += rd32(E1000_SCC);
4643 adapter->stats.ecol += rd32(E1000_ECOL);
4644 adapter->stats.mcc += rd32(E1000_MCC);
4645 adapter->stats.latecol += rd32(E1000_LATECOL);
4646 adapter->stats.dc += rd32(E1000_DC);
4647 adapter->stats.rlec += rd32(E1000_RLEC);
4648 adapter->stats.xonrxc += rd32(E1000_XONRXC);
4649 adapter->stats.xontxc += rd32(E1000_XONTXC);
4650 adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
4651 adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
4652 adapter->stats.fcruc += rd32(E1000_FCRUC);
4653 adapter->stats.gptc += rd32(E1000_GPTC);
4654 adapter->stats.gotc += rd32(E1000_GOTCL);
4655 rd32(E1000_GOTCH); /* clear GOTCL */
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004656 adapter->stats.rnbc += rd32(E1000_RNBC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004657 adapter->stats.ruc += rd32(E1000_RUC);
4658 adapter->stats.rfc += rd32(E1000_RFC);
4659 adapter->stats.rjc += rd32(E1000_RJC);
4660 adapter->stats.tor += rd32(E1000_TORH);
4661 adapter->stats.tot += rd32(E1000_TOTH);
4662 adapter->stats.tpr += rd32(E1000_TPR);
4663
4664 adapter->stats.ptc64 += rd32(E1000_PTC64);
4665 adapter->stats.ptc127 += rd32(E1000_PTC127);
4666 adapter->stats.ptc255 += rd32(E1000_PTC255);
4667 adapter->stats.ptc511 += rd32(E1000_PTC511);
4668 adapter->stats.ptc1023 += rd32(E1000_PTC1023);
4669 adapter->stats.ptc1522 += rd32(E1000_PTC1522);
4670
4671 adapter->stats.mptc += rd32(E1000_MPTC);
4672 adapter->stats.bptc += rd32(E1000_BPTC);
4673
Nick Nunley2d0b0f62010-02-17 01:02:59 +00004674 adapter->stats.tpt += rd32(E1000_TPT);
4675 adapter->stats.colc += rd32(E1000_COLC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004676
4677 adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
Nick Nunley43915c7c2010-02-17 01:03:58 +00004678 /* read internal phy specific stats */
4679 reg = rd32(E1000_CTRL_EXT);
4680 if (!(reg & E1000_CTRL_EXT_LINK_MODE_MASK)) {
4681 adapter->stats.rxerrc += rd32(E1000_RXERRC);
Carolyn Wyborny3dbdf962012-09-12 04:36:24 +00004682
4683 /* this stat has invalid values on i210/i211 */
4684 if ((hw->mac.type != e1000_i210) &&
4685 (hw->mac.type != e1000_i211))
4686 adapter->stats.tncrs += rd32(E1000_TNCRS);
Nick Nunley43915c7c2010-02-17 01:03:58 +00004687 }
4688
Auke Kok9d5c8242008-01-24 02:22:38 -08004689 adapter->stats.tsctc += rd32(E1000_TSCTC);
4690 adapter->stats.tsctfc += rd32(E1000_TSCTFC);
4691
4692 adapter->stats.iac += rd32(E1000_IAC);
4693 adapter->stats.icrxoc += rd32(E1000_ICRXOC);
4694 adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
4695 adapter->stats.icrxatc += rd32(E1000_ICRXATC);
4696 adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
4697 adapter->stats.ictxatc += rd32(E1000_ICTXATC);
4698 adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
4699 adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
4700 adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
4701
4702 /* Fill out the OS statistics structure */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004703 net_stats->multicast = adapter->stats.mprc;
4704 net_stats->collisions = adapter->stats.colc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004705
4706 /* Rx Errors */
4707
4708 /* RLEC on some newer hardware can be incorrect so build
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00004709 * our own version based on RUC and ROC */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004710 net_stats->rx_errors = adapter->stats.rxerrc +
Auke Kok9d5c8242008-01-24 02:22:38 -08004711 adapter->stats.crcerrs + adapter->stats.algnerrc +
4712 adapter->stats.ruc + adapter->stats.roc +
4713 adapter->stats.cexterr;
Alexander Duyck128e45e2009-11-12 18:37:38 +00004714 net_stats->rx_length_errors = adapter->stats.ruc +
4715 adapter->stats.roc;
4716 net_stats->rx_crc_errors = adapter->stats.crcerrs;
4717 net_stats->rx_frame_errors = adapter->stats.algnerrc;
4718 net_stats->rx_missed_errors = adapter->stats.mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004719
4720 /* Tx Errors */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004721 net_stats->tx_errors = adapter->stats.ecol +
4722 adapter->stats.latecol;
4723 net_stats->tx_aborted_errors = adapter->stats.ecol;
4724 net_stats->tx_window_errors = adapter->stats.latecol;
4725 net_stats->tx_carrier_errors = adapter->stats.tncrs;
Auke Kok9d5c8242008-01-24 02:22:38 -08004726
4727 /* Tx Dropped needs to be maintained elsewhere */
4728
4729 /* Phy Stats */
4730 if (hw->phy.media_type == e1000_media_type_copper) {
4731 if ((adapter->link_speed == SPEED_1000) &&
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004732 (!igb_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004733 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
4734 adapter->phy_stats.idle_errors += phy_tmp;
4735 }
4736 }
4737
4738 /* Management Stats */
4739 adapter->stats.mgptc += rd32(E1000_MGTPTC);
4740 adapter->stats.mgprc += rd32(E1000_MGTPRC);
4741 adapter->stats.mgpdc += rd32(E1000_MGTPDC);
Carolyn Wyborny0a915b92011-02-26 07:42:37 +00004742
4743 /* OS2BMC Stats */
4744 reg = rd32(E1000_MANC);
4745 if (reg & E1000_MANC_EN_BMC2OS) {
4746 adapter->stats.o2bgptc += rd32(E1000_O2BGPTC);
4747 adapter->stats.o2bspc += rd32(E1000_O2BSPC);
4748 adapter->stats.b2ospc += rd32(E1000_B2OSPC);
4749 adapter->stats.b2ogprc += rd32(E1000_B2OGPRC);
4750 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004751}
4752
Auke Kok9d5c8242008-01-24 02:22:38 -08004753static irqreturn_t igb_msix_other(int irq, void *data)
4754{
Alexander Duyck047e0032009-10-27 15:49:27 +00004755 struct igb_adapter *adapter = data;
Auke Kok9d5c8242008-01-24 02:22:38 -08004756 struct e1000_hw *hw = &adapter->hw;
PJ Waskiewicz844290e2008-06-27 11:00:39 -07004757 u32 icr = rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07004758 /* reading ICR causes bit 31 of EICR to be cleared */
Alexander Duyckdda0e082009-02-06 23:19:08 +00004759
Alexander Duyck7f081d42010-01-07 17:41:00 +00004760 if (icr & E1000_ICR_DRSTA)
4761 schedule_work(&adapter->reset_task);
4762
Alexander Duyck047e0032009-10-27 15:49:27 +00004763 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00004764 /* HW is reporting DMA is out of sync */
4765 adapter->stats.doosync++;
Greg Rose13800462010-11-06 02:08:26 +00004766 /* The DMA Out of Sync is also indication of a spoof event
4767 * in IOV mode. Check the Wrong VM Behavior register to
4768 * see if it is really a spoof event. */
4769 igb_check_wvbr(adapter);
Alexander Duyckdda0e082009-02-06 23:19:08 +00004770 }
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00004771
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004772 /* Check for a mailbox event */
4773 if (icr & E1000_ICR_VMMB)
4774 igb_msg_task(adapter);
4775
4776 if (icr & E1000_ICR_LSC) {
4777 hw->mac.get_link_status = 1;
4778 /* guard against interrupt when we're going down */
4779 if (!test_bit(__IGB_DOWN, &adapter->state))
4780 mod_timer(&adapter->watchdog_timer, jiffies + 1);
4781 }
4782
Matthew Vick1f6e8172012-08-18 07:26:33 +00004783 if (icr & E1000_ICR_TS) {
4784 u32 tsicr = rd32(E1000_TSICR);
4785
4786 if (tsicr & E1000_TSICR_TXTS) {
4787 /* acknowledge the interrupt */
4788 wr32(E1000_TSICR, E1000_TSICR_TXTS);
4789 /* retrieve hardware timestamp */
4790 schedule_work(&adapter->ptp_tx_work);
4791 }
4792 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00004793
PJ Waskiewicz844290e2008-06-27 11:00:39 -07004794 wr32(E1000_EIMS, adapter->eims_other);
Auke Kok9d5c8242008-01-24 02:22:38 -08004795
4796 return IRQ_HANDLED;
4797}
4798
Alexander Duyck047e0032009-10-27 15:49:27 +00004799static void igb_write_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08004800{
Alexander Duyck26b39272010-02-17 01:00:41 +00004801 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck047e0032009-10-27 15:49:27 +00004802 u32 itr_val = q_vector->itr_val & 0x7FFC;
Auke Kok9d5c8242008-01-24 02:22:38 -08004803
Alexander Duyck047e0032009-10-27 15:49:27 +00004804 if (!q_vector->set_itr)
4805 return;
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004806
Alexander Duyck047e0032009-10-27 15:49:27 +00004807 if (!itr_val)
4808 itr_val = 0x4;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004809
Alexander Duyck26b39272010-02-17 01:00:41 +00004810 if (adapter->hw.mac.type == e1000_82575)
4811 itr_val |= itr_val << 16;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004812 else
Alexander Duyck0ba82992011-08-26 07:45:47 +00004813 itr_val |= E1000_EITR_CNT_IGNR;
Alexander Duyck047e0032009-10-27 15:49:27 +00004814
4815 writel(itr_val, q_vector->itr_register);
4816 q_vector->set_itr = 0;
4817}
4818
4819static irqreturn_t igb_msix_ring(int irq, void *data)
4820{
4821 struct igb_q_vector *q_vector = data;
4822
4823 /* Write the ITR value calculated from the previous interrupt. */
4824 igb_write_itr(q_vector);
4825
4826 napi_schedule(&q_vector->napi);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004827
Auke Kok9d5c8242008-01-24 02:22:38 -08004828 return IRQ_HANDLED;
4829}
4830
Jeff Kirsher421e02f2008-10-17 11:08:31 -07004831#ifdef CONFIG_IGB_DCA
Alexander Duyck6a050042012-09-25 00:31:27 +00004832static void igb_update_tx_dca(struct igb_adapter *adapter,
4833 struct igb_ring *tx_ring,
4834 int cpu)
4835{
4836 struct e1000_hw *hw = &adapter->hw;
4837 u32 txctrl = dca3_get_tag(tx_ring->dev, cpu);
4838
4839 if (hw->mac.type != e1000_82575)
4840 txctrl <<= E1000_DCA_TXCTRL_CPUID_SHIFT;
4841
4842 /*
4843 * We can enable relaxed ordering for reads, but not writes when
4844 * DCA is enabled. This is due to a known issue in some chipsets
4845 * which will cause the DCA tag to be cleared.
4846 */
4847 txctrl |= E1000_DCA_TXCTRL_DESC_RRO_EN |
4848 E1000_DCA_TXCTRL_DATA_RRO_EN |
4849 E1000_DCA_TXCTRL_DESC_DCA_EN;
4850
4851 wr32(E1000_DCA_TXCTRL(tx_ring->reg_idx), txctrl);
4852}
4853
4854static void igb_update_rx_dca(struct igb_adapter *adapter,
4855 struct igb_ring *rx_ring,
4856 int cpu)
4857{
4858 struct e1000_hw *hw = &adapter->hw;
4859 u32 rxctrl = dca3_get_tag(&adapter->pdev->dev, cpu);
4860
4861 if (hw->mac.type != e1000_82575)
4862 rxctrl <<= E1000_DCA_RXCTRL_CPUID_SHIFT;
4863
4864 /*
4865 * We can enable relaxed ordering for reads, but not writes when
4866 * DCA is enabled. This is due to a known issue in some chipsets
4867 * which will cause the DCA tag to be cleared.
4868 */
4869 rxctrl |= E1000_DCA_RXCTRL_DESC_RRO_EN |
4870 E1000_DCA_RXCTRL_DESC_DCA_EN;
4871
4872 wr32(E1000_DCA_RXCTRL(rx_ring->reg_idx), rxctrl);
4873}
4874
Alexander Duyck047e0032009-10-27 15:49:27 +00004875static void igb_update_dca(struct igb_q_vector *q_vector)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004876{
Alexander Duyck047e0032009-10-27 15:49:27 +00004877 struct igb_adapter *adapter = q_vector->adapter;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004878 int cpu = get_cpu();
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004879
Alexander Duyck047e0032009-10-27 15:49:27 +00004880 if (q_vector->cpu == cpu)
4881 goto out_no_update;
4882
Alexander Duyck6a050042012-09-25 00:31:27 +00004883 if (q_vector->tx.ring)
4884 igb_update_tx_dca(adapter, q_vector->tx.ring, cpu);
4885
4886 if (q_vector->rx.ring)
4887 igb_update_rx_dca(adapter, q_vector->rx.ring, cpu);
4888
Alexander Duyck047e0032009-10-27 15:49:27 +00004889 q_vector->cpu = cpu;
4890out_no_update:
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004891 put_cpu();
4892}
4893
4894static void igb_setup_dca(struct igb_adapter *adapter)
4895{
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00004896 struct e1000_hw *hw = &adapter->hw;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004897 int i;
4898
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004899 if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004900 return;
4901
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00004902 /* Always use CB2 mode, difference is masked in the CB driver. */
4903 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
4904
Alexander Duyck047e0032009-10-27 15:49:27 +00004905 for (i = 0; i < adapter->num_q_vectors; i++) {
Alexander Duyck26b39272010-02-17 01:00:41 +00004906 adapter->q_vector[i]->cpu = -1;
4907 igb_update_dca(adapter->q_vector[i]);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004908 }
4909}
4910
4911static int __igb_notify_dca(struct device *dev, void *data)
4912{
4913 struct net_device *netdev = dev_get_drvdata(dev);
4914 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck090b1792009-10-27 23:51:55 +00004915 struct pci_dev *pdev = adapter->pdev;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004916 struct e1000_hw *hw = &adapter->hw;
4917 unsigned long event = *(unsigned long *)data;
4918
4919 switch (event) {
4920 case DCA_PROVIDER_ADD:
4921 /* if already enabled, don't do it again */
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004922 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004923 break;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004924 if (dca_add_requester(dev) == 0) {
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08004925 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Alexander Duyck090b1792009-10-27 23:51:55 +00004926 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004927 igb_setup_dca(adapter);
4928 break;
4929 }
4930 /* Fall Through since DCA is disabled. */
4931 case DCA_PROVIDER_REMOVE:
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004932 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004933 /* without this a class_device is left
Alexander Duyck047e0032009-10-27 15:49:27 +00004934 * hanging around in the sysfs model */
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004935 dca_remove_requester(dev);
Alexander Duyck090b1792009-10-27 23:51:55 +00004936 dev_info(&pdev->dev, "DCA disabled\n");
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004937 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08004938 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004939 }
4940 break;
4941 }
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08004942
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004943 return 0;
4944}
4945
4946static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
4947 void *p)
4948{
4949 int ret_val;
4950
4951 ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
4952 __igb_notify_dca);
4953
4954 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
4955}
Jeff Kirsher421e02f2008-10-17 11:08:31 -07004956#endif /* CONFIG_IGB_DCA */
Auke Kok9d5c8242008-01-24 02:22:38 -08004957
Greg Rose0224d662011-10-14 02:57:14 +00004958#ifdef CONFIG_PCI_IOV
4959static int igb_vf_configure(struct igb_adapter *adapter, int vf)
4960{
4961 unsigned char mac_addr[ETH_ALEN];
Greg Rose0224d662011-10-14 02:57:14 +00004962
Joe Perches7efd26d2012-07-12 19:33:06 +00004963 eth_random_addr(mac_addr);
Greg Rose0224d662011-10-14 02:57:14 +00004964 igb_set_vf_mac(adapter, vf, mac_addr);
4965
Stefan Assmannf5571472012-08-18 04:06:11 +00004966 return 0;
Greg Rose0224d662011-10-14 02:57:14 +00004967}
4968
Stefan Assmannf5571472012-08-18 04:06:11 +00004969static bool igb_vfs_are_assigned(struct igb_adapter *adapter)
Greg Rose0224d662011-10-14 02:57:14 +00004970{
Greg Rose0224d662011-10-14 02:57:14 +00004971 struct pci_dev *pdev = adapter->pdev;
Stefan Assmannf5571472012-08-18 04:06:11 +00004972 struct pci_dev *vfdev;
4973 int dev_id;
Greg Rose0224d662011-10-14 02:57:14 +00004974
4975 switch (adapter->hw.mac.type) {
4976 case e1000_82576:
Stefan Assmannf5571472012-08-18 04:06:11 +00004977 dev_id = IGB_82576_VF_DEV_ID;
Greg Rose0224d662011-10-14 02:57:14 +00004978 break;
4979 case e1000_i350:
Stefan Assmannf5571472012-08-18 04:06:11 +00004980 dev_id = IGB_I350_VF_DEV_ID;
Greg Rose0224d662011-10-14 02:57:14 +00004981 break;
4982 default:
Stefan Assmannf5571472012-08-18 04:06:11 +00004983 return false;
Greg Rose0224d662011-10-14 02:57:14 +00004984 }
4985
Stefan Assmannf5571472012-08-18 04:06:11 +00004986 /* loop through all the VFs to see if we own any that are assigned */
4987 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
4988 while (vfdev) {
4989 /* if we don't own it we don't care */
4990 if (vfdev->is_virtfn && vfdev->physfn == pdev) {
4991 /* if it is assigned we cannot release it */
4992 if (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
Greg Rose0224d662011-10-14 02:57:14 +00004993 return true;
4994 }
Stefan Assmannf5571472012-08-18 04:06:11 +00004995
4996 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, vfdev);
Greg Rose0224d662011-10-14 02:57:14 +00004997 }
Stefan Assmannf5571472012-08-18 04:06:11 +00004998
Greg Rose0224d662011-10-14 02:57:14 +00004999 return false;
5000}
5001
5002#endif
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005003static void igb_ping_all_vfs(struct igb_adapter *adapter)
5004{
5005 struct e1000_hw *hw = &adapter->hw;
5006 u32 ping;
5007 int i;
5008
5009 for (i = 0 ; i < adapter->vfs_allocated_count; i++) {
5010 ping = E1000_PF_CONTROL_MSG;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005011 if (adapter->vf_data[i].flags & IGB_VF_FLAG_CTS)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005012 ping |= E1000_VT_MSGTYPE_CTS;
5013 igb_write_mbx(hw, &ping, 1, i);
5014 }
5015}
5016
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005017static int igb_set_vf_promisc(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5018{
5019 struct e1000_hw *hw = &adapter->hw;
5020 u32 vmolr = rd32(E1000_VMOLR(vf));
5021 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5022
Alexander Duyckd85b90042010-09-22 17:56:20 +00005023 vf_data->flags &= ~(IGB_VF_FLAG_UNI_PROMISC |
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005024 IGB_VF_FLAG_MULTI_PROMISC);
5025 vmolr &= ~(E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5026
5027 if (*msgbuf & E1000_VF_SET_PROMISC_MULTICAST) {
5028 vmolr |= E1000_VMOLR_MPME;
Alexander Duyckd85b90042010-09-22 17:56:20 +00005029 vf_data->flags |= IGB_VF_FLAG_MULTI_PROMISC;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005030 *msgbuf &= ~E1000_VF_SET_PROMISC_MULTICAST;
5031 } else {
5032 /*
5033 * if we have hashes and we are clearing a multicast promisc
5034 * flag we need to write the hashes to the MTA as this step
5035 * was previously skipped
5036 */
5037 if (vf_data->num_vf_mc_hashes > 30) {
5038 vmolr |= E1000_VMOLR_MPME;
5039 } else if (vf_data->num_vf_mc_hashes) {
5040 int j;
5041 vmolr |= E1000_VMOLR_ROMPE;
5042 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5043 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5044 }
5045 }
5046
5047 wr32(E1000_VMOLR(vf), vmolr);
5048
5049 /* there are flags left unprocessed, likely not supported */
5050 if (*msgbuf & E1000_VT_MSGINFO_MASK)
5051 return -EINVAL;
5052
5053 return 0;
5054
5055}
5056
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005057static int igb_set_vf_multicasts(struct igb_adapter *adapter,
5058 u32 *msgbuf, u32 vf)
5059{
5060 int n = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5061 u16 *hash_list = (u16 *)&msgbuf[1];
5062 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5063 int i;
5064
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005065 /* salt away the number of multicast addresses assigned
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005066 * to this VF for later use to restore when the PF multi cast
5067 * list changes
5068 */
5069 vf_data->num_vf_mc_hashes = n;
5070
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005071 /* only up to 30 hash values supported */
5072 if (n > 30)
5073 n = 30;
5074
5075 /* store the hashes for later use */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005076 for (i = 0; i < n; i++)
Joe Perchesa419aef2009-08-18 11:18:35 -07005077 vf_data->vf_mc_hashes[i] = hash_list[i];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005078
5079 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005080 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005081
5082 return 0;
5083}
5084
5085static void igb_restore_vf_multicasts(struct igb_adapter *adapter)
5086{
5087 struct e1000_hw *hw = &adapter->hw;
5088 struct vf_data_storage *vf_data;
5089 int i, j;
5090
5091 for (i = 0; i < adapter->vfs_allocated_count; i++) {
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005092 u32 vmolr = rd32(E1000_VMOLR(i));
5093 vmolr &= ~(E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5094
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005095 vf_data = &adapter->vf_data[i];
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005096
5097 if ((vf_data->num_vf_mc_hashes > 30) ||
5098 (vf_data->flags & IGB_VF_FLAG_MULTI_PROMISC)) {
5099 vmolr |= E1000_VMOLR_MPME;
5100 } else if (vf_data->num_vf_mc_hashes) {
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 wr32(E1000_VMOLR(i), vmolr);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005106 }
5107}
5108
5109static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf)
5110{
5111 struct e1000_hw *hw = &adapter->hw;
5112 u32 pool_mask, reg, vid;
5113 int i;
5114
5115 pool_mask = 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5116
5117 /* Find the vlan filter for this id */
5118 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5119 reg = rd32(E1000_VLVF(i));
5120
5121 /* remove the vf from the pool */
5122 reg &= ~pool_mask;
5123
5124 /* if pool is empty then remove entry from vfta */
5125 if (!(reg & E1000_VLVF_POOLSEL_MASK) &&
5126 (reg & E1000_VLVF_VLANID_ENABLE)) {
5127 reg = 0;
5128 vid = reg & E1000_VLVF_VLANID_MASK;
5129 igb_vfta_set(hw, vid, false);
5130 }
5131
5132 wr32(E1000_VLVF(i), reg);
5133 }
Alexander Duyckae641bd2009-09-03 14:49:33 +00005134
5135 adapter->vf_data[vf].vlans_enabled = 0;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005136}
5137
5138static s32 igb_vlvf_set(struct igb_adapter *adapter, u32 vid, bool add, u32 vf)
5139{
5140 struct e1000_hw *hw = &adapter->hw;
5141 u32 reg, i;
5142
Alexander Duyck51466232009-10-27 23:47:35 +00005143 /* The vlvf table only exists on 82576 hardware and newer */
5144 if (hw->mac.type < e1000_82576)
5145 return -1;
5146
5147 /* we only need to do this if VMDq is enabled */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005148 if (!adapter->vfs_allocated_count)
5149 return -1;
5150
5151 /* Find the vlan filter for this id */
5152 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5153 reg = rd32(E1000_VLVF(i));
5154 if ((reg & E1000_VLVF_VLANID_ENABLE) &&
5155 vid == (reg & E1000_VLVF_VLANID_MASK))
5156 break;
5157 }
5158
5159 if (add) {
5160 if (i == E1000_VLVF_ARRAY_SIZE) {
5161 /* Did not find a matching VLAN ID entry that was
5162 * enabled. Search for a free filter entry, i.e.
5163 * one without the enable bit set
5164 */
5165 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5166 reg = rd32(E1000_VLVF(i));
5167 if (!(reg & E1000_VLVF_VLANID_ENABLE))
5168 break;
5169 }
5170 }
5171 if (i < E1000_VLVF_ARRAY_SIZE) {
5172 /* Found an enabled/available entry */
5173 reg |= 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5174
5175 /* if !enabled we need to set this up in vfta */
5176 if (!(reg & E1000_VLVF_VLANID_ENABLE)) {
Alexander Duyck51466232009-10-27 23:47:35 +00005177 /* add VID to filter table */
5178 igb_vfta_set(hw, vid, true);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005179 reg |= E1000_VLVF_VLANID_ENABLE;
5180 }
Alexander Duyckcad6d052009-03-13 20:41:37 +00005181 reg &= ~E1000_VLVF_VLANID_MASK;
5182 reg |= vid;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005183 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00005184
5185 /* do not modify RLPML for PF devices */
5186 if (vf >= adapter->vfs_allocated_count)
5187 return 0;
5188
5189 if (!adapter->vf_data[vf].vlans_enabled) {
5190 u32 size;
5191 reg = rd32(E1000_VMOLR(vf));
5192 size = reg & E1000_VMOLR_RLPML_MASK;
5193 size += 4;
5194 reg &= ~E1000_VMOLR_RLPML_MASK;
5195 reg |= size;
5196 wr32(E1000_VMOLR(vf), reg);
5197 }
Alexander Duyckae641bd2009-09-03 14:49:33 +00005198
Alexander Duyck51466232009-10-27 23:47:35 +00005199 adapter->vf_data[vf].vlans_enabled++;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005200 }
5201 } else {
5202 if (i < E1000_VLVF_ARRAY_SIZE) {
5203 /* remove vf from the pool */
5204 reg &= ~(1 << (E1000_VLVF_POOLSEL_SHIFT + vf));
5205 /* if pool is empty then remove entry from vfta */
5206 if (!(reg & E1000_VLVF_POOLSEL_MASK)) {
5207 reg = 0;
5208 igb_vfta_set(hw, vid, false);
5209 }
5210 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00005211
5212 /* do not modify RLPML for PF devices */
5213 if (vf >= adapter->vfs_allocated_count)
5214 return 0;
5215
5216 adapter->vf_data[vf].vlans_enabled--;
5217 if (!adapter->vf_data[vf].vlans_enabled) {
5218 u32 size;
5219 reg = rd32(E1000_VMOLR(vf));
5220 size = reg & E1000_VMOLR_RLPML_MASK;
5221 size -= 4;
5222 reg &= ~E1000_VMOLR_RLPML_MASK;
5223 reg |= size;
5224 wr32(E1000_VMOLR(vf), reg);
5225 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005226 }
5227 }
Williams, Mitch A8151d292010-02-10 01:44:24 +00005228 return 0;
5229}
5230
5231static void igb_set_vmvir(struct igb_adapter *adapter, u32 vid, u32 vf)
5232{
5233 struct e1000_hw *hw = &adapter->hw;
5234
5235 if (vid)
5236 wr32(E1000_VMVIR(vf), (vid | E1000_VMVIR_VLANA_DEFAULT));
5237 else
5238 wr32(E1000_VMVIR(vf), 0);
5239}
5240
5241static int igb_ndo_set_vf_vlan(struct net_device *netdev,
5242 int vf, u16 vlan, u8 qos)
5243{
5244 int err = 0;
5245 struct igb_adapter *adapter = netdev_priv(netdev);
5246
5247 if ((vf >= adapter->vfs_allocated_count) || (vlan > 4095) || (qos > 7))
5248 return -EINVAL;
5249 if (vlan || qos) {
5250 err = igb_vlvf_set(adapter, vlan, !!vlan, vf);
5251 if (err)
5252 goto out;
5253 igb_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
5254 igb_set_vmolr(adapter, vf, !vlan);
5255 adapter->vf_data[vf].pf_vlan = vlan;
5256 adapter->vf_data[vf].pf_qos = qos;
5257 dev_info(&adapter->pdev->dev,
5258 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
5259 if (test_bit(__IGB_DOWN, &adapter->state)) {
5260 dev_warn(&adapter->pdev->dev,
5261 "The VF VLAN has been set,"
5262 " but the PF device is not up.\n");
5263 dev_warn(&adapter->pdev->dev,
5264 "Bring the PF device up before"
5265 " attempting to use the VF device.\n");
5266 }
5267 } else {
5268 igb_vlvf_set(adapter, adapter->vf_data[vf].pf_vlan,
5269 false, vf);
5270 igb_set_vmvir(adapter, vlan, vf);
5271 igb_set_vmolr(adapter, vf, true);
5272 adapter->vf_data[vf].pf_vlan = 0;
5273 adapter->vf_data[vf].pf_qos = 0;
5274 }
5275out:
5276 return err;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005277}
5278
5279static int igb_set_vf_vlan(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5280{
5281 int add = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5282 int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
5283
5284 return igb_vlvf_set(adapter, vid, add, vf);
5285}
5286
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005287static inline void igb_vf_reset(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005288{
Greg Rose8fa7e0f2010-11-06 05:43:21 +00005289 /* clear flags - except flag that indicates PF has set the MAC */
5290 adapter->vf_data[vf].flags &= IGB_VF_FLAG_PF_SET_MAC;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005291 adapter->vf_data[vf].last_nack = jiffies;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005292
5293 /* reset offloads to defaults */
Williams, Mitch A8151d292010-02-10 01:44:24 +00005294 igb_set_vmolr(adapter, vf, true);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005295
5296 /* reset vlans for device */
5297 igb_clear_vf_vfta(adapter, vf);
Williams, Mitch A8151d292010-02-10 01:44:24 +00005298 if (adapter->vf_data[vf].pf_vlan)
5299 igb_ndo_set_vf_vlan(adapter->netdev, vf,
5300 adapter->vf_data[vf].pf_vlan,
5301 adapter->vf_data[vf].pf_qos);
5302 else
5303 igb_clear_vf_vfta(adapter, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005304
5305 /* reset multicast table array for vf */
5306 adapter->vf_data[vf].num_vf_mc_hashes = 0;
5307
5308 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005309 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005310}
5311
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005312static void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf)
5313{
5314 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
5315
5316 /* generate a new mac address as we were hotplug removed/added */
Williams, Mitch A8151d292010-02-10 01:44:24 +00005317 if (!(adapter->vf_data[vf].flags & IGB_VF_FLAG_PF_SET_MAC))
Joe Perches7efd26d2012-07-12 19:33:06 +00005318 eth_random_addr(vf_mac);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005319
5320 /* process remaining reset events */
5321 igb_vf_reset(adapter, vf);
5322}
5323
5324static void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005325{
5326 struct e1000_hw *hw = &adapter->hw;
5327 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005328 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005329 u32 reg, msgbuf[3];
5330 u8 *addr = (u8 *)(&msgbuf[1]);
5331
5332 /* process all the same items cleared in a function level reset */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005333 igb_vf_reset(adapter, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005334
5335 /* set vf mac address */
Alexander Duyck26ad9172009-10-05 06:32:49 +00005336 igb_rar_set_qsel(adapter, vf_mac, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005337
5338 /* enable transmit and receive for vf */
5339 reg = rd32(E1000_VFTE);
5340 wr32(E1000_VFTE, reg | (1 << vf));
5341 reg = rd32(E1000_VFRE);
5342 wr32(E1000_VFRE, reg | (1 << vf));
5343
Greg Rose8fa7e0f2010-11-06 05:43:21 +00005344 adapter->vf_data[vf].flags |= IGB_VF_FLAG_CTS;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005345
5346 /* reply to reset with ack and vf mac address */
5347 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
5348 memcpy(addr, vf_mac, 6);
5349 igb_write_mbx(hw, msgbuf, 3, vf);
5350}
5351
5352static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf)
5353{
Greg Rosede42edd2010-07-01 13:39:23 +00005354 /*
5355 * The VF MAC Address is stored in a packed array of bytes
5356 * starting at the second 32 bit word of the msg array
5357 */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005358 unsigned char *addr = (char *)&msg[1];
5359 int err = -1;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005360
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005361 if (is_valid_ether_addr(addr))
5362 err = igb_set_vf_mac(adapter, vf, addr);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005363
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005364 return err;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005365}
5366
5367static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf)
5368{
5369 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005370 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005371 u32 msg = E1000_VT_MSGTYPE_NACK;
5372
5373 /* if device isn't clear to send it shouldn't be reading either */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005374 if (!(vf_data->flags & IGB_VF_FLAG_CTS) &&
5375 time_after(jiffies, vf_data->last_nack + (2 * HZ))) {
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005376 igb_write_mbx(hw, &msg, 1, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005377 vf_data->last_nack = jiffies;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005378 }
5379}
5380
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005381static void igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005382{
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005383 struct pci_dev *pdev = adapter->pdev;
5384 u32 msgbuf[E1000_VFMAILBOX_SIZE];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005385 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005386 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005387 s32 retval;
5388
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005389 retval = igb_read_mbx(hw, msgbuf, E1000_VFMAILBOX_SIZE, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005390
Alexander Duyckfef45f42009-12-11 22:57:34 -08005391 if (retval) {
5392 /* if receive failed revoke VF CTS stats and restart init */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005393 dev_err(&pdev->dev, "Error receiving message from VF\n");
Alexander Duyckfef45f42009-12-11 22:57:34 -08005394 vf_data->flags &= ~IGB_VF_FLAG_CTS;
5395 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5396 return;
5397 goto out;
5398 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005399
5400 /* this is a message we already processed, do nothing */
5401 if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005402 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005403
5404 /*
5405 * until the vf completes a reset it should not be
5406 * allowed to start any configuration.
5407 */
5408
5409 if (msgbuf[0] == E1000_VF_RESET) {
5410 igb_vf_reset_msg(adapter, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005411 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005412 }
5413
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005414 if (!(vf_data->flags & IGB_VF_FLAG_CTS)) {
Alexander Duyckfef45f42009-12-11 22:57:34 -08005415 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5416 return;
5417 retval = -1;
5418 goto out;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005419 }
5420
5421 switch ((msgbuf[0] & 0xFFFF)) {
5422 case E1000_VF_SET_MAC_ADDR:
Greg Rosea6b5ea32010-11-06 05:42:59 +00005423 retval = -EINVAL;
5424 if (!(vf_data->flags & IGB_VF_FLAG_PF_SET_MAC))
5425 retval = igb_set_vf_mac_addr(adapter, msgbuf, vf);
5426 else
5427 dev_warn(&pdev->dev,
5428 "VF %d attempted to override administratively "
5429 "set MAC address\nReload the VF driver to "
5430 "resume operations\n", vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005431 break;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005432 case E1000_VF_SET_PROMISC:
5433 retval = igb_set_vf_promisc(adapter, msgbuf, vf);
5434 break;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005435 case E1000_VF_SET_MULTICAST:
5436 retval = igb_set_vf_multicasts(adapter, msgbuf, vf);
5437 break;
5438 case E1000_VF_SET_LPE:
5439 retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf);
5440 break;
5441 case E1000_VF_SET_VLAN:
Greg Rosea6b5ea32010-11-06 05:42:59 +00005442 retval = -1;
5443 if (vf_data->pf_vlan)
5444 dev_warn(&pdev->dev,
5445 "VF %d attempted to override administratively "
5446 "set VLAN tag\nReload the VF driver to "
5447 "resume operations\n", vf);
Williams, Mitch A8151d292010-02-10 01:44:24 +00005448 else
5449 retval = igb_set_vf_vlan(adapter, msgbuf, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005450 break;
5451 default:
Alexander Duyck090b1792009-10-27 23:51:55 +00005452 dev_err(&pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005453 retval = -1;
5454 break;
5455 }
5456
Alexander Duyckfef45f42009-12-11 22:57:34 -08005457 msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
5458out:
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005459 /* notify the VF of the results of what it sent us */
5460 if (retval)
5461 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
5462 else
5463 msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
5464
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005465 igb_write_mbx(hw, msgbuf, 1, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005466}
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005467
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005468static void igb_msg_task(struct igb_adapter *adapter)
5469{
5470 struct e1000_hw *hw = &adapter->hw;
5471 u32 vf;
5472
5473 for (vf = 0; vf < adapter->vfs_allocated_count; vf++) {
5474 /* process any reset requests */
5475 if (!igb_check_for_rst(hw, vf))
5476 igb_vf_reset_event(adapter, vf);
5477
5478 /* process any messages pending */
5479 if (!igb_check_for_msg(hw, vf))
5480 igb_rcv_msg_from_vf(adapter, vf);
5481
5482 /* process any acks */
5483 if (!igb_check_for_ack(hw, vf))
5484 igb_rcv_ack_from_vf(adapter, vf);
5485 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005486}
5487
Auke Kok9d5c8242008-01-24 02:22:38 -08005488/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00005489 * igb_set_uta - Set unicast filter table address
5490 * @adapter: board private structure
5491 *
5492 * The unicast table address is a register array of 32-bit registers.
5493 * The table is meant to be used in a way similar to how the MTA is used
5494 * however due to certain limitations in the hardware it is necessary to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005495 * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscuous
5496 * enable bit to allow vlan tag stripping when promiscuous mode is enabled
Alexander Duyck68d480c2009-10-05 06:33:08 +00005497 **/
5498static void igb_set_uta(struct igb_adapter *adapter)
5499{
5500 struct e1000_hw *hw = &adapter->hw;
5501 int i;
5502
5503 /* The UTA table only exists on 82576 hardware and newer */
5504 if (hw->mac.type < e1000_82576)
5505 return;
5506
5507 /* we only need to do this if VMDq is enabled */
5508 if (!adapter->vfs_allocated_count)
5509 return;
5510
5511 for (i = 0; i < hw->mac.uta_reg_count; i++)
5512 array_wr32(E1000_UTA, i, ~0);
5513}
5514
5515/**
Auke Kok9d5c8242008-01-24 02:22:38 -08005516 * igb_intr_msi - Interrupt Handler
5517 * @irq: interrupt number
5518 * @data: pointer to a network interface device structure
5519 **/
5520static irqreturn_t igb_intr_msi(int irq, void *data)
5521{
Alexander Duyck047e0032009-10-27 15:49:27 +00005522 struct igb_adapter *adapter = data;
5523 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08005524 struct e1000_hw *hw = &adapter->hw;
5525 /* read ICR disables interrupts using IAM */
5526 u32 icr = rd32(E1000_ICR);
5527
Alexander Duyck047e0032009-10-27 15:49:27 +00005528 igb_write_itr(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08005529
Alexander Duyck7f081d42010-01-07 17:41:00 +00005530 if (icr & E1000_ICR_DRSTA)
5531 schedule_work(&adapter->reset_task);
5532
Alexander Duyck047e0032009-10-27 15:49:27 +00005533 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00005534 /* HW is reporting DMA is out of sync */
5535 adapter->stats.doosync++;
5536 }
5537
Auke Kok9d5c8242008-01-24 02:22:38 -08005538 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5539 hw->mac.get_link_status = 1;
5540 if (!test_bit(__IGB_DOWN, &adapter->state))
5541 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5542 }
5543
Matthew Vick1f6e8172012-08-18 07:26:33 +00005544 if (icr & E1000_ICR_TS) {
5545 u32 tsicr = rd32(E1000_TSICR);
5546
5547 if (tsicr & E1000_TSICR_TXTS) {
5548 /* acknowledge the interrupt */
5549 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5550 /* retrieve hardware timestamp */
5551 schedule_work(&adapter->ptp_tx_work);
5552 }
5553 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00005554
Alexander Duyck047e0032009-10-27 15:49:27 +00005555 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08005556
5557 return IRQ_HANDLED;
5558}
5559
5560/**
Alexander Duyck4a3c6432009-02-06 23:20:49 +00005561 * igb_intr - Legacy Interrupt Handler
Auke Kok9d5c8242008-01-24 02:22:38 -08005562 * @irq: interrupt number
5563 * @data: pointer to a network interface device structure
5564 **/
5565static irqreturn_t igb_intr(int irq, void *data)
5566{
Alexander Duyck047e0032009-10-27 15:49:27 +00005567 struct igb_adapter *adapter = data;
5568 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08005569 struct e1000_hw *hw = &adapter->hw;
5570 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
5571 * need for the IMC write */
5572 u32 icr = rd32(E1000_ICR);
Auke Kok9d5c8242008-01-24 02:22:38 -08005573
5574 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
5575 * not set, then the adapter didn't send an interrupt */
5576 if (!(icr & E1000_ICR_INT_ASSERTED))
5577 return IRQ_NONE;
5578
Alexander Duyck0ba82992011-08-26 07:45:47 +00005579 igb_write_itr(q_vector);
5580
Alexander Duyck7f081d42010-01-07 17:41:00 +00005581 if (icr & E1000_ICR_DRSTA)
5582 schedule_work(&adapter->reset_task);
5583
Alexander Duyck047e0032009-10-27 15:49:27 +00005584 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00005585 /* HW is reporting DMA is out of sync */
5586 adapter->stats.doosync++;
5587 }
5588
Auke Kok9d5c8242008-01-24 02:22:38 -08005589 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5590 hw->mac.get_link_status = 1;
5591 /* guard against interrupt when we're going down */
5592 if (!test_bit(__IGB_DOWN, &adapter->state))
5593 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5594 }
5595
Matthew Vick1f6e8172012-08-18 07:26:33 +00005596 if (icr & E1000_ICR_TS) {
5597 u32 tsicr = rd32(E1000_TSICR);
5598
5599 if (tsicr & E1000_TSICR_TXTS) {
5600 /* acknowledge the interrupt */
5601 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5602 /* retrieve hardware timestamp */
5603 schedule_work(&adapter->ptp_tx_work);
5604 }
5605 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00005606
Alexander Duyck047e0032009-10-27 15:49:27 +00005607 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08005608
5609 return IRQ_HANDLED;
5610}
5611
Stephen Hemmingerc50b52a2012-01-18 22:13:26 +00005612static void igb_ring_irq_enable(struct igb_q_vector *q_vector)
Alexander Duyck46544252009-02-19 20:39:04 -08005613{
Alexander Duyck047e0032009-10-27 15:49:27 +00005614 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck46544252009-02-19 20:39:04 -08005615 struct e1000_hw *hw = &adapter->hw;
5616
Alexander Duyck0ba82992011-08-26 07:45:47 +00005617 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
5618 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
5619 if ((adapter->num_q_vectors == 1) && !adapter->vf_data)
5620 igb_set_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005621 else
Alexander Duyck047e0032009-10-27 15:49:27 +00005622 igb_update_ring_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005623 }
5624
5625 if (!test_bit(__IGB_DOWN, &adapter->state)) {
5626 if (adapter->msix_entries)
Alexander Duyck047e0032009-10-27 15:49:27 +00005627 wr32(E1000_EIMS, q_vector->eims_value);
Alexander Duyck46544252009-02-19 20:39:04 -08005628 else
5629 igb_irq_enable(adapter);
5630 }
5631}
5632
Auke Kok9d5c8242008-01-24 02:22:38 -08005633/**
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005634 * igb_poll - NAPI Rx polling callback
5635 * @napi: napi polling structure
5636 * @budget: count of how many packets we should handle
Auke Kok9d5c8242008-01-24 02:22:38 -08005637 **/
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005638static int igb_poll(struct napi_struct *napi, int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08005639{
Alexander Duyck047e0032009-10-27 15:49:27 +00005640 struct igb_q_vector *q_vector = container_of(napi,
5641 struct igb_q_vector,
5642 napi);
Alexander Duyck16eb8812011-08-26 07:43:54 +00005643 bool clean_complete = true;
Auke Kok9d5c8242008-01-24 02:22:38 -08005644
Jeff Kirsher421e02f2008-10-17 11:08:31 -07005645#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +00005646 if (q_vector->adapter->flags & IGB_FLAG_DCA_ENABLED)
5647 igb_update_dca(q_vector);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005648#endif
Alexander Duyck0ba82992011-08-26 07:45:47 +00005649 if (q_vector->tx.ring)
Alexander Duyck13fde972011-10-05 13:35:24 +00005650 clean_complete = igb_clean_tx_irq(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08005651
Alexander Duyck0ba82992011-08-26 07:45:47 +00005652 if (q_vector->rx.ring)
Alexander Duyckcd392f52011-08-26 07:43:59 +00005653 clean_complete &= igb_clean_rx_irq(q_vector, budget);
Alexander Duyck047e0032009-10-27 15:49:27 +00005654
Alexander Duyck16eb8812011-08-26 07:43:54 +00005655 /* If all work not completed, return budget and keep polling */
5656 if (!clean_complete)
5657 return budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08005658
Alexander Duyck46544252009-02-19 20:39:04 -08005659 /* If not enough Rx work done, exit the polling mode */
Alexander Duyck16eb8812011-08-26 07:43:54 +00005660 napi_complete(napi);
5661 igb_ring_irq_enable(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005662
Alexander Duyck16eb8812011-08-26 07:43:54 +00005663 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08005664}
Al Viro6d8126f2008-03-16 22:23:24 +00005665
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005666/**
Auke Kok9d5c8242008-01-24 02:22:38 -08005667 * igb_clean_tx_irq - Reclaim resources after transmit completes
Alexander Duyck047e0032009-10-27 15:49:27 +00005668 * @q_vector: pointer to q_vector containing needed info
Ben Hutchings49ce9c22012-07-10 10:56:00 +00005669 *
Auke Kok9d5c8242008-01-24 02:22:38 -08005670 * returns true if ring is completely cleaned
5671 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00005672static bool igb_clean_tx_irq(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08005673{
Alexander Duyck047e0032009-10-27 15:49:27 +00005674 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck0ba82992011-08-26 07:45:47 +00005675 struct igb_ring *tx_ring = q_vector->tx.ring;
Alexander Duyck06034642011-08-26 07:44:22 +00005676 struct igb_tx_buffer *tx_buffer;
Alexander Duyckf4128782012-09-13 06:28:01 +00005677 union e1000_adv_tx_desc *tx_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08005678 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck0ba82992011-08-26 07:45:47 +00005679 unsigned int budget = q_vector->tx.work_limit;
Alexander Duyck8542db02011-08-26 07:44:43 +00005680 unsigned int i = tx_ring->next_to_clean;
Auke Kok9d5c8242008-01-24 02:22:38 -08005681
Alexander Duyck13fde972011-10-05 13:35:24 +00005682 if (test_bit(__IGB_DOWN, &adapter->state))
5683 return true;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08005684
Alexander Duyck06034642011-08-26 07:44:22 +00005685 tx_buffer = &tx_ring->tx_buffer_info[i];
Alexander Duyck13fde972011-10-05 13:35:24 +00005686 tx_desc = IGB_TX_DESC(tx_ring, i);
Alexander Duyck8542db02011-08-26 07:44:43 +00005687 i -= tx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08005688
Alexander Duyckf4128782012-09-13 06:28:01 +00005689 do {
5690 union e1000_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
Alexander Duyck8542db02011-08-26 07:44:43 +00005691
5692 /* if next_to_watch is not set then there is no work pending */
5693 if (!eop_desc)
5694 break;
Alexander Duyck13fde972011-10-05 13:35:24 +00005695
Alexander Duyckf4128782012-09-13 06:28:01 +00005696 /* prevent any other reads prior to eop_desc */
5697 rmb();
5698
Alexander Duyck13fde972011-10-05 13:35:24 +00005699 /* if DD is not set pending work has not been completed */
5700 if (!(eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)))
5701 break;
5702
Alexander Duyck8542db02011-08-26 07:44:43 +00005703 /* clear next_to_watch to prevent false hangs */
5704 tx_buffer->next_to_watch = NULL;
Alexander Duyck13fde972011-10-05 13:35:24 +00005705
Alexander Duyckebe42d12011-08-26 07:45:09 +00005706 /* update the statistics for this packet */
5707 total_bytes += tx_buffer->bytecount;
5708 total_packets += tx_buffer->gso_segs;
Alexander Duyck13fde972011-10-05 13:35:24 +00005709
Alexander Duyckebe42d12011-08-26 07:45:09 +00005710 /* free the skb */
5711 dev_kfree_skb_any(tx_buffer->skb);
Alexander Duyckebe42d12011-08-26 07:45:09 +00005712
5713 /* unmap skb header data */
5714 dma_unmap_single(tx_ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005715 dma_unmap_addr(tx_buffer, dma),
5716 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00005717 DMA_TO_DEVICE);
5718
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005719 /* clear tx_buffer data */
5720 tx_buffer->skb = NULL;
5721 dma_unmap_len_set(tx_buffer, len, 0);
5722
Alexander Duyckebe42d12011-08-26 07:45:09 +00005723 /* clear last DMA location and unmap remaining buffers */
5724 while (tx_desc != eop_desc) {
Alexander Duyck13fde972011-10-05 13:35:24 +00005725 tx_buffer++;
5726 tx_desc++;
Auke Kok9d5c8242008-01-24 02:22:38 -08005727 i++;
Alexander Duyck8542db02011-08-26 07:44:43 +00005728 if (unlikely(!i)) {
5729 i -= tx_ring->count;
Alexander Duyck06034642011-08-26 07:44:22 +00005730 tx_buffer = tx_ring->tx_buffer_info;
Alexander Duyck13fde972011-10-05 13:35:24 +00005731 tx_desc = IGB_TX_DESC(tx_ring, 0);
5732 }
Alexander Duyckebe42d12011-08-26 07:45:09 +00005733
5734 /* unmap any remaining paged data */
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005735 if (dma_unmap_len(tx_buffer, len)) {
Alexander Duyckebe42d12011-08-26 07:45:09 +00005736 dma_unmap_page(tx_ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005737 dma_unmap_addr(tx_buffer, dma),
5738 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00005739 DMA_TO_DEVICE);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005740 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duyckebe42d12011-08-26 07:45:09 +00005741 }
5742 }
5743
Alexander Duyckebe42d12011-08-26 07:45:09 +00005744 /* move us one more past the eop_desc for start of next pkt */
5745 tx_buffer++;
5746 tx_desc++;
5747 i++;
5748 if (unlikely(!i)) {
5749 i -= tx_ring->count;
5750 tx_buffer = tx_ring->tx_buffer_info;
5751 tx_desc = IGB_TX_DESC(tx_ring, 0);
5752 }
Alexander Duyckf4128782012-09-13 06:28:01 +00005753
5754 /* issue prefetch for next Tx descriptor */
5755 prefetch(tx_desc);
5756
5757 /* update budget accounting */
5758 budget--;
5759 } while (likely(budget));
Alexander Duyck0e014cb2008-12-26 01:33:18 -08005760
Eric Dumazetbdbc0632012-01-04 20:23:36 +00005761 netdev_tx_completed_queue(txring_txq(tx_ring),
5762 total_packets, total_bytes);
Alexander Duyck8542db02011-08-26 07:44:43 +00005763 i += tx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08005764 tx_ring->next_to_clean = i;
Alexander Duyck13fde972011-10-05 13:35:24 +00005765 u64_stats_update_begin(&tx_ring->tx_syncp);
5766 tx_ring->tx_stats.bytes += total_bytes;
5767 tx_ring->tx_stats.packets += total_packets;
5768 u64_stats_update_end(&tx_ring->tx_syncp);
Alexander Duyck0ba82992011-08-26 07:45:47 +00005769 q_vector->tx.total_bytes += total_bytes;
5770 q_vector->tx.total_packets += total_packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08005771
Alexander Duyck6d095fa2011-08-26 07:46:19 +00005772 if (test_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
Alexander Duyck13fde972011-10-05 13:35:24 +00005773 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck13fde972011-10-05 13:35:24 +00005774
Auke Kok9d5c8242008-01-24 02:22:38 -08005775 /* Detect a transmit hang in hardware, this serializes the
5776 * check with the clearing of time_stamp and movement of i */
Alexander Duyck6d095fa2011-08-26 07:46:19 +00005777 clear_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
Alexander Duyckf4128782012-09-13 06:28:01 +00005778 if (tx_buffer->next_to_watch &&
Alexander Duyck8542db02011-08-26 07:44:43 +00005779 time_after(jiffies, tx_buffer->time_stamp +
Joe Perches8e95a202009-12-03 07:58:21 +00005780 (adapter->tx_timeout_factor * HZ)) &&
5781 !(rd32(E1000_STATUS) & E1000_STATUS_TXOFF)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08005782
Auke Kok9d5c8242008-01-24 02:22:38 -08005783 /* detected Tx unit hang */
Alexander Duyck59d71982010-04-27 13:09:25 +00005784 dev_err(tx_ring->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08005785 "Detected Tx Unit Hang\n"
Alexander Duyck2d064c02008-07-08 15:10:12 -07005786 " Tx Queue <%d>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08005787 " TDH <%x>\n"
5788 " TDT <%x>\n"
5789 " next_to_use <%x>\n"
5790 " next_to_clean <%x>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08005791 "buffer_info[next_to_clean]\n"
5792 " time_stamp <%lx>\n"
Alexander Duyck8542db02011-08-26 07:44:43 +00005793 " next_to_watch <%p>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08005794 " jiffies <%lx>\n"
5795 " desc.status <%x>\n",
Alexander Duyck2d064c02008-07-08 15:10:12 -07005796 tx_ring->queue_index,
Alexander Duyck238ac812011-08-26 07:43:48 +00005797 rd32(E1000_TDH(tx_ring->reg_idx)),
Alexander Duyckfce99e32009-10-27 15:51:27 +00005798 readl(tx_ring->tail),
Auke Kok9d5c8242008-01-24 02:22:38 -08005799 tx_ring->next_to_use,
5800 tx_ring->next_to_clean,
Alexander Duyck8542db02011-08-26 07:44:43 +00005801 tx_buffer->time_stamp,
Alexander Duyckf4128782012-09-13 06:28:01 +00005802 tx_buffer->next_to_watch,
Auke Kok9d5c8242008-01-24 02:22:38 -08005803 jiffies,
Alexander Duyckf4128782012-09-13 06:28:01 +00005804 tx_buffer->next_to_watch->wb.status);
Alexander Duyck13fde972011-10-05 13:35:24 +00005805 netif_stop_subqueue(tx_ring->netdev,
5806 tx_ring->queue_index);
5807
5808 /* we are about to reset, no point in enabling stuff */
5809 return true;
Auke Kok9d5c8242008-01-24 02:22:38 -08005810 }
5811 }
Alexander Duyck13fde972011-10-05 13:35:24 +00005812
5813 if (unlikely(total_packets &&
5814 netif_carrier_ok(tx_ring->netdev) &&
5815 igb_desc_unused(tx_ring) >= IGB_TX_QUEUE_WAKE)) {
5816 /* Make sure that anybody stopping the queue after this
5817 * sees the new next_to_clean.
5818 */
5819 smp_mb();
5820 if (__netif_subqueue_stopped(tx_ring->netdev,
5821 tx_ring->queue_index) &&
5822 !(test_bit(__IGB_DOWN, &adapter->state))) {
5823 netif_wake_subqueue(tx_ring->netdev,
5824 tx_ring->queue_index);
5825
5826 u64_stats_update_begin(&tx_ring->tx_syncp);
5827 tx_ring->tx_stats.restart_queue++;
5828 u64_stats_update_end(&tx_ring->tx_syncp);
5829 }
5830 }
5831
5832 return !!budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08005833}
5834
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005835/**
5836 * igb_reuse_rx_page - page flip buffer and store it back on the ring
5837 * @rx_ring: rx descriptor ring to store buffers on
5838 * @old_buff: donor buffer to have page reused
5839 *
5840 * Synchronizes page for reuse by the adapter
5841 **/
5842static void igb_reuse_rx_page(struct igb_ring *rx_ring,
5843 struct igb_rx_buffer *old_buff)
5844{
5845 struct igb_rx_buffer *new_buff;
5846 u16 nta = rx_ring->next_to_alloc;
5847
5848 new_buff = &rx_ring->rx_buffer_info[nta];
5849
5850 /* update, and store next to alloc */
5851 nta++;
5852 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
5853
5854 /* transfer page from old buffer to new buffer */
5855 memcpy(new_buff, old_buff, sizeof(struct igb_rx_buffer));
5856
5857 /* sync the buffer for use by the device */
5858 dma_sync_single_range_for_device(rx_ring->dev, old_buff->dma,
5859 old_buff->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005860 IGB_RX_BUFSZ,
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005861 DMA_FROM_DEVICE);
5862}
5863
5864/**
5865 * igb_add_rx_frag - Add contents of Rx buffer to sk_buff
5866 * @rx_ring: rx descriptor ring to transact packets on
5867 * @rx_buffer: buffer containing page to add
5868 * @rx_desc: descriptor containing length of buffer written by hardware
5869 * @skb: sk_buff to place the data into
5870 *
5871 * This function will add the data contained in rx_buffer->page to the skb.
5872 * This is done either through a direct copy if the data in the buffer is
5873 * less than the skb header size, otherwise it will just attach the page as
5874 * a frag to the skb.
5875 *
5876 * The function will then update the page offset if necessary and return
5877 * true if the buffer can be reused by the adapter.
5878 **/
5879static bool igb_add_rx_frag(struct igb_ring *rx_ring,
5880 struct igb_rx_buffer *rx_buffer,
5881 union e1000_adv_rx_desc *rx_desc,
5882 struct sk_buff *skb)
5883{
5884 struct page *page = rx_buffer->page;
5885 unsigned int size = le16_to_cpu(rx_desc->wb.upper.length);
5886
5887 if ((size <= IGB_RX_HDR_LEN) && !skb_is_nonlinear(skb)) {
5888 unsigned char *va = page_address(page) + rx_buffer->page_offset;
5889
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005890 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
5891 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
5892 va += IGB_TS_HDR_LEN;
5893 size -= IGB_TS_HDR_LEN;
5894 }
5895
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005896 memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
5897
5898 /* we can reuse buffer as-is, just make sure it is local */
5899 if (likely(page_to_nid(page) == numa_node_id()))
5900 return true;
5901
5902 /* this page cannot be reused so discard it */
5903 put_page(page);
5904 return false;
5905 }
5906
5907 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005908 rx_buffer->page_offset, size, IGB_RX_BUFSZ);
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005909
5910 /* avoid re-using remote pages */
5911 if (unlikely(page_to_nid(page) != numa_node_id()))
5912 return false;
5913
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005914#if (PAGE_SIZE < 8192)
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005915 /* if we are only owner of page we can reuse it */
5916 if (unlikely(page_count(page) != 1))
5917 return false;
5918
5919 /* flip page offset to other buffer */
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005920 rx_buffer->page_offset ^= IGB_RX_BUFSZ;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005921
5922 /*
5923 * since we are the only owner of the page and we need to
5924 * increment it, just set the value to 2 in order to avoid
5925 * an unnecessary locked operation
5926 */
5927 atomic_set(&page->_count, 2);
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005928#else
5929 /* move offset up to the next cache line */
5930 rx_buffer->page_offset += SKB_DATA_ALIGN(size);
5931
5932 if (rx_buffer->page_offset > (PAGE_SIZE - IGB_RX_BUFSZ))
5933 return false;
5934
5935 /* bump ref count on page before it is given to the stack */
5936 get_page(page);
5937#endif
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005938
5939 return true;
5940}
5941
Alexander Duyck2e334ee2012-09-25 00:31:07 +00005942static struct sk_buff *igb_fetch_rx_buffer(struct igb_ring *rx_ring,
5943 union e1000_adv_rx_desc *rx_desc,
5944 struct sk_buff *skb)
5945{
5946 struct igb_rx_buffer *rx_buffer;
5947 struct page *page;
5948
5949 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
5950
5951 /*
5952 * This memory barrier is needed to keep us from reading
5953 * any other fields out of the rx_desc until we know the
5954 * RXD_STAT_DD bit is set
5955 */
5956 rmb();
5957
5958 page = rx_buffer->page;
5959 prefetchw(page);
5960
5961 if (likely(!skb)) {
5962 void *page_addr = page_address(page) +
5963 rx_buffer->page_offset;
5964
5965 /* prefetch first cache line of first page */
5966 prefetch(page_addr);
5967#if L1_CACHE_BYTES < 128
5968 prefetch(page_addr + L1_CACHE_BYTES);
5969#endif
5970
5971 /* allocate a skb to store the frags */
5972 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
5973 IGB_RX_HDR_LEN);
5974 if (unlikely(!skb)) {
5975 rx_ring->rx_stats.alloc_failed++;
5976 return NULL;
5977 }
5978
5979 /*
5980 * we will be copying header into skb->data in
5981 * pskb_may_pull so it is in our interest to prefetch
5982 * it now to avoid a possible cache miss
5983 */
5984 prefetchw(skb->data);
5985 }
5986
5987 /* we are reusing so sync this buffer for CPU use */
5988 dma_sync_single_range_for_cpu(rx_ring->dev,
5989 rx_buffer->dma,
5990 rx_buffer->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005991 IGB_RX_BUFSZ,
Alexander Duyck2e334ee2012-09-25 00:31:07 +00005992 DMA_FROM_DEVICE);
5993
5994 /* pull page into skb */
5995 if (igb_add_rx_frag(rx_ring, rx_buffer, rx_desc, skb)) {
5996 /* hand second half of page back to the ring */
5997 igb_reuse_rx_page(rx_ring, rx_buffer);
5998 } else {
5999 /* we are not reusing the buffer so unmap it */
6000 dma_unmap_page(rx_ring->dev, rx_buffer->dma,
6001 PAGE_SIZE, DMA_FROM_DEVICE);
6002 }
6003
6004 /* clear contents of rx_buffer */
6005 rx_buffer->page = NULL;
6006
6007 return skb;
6008}
6009
Alexander Duyckcd392f52011-08-26 07:43:59 +00006010static inline void igb_rx_checksum(struct igb_ring *ring,
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006011 union e1000_adv_rx_desc *rx_desc,
6012 struct sk_buff *skb)
Auke Kok9d5c8242008-01-24 02:22:38 -08006013{
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006014 skb_checksum_none_assert(skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08006015
Alexander Duyck294e7d72011-08-26 07:45:57 +00006016 /* Ignore Checksum bit is set */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006017 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_IXSM))
Alexander Duyck294e7d72011-08-26 07:45:57 +00006018 return;
6019
6020 /* Rx checksum disabled via ethtool */
6021 if (!(ring->netdev->features & NETIF_F_RXCSUM))
Auke Kok9d5c8242008-01-24 02:22:38 -08006022 return;
Alexander Duyck85ad76b2009-10-27 15:52:46 +00006023
Auke Kok9d5c8242008-01-24 02:22:38 -08006024 /* TCP/UDP checksum error bit is set */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006025 if (igb_test_staterr(rx_desc,
6026 E1000_RXDEXT_STATERR_TCPE |
6027 E1000_RXDEXT_STATERR_IPE)) {
Jesse Brandeburgb9473562009-04-27 22:36:13 +00006028 /*
6029 * work around errata with sctp packets where the TCPE aka
6030 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
6031 * packets, (aka let the stack check the crc32c)
6032 */
Alexander Duyck866cff02011-08-26 07:45:36 +00006033 if (!((skb->len == 60) &&
6034 test_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
Eric Dumazet12dcd862010-10-15 17:27:10 +00006035 u64_stats_update_begin(&ring->rx_syncp);
Alexander Duyck04a5fcaa2009-10-27 15:52:27 +00006036 ring->rx_stats.csum_err++;
Eric Dumazet12dcd862010-10-15 17:27:10 +00006037 u64_stats_update_end(&ring->rx_syncp);
6038 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006039 /* let the stack verify checksum errors */
Auke Kok9d5c8242008-01-24 02:22:38 -08006040 return;
6041 }
6042 /* It must be a TCP or UDP packet with a valid checksum */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006043 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_TCPCS |
6044 E1000_RXD_STAT_UDPCS))
Auke Kok9d5c8242008-01-24 02:22:38 -08006045 skb->ip_summed = CHECKSUM_UNNECESSARY;
6046
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006047 dev_dbg(ring->dev, "cksum success: bits %08X\n",
6048 le32_to_cpu(rx_desc->wb.upper.status_error));
Auke Kok9d5c8242008-01-24 02:22:38 -08006049}
6050
Alexander Duyck077887c2011-08-26 07:46:29 +00006051static inline void igb_rx_hash(struct igb_ring *ring,
6052 union e1000_adv_rx_desc *rx_desc,
6053 struct sk_buff *skb)
6054{
6055 if (ring->netdev->features & NETIF_F_RXHASH)
6056 skb->rxhash = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
6057}
6058
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006059/**
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006060 * igb_is_non_eop - process handling of non-EOP buffers
6061 * @rx_ring: Rx ring being processed
6062 * @rx_desc: Rx descriptor for current buffer
6063 * @skb: current socket buffer containing buffer in progress
6064 *
6065 * This function updates next to clean. If the buffer is an EOP buffer
6066 * this function exits returning false, otherwise it will place the
6067 * sk_buff in the next buffer to be chained and return true indicating
6068 * that this is in fact a non-EOP buffer.
6069 **/
6070static bool igb_is_non_eop(struct igb_ring *rx_ring,
6071 union e1000_adv_rx_desc *rx_desc)
6072{
6073 u32 ntc = rx_ring->next_to_clean + 1;
6074
6075 /* fetch, update, and store next to clean */
6076 ntc = (ntc < rx_ring->count) ? ntc : 0;
6077 rx_ring->next_to_clean = ntc;
6078
6079 prefetch(IGB_RX_DESC(rx_ring, ntc));
6080
6081 if (likely(igb_test_staterr(rx_desc, E1000_RXD_STAT_EOP)))
6082 return false;
6083
6084 return true;
6085}
6086
6087/**
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006088 * igb_get_headlen - determine size of header for LRO/GRO
6089 * @data: pointer to the start of the headers
6090 * @max_len: total length of section to find headers in
6091 *
6092 * This function is meant to determine the length of headers that will
6093 * be recognized by hardware for LRO, and GRO offloads. The main
6094 * motivation of doing this is to only perform one pull for IPv4 TCP
6095 * packets so that we can do basic things like calculating the gso_size
6096 * based on the average data per packet.
6097 **/
6098static unsigned int igb_get_headlen(unsigned char *data,
6099 unsigned int max_len)
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006100{
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006101 union {
6102 unsigned char *network;
6103 /* l2 headers */
6104 struct ethhdr *eth;
6105 struct vlan_hdr *vlan;
6106 /* l3 headers */
6107 struct iphdr *ipv4;
6108 struct ipv6hdr *ipv6;
6109 } hdr;
6110 __be16 protocol;
6111 u8 nexthdr = 0; /* default to not TCP */
6112 u8 hlen;
6113
6114 /* this should never happen, but better safe than sorry */
6115 if (max_len < ETH_HLEN)
6116 return max_len;
6117
6118 /* initialize network frame pointer */
6119 hdr.network = data;
6120
6121 /* set first protocol and move network header forward */
6122 protocol = hdr.eth->h_proto;
6123 hdr.network += ETH_HLEN;
6124
6125 /* handle any vlan tag if present */
6126 if (protocol == __constant_htons(ETH_P_8021Q)) {
6127 if ((hdr.network - data) > (max_len - VLAN_HLEN))
6128 return max_len;
6129
6130 protocol = hdr.vlan->h_vlan_encapsulated_proto;
6131 hdr.network += VLAN_HLEN;
6132 }
6133
6134 /* handle L3 protocols */
6135 if (protocol == __constant_htons(ETH_P_IP)) {
6136 if ((hdr.network - data) > (max_len - sizeof(struct iphdr)))
6137 return max_len;
6138
6139 /* access ihl as a u8 to avoid unaligned access on ia64 */
6140 hlen = (hdr.network[0] & 0x0F) << 2;
6141
6142 /* verify hlen meets minimum size requirements */
6143 if (hlen < sizeof(struct iphdr))
6144 return hdr.network - data;
6145
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006146 /* record next protocol if header is present */
6147 if (!hdr.ipv4->frag_off)
6148 nexthdr = hdr.ipv4->protocol;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006149 } else if (protocol == __constant_htons(ETH_P_IPV6)) {
6150 if ((hdr.network - data) > (max_len - sizeof(struct ipv6hdr)))
6151 return max_len;
6152
6153 /* record next protocol */
6154 nexthdr = hdr.ipv6->nexthdr;
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006155 hlen = sizeof(struct ipv6hdr);
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006156 } else {
6157 return hdr.network - data;
6158 }
6159
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006160 /* relocate pointer to start of L4 header */
6161 hdr.network += hlen;
6162
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006163 /* finally sort out TCP */
6164 if (nexthdr == IPPROTO_TCP) {
6165 if ((hdr.network - data) > (max_len - sizeof(struct tcphdr)))
6166 return max_len;
6167
6168 /* access doff as a u8 to avoid unaligned access on ia64 */
6169 hlen = (hdr.network[12] & 0xF0) >> 2;
6170
6171 /* verify hlen meets minimum size requirements */
6172 if (hlen < sizeof(struct tcphdr))
6173 return hdr.network - data;
6174
6175 hdr.network += hlen;
6176 } else if (nexthdr == IPPROTO_UDP) {
6177 if ((hdr.network - data) > (max_len - sizeof(struct udphdr)))
6178 return max_len;
6179
6180 hdr.network += sizeof(struct udphdr);
6181 }
6182
6183 /*
6184 * If everything has gone correctly hdr.network should be the
6185 * data section of the packet and will be the end of the header.
6186 * If not then it probably represents the end of the last recognized
6187 * header.
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006188 */
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006189 if ((hdr.network - data) < max_len)
6190 return hdr.network - data;
6191 else
6192 return max_len;
6193}
6194
6195/**
6196 * igb_pull_tail - igb specific version of skb_pull_tail
6197 * @rx_ring: rx descriptor ring packet is being transacted on
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006198 * @rx_desc: pointer to the EOP Rx descriptor
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006199 * @skb: pointer to current skb being adjusted
6200 *
6201 * This function is an igb specific version of __pskb_pull_tail. The
6202 * main difference between this version and the original function is that
6203 * this function can make several assumptions about the state of things
6204 * that allow for significant optimizations versus the standard function.
6205 * As a result we can do things like drop a frag and maintain an accurate
6206 * truesize for the skb.
6207 */
6208static void igb_pull_tail(struct igb_ring *rx_ring,
6209 union e1000_adv_rx_desc *rx_desc,
6210 struct sk_buff *skb)
6211{
6212 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
6213 unsigned char *va;
6214 unsigned int pull_len;
6215
6216 /*
6217 * it is valid to use page_address instead of kmap since we are
6218 * working with pages allocated out of the lomem pool per
6219 * alloc_page(GFP_ATOMIC)
6220 */
6221 va = skb_frag_address(frag);
6222
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006223 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
6224 /* retrieve timestamp from buffer */
6225 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
6226
6227 /* update pointers to remove timestamp header */
6228 skb_frag_size_sub(frag, IGB_TS_HDR_LEN);
6229 frag->page_offset += IGB_TS_HDR_LEN;
6230 skb->data_len -= IGB_TS_HDR_LEN;
6231 skb->len -= IGB_TS_HDR_LEN;
6232
6233 /* move va to start of packet data */
6234 va += IGB_TS_HDR_LEN;
6235 }
6236
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006237 /*
6238 * we need the header to contain the greater of either ETH_HLEN or
6239 * 60 bytes if the skb->len is less than 60 for skb_pad.
6240 */
6241 pull_len = igb_get_headlen(va, IGB_RX_HDR_LEN);
6242
6243 /* align pull length to size of long to optimize memcpy performance */
6244 skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
6245
6246 /* update all of the pointers */
6247 skb_frag_size_sub(frag, pull_len);
6248 frag->page_offset += pull_len;
6249 skb->data_len -= pull_len;
6250 skb->tail += pull_len;
6251}
6252
6253/**
6254 * igb_cleanup_headers - Correct corrupted or empty headers
6255 * @rx_ring: rx descriptor ring packet is being transacted on
6256 * @rx_desc: pointer to the EOP Rx descriptor
6257 * @skb: pointer to current skb being fixed
6258 *
6259 * Address the case where we are pulling data in on pages only
6260 * and as such no data is present in the skb header.
6261 *
6262 * In addition if skb is not at least 60 bytes we need to pad it so that
6263 * it is large enough to qualify as a valid Ethernet frame.
6264 *
6265 * Returns true if an error was encountered and skb was freed.
6266 **/
6267static bool igb_cleanup_headers(struct igb_ring *rx_ring,
6268 union e1000_adv_rx_desc *rx_desc,
6269 struct sk_buff *skb)
6270{
6271
6272 if (unlikely((igb_test_staterr(rx_desc,
6273 E1000_RXDEXT_ERR_FRAME_ERR_MASK)))) {
6274 struct net_device *netdev = rx_ring->netdev;
6275 if (!(netdev->features & NETIF_F_RXALL)) {
6276 dev_kfree_skb_any(skb);
6277 return true;
6278 }
6279 }
6280
6281 /* place header in linear portion of buffer */
6282 if (skb_is_nonlinear(skb))
6283 igb_pull_tail(rx_ring, rx_desc, skb);
6284
6285 /* if skb_pad returns an error the skb was freed */
6286 if (unlikely(skb->len < 60)) {
6287 int pad_len = 60 - skb->len;
6288
6289 if (skb_pad(skb, pad_len))
6290 return true;
6291 __skb_put(skb, pad_len);
6292 }
6293
6294 return false;
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006295}
6296
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006297/**
6298 * igb_process_skb_fields - Populate skb header fields from Rx descriptor
6299 * @rx_ring: rx descriptor ring packet is being transacted on
6300 * @rx_desc: pointer to the EOP Rx descriptor
6301 * @skb: pointer to current skb being populated
6302 *
6303 * This function checks the ring, descriptor, and packet information in
6304 * order to populate the hash, checksum, VLAN, timestamp, protocol, and
6305 * other fields within the skb.
6306 **/
6307static void igb_process_skb_fields(struct igb_ring *rx_ring,
6308 union e1000_adv_rx_desc *rx_desc,
6309 struct sk_buff *skb)
6310{
6311 struct net_device *dev = rx_ring->netdev;
6312
6313 igb_rx_hash(rx_ring, rx_desc, skb);
6314
6315 igb_rx_checksum(rx_ring, rx_desc, skb);
6316
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006317 igb_ptp_rx_hwtstamp(rx_ring->q_vector, rx_desc, skb);
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006318
6319 if ((dev->features & NETIF_F_HW_VLAN_RX) &&
6320 igb_test_staterr(rx_desc, E1000_RXD_STAT_VP)) {
6321 u16 vid;
6322 if (igb_test_staterr(rx_desc, E1000_RXDEXT_STATERR_LB) &&
6323 test_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags))
6324 vid = be16_to_cpu(rx_desc->wb.upper.vlan);
6325 else
6326 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
6327
6328 __vlan_hwaccel_put_tag(skb, vid);
6329 }
6330
6331 skb_record_rx_queue(skb, rx_ring->queue_index);
6332
6333 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
6334}
6335
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006336static bool igb_clean_rx_irq(struct igb_q_vector *q_vector, const int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08006337{
Alexander Duyck0ba82992011-08-26 07:45:47 +00006338 struct igb_ring *rx_ring = q_vector->rx.ring;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006339 struct sk_buff *skb = rx_ring->skb;
Auke Kok9d5c8242008-01-24 02:22:38 -08006340 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck16eb8812011-08-26 07:43:54 +00006341 u16 cleaned_count = igb_desc_unused(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08006342
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006343 do {
6344 union e1000_adv_rx_desc *rx_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08006345
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006346 /* return some buffers to hardware, one at a time is too slow */
6347 if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
6348 igb_alloc_rx_buffers(rx_ring, cleaned_count);
6349 cleaned_count = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006350 }
6351
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006352 rx_desc = IGB_RX_DESC(rx_ring, rx_ring->next_to_clean);
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006353
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006354 if (!igb_test_staterr(rx_desc, E1000_RXD_STAT_DD))
6355 break;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006356
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006357 /* retrieve a buffer from the ring */
6358 skb = igb_fetch_rx_buffer(rx_ring, rx_desc, skb);
Alexander Duyck16eb8812011-08-26 07:43:54 +00006359
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006360 /* exit if we failed to retrieve a buffer */
6361 if (!skb)
6362 break;
6363
6364 cleaned_count++;
6365
6366 /* fetch next buffer in frame if non-eop */
6367 if (igb_is_non_eop(rx_ring, rx_desc))
6368 continue;
Alexander Duyck44390ca2011-08-26 07:43:38 +00006369
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006370 /* verify the packet layout is correct */
6371 if (igb_cleanup_headers(rx_ring, rx_desc, skb)) {
6372 skb = NULL;
6373 continue;
Auke Kok9d5c8242008-01-24 02:22:38 -08006374 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006375
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006376 /* probably a little skewed due to removing CRC */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006377 total_bytes += skb->len;
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006378
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006379 /* populate checksum, timestamp, VLAN, and protocol */
6380 igb_process_skb_fields(rx_ring, rx_desc, skb);
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006381
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006382 napi_gro_receive(&q_vector->napi, skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08006383
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006384 /* reset skb pointer */
6385 skb = NULL;
6386
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006387 /* update budget accounting */
6388 total_packets++;
6389 } while (likely(total_packets < budget));
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006390
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006391 /* place incomplete frames back on ring for completion */
6392 rx_ring->skb = skb;
6393
Eric Dumazet12dcd862010-10-15 17:27:10 +00006394 u64_stats_update_begin(&rx_ring->rx_syncp);
Auke Kok9d5c8242008-01-24 02:22:38 -08006395 rx_ring->rx_stats.packets += total_packets;
6396 rx_ring->rx_stats.bytes += total_bytes;
Eric Dumazet12dcd862010-10-15 17:27:10 +00006397 u64_stats_update_end(&rx_ring->rx_syncp);
Alexander Duyck0ba82992011-08-26 07:45:47 +00006398 q_vector->rx.total_packets += total_packets;
6399 q_vector->rx.total_bytes += total_bytes;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006400
6401 if (cleaned_count)
Alexander Duyckcd392f52011-08-26 07:43:59 +00006402 igb_alloc_rx_buffers(rx_ring, cleaned_count);
Alexander Duyckc023cd82011-08-26 07:43:43 +00006403
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006404 return (total_packets < budget);
Auke Kok9d5c8242008-01-24 02:22:38 -08006405}
6406
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006407static bool igb_alloc_mapped_page(struct igb_ring *rx_ring,
6408 struct igb_rx_buffer *bi)
Alexander Duyckc023cd82011-08-26 07:43:43 +00006409{
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006410 struct page *page = bi->page;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006411 dma_addr_t dma;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006412
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006413 /* since we are recycling buffers we should seldom need to alloc */
6414 if (likely(page))
Alexander Duyckc023cd82011-08-26 07:43:43 +00006415 return true;
6416
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006417 /* alloc new page for storage */
6418 page = __skb_alloc_page(GFP_ATOMIC | __GFP_COLD, NULL);
6419 if (unlikely(!page)) {
6420 rx_ring->rx_stats.alloc_failed++;
6421 return false;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006422 }
6423
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006424 /* map page for use */
6425 dma = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
Alexander Duyckc023cd82011-08-26 07:43:43 +00006426
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006427 /*
6428 * if mapping failed free memory back to system since
6429 * there isn't much point in holding memory we can't use
6430 */
Alexander Duyckc023cd82011-08-26 07:43:43 +00006431 if (dma_mapping_error(rx_ring->dev, dma)) {
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006432 __free_page(page);
6433
Alexander Duyckc023cd82011-08-26 07:43:43 +00006434 rx_ring->rx_stats.alloc_failed++;
6435 return false;
6436 }
6437
6438 bi->dma = dma;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006439 bi->page = page;
6440 bi->page_offset = 0;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006441
Alexander Duyckc023cd82011-08-26 07:43:43 +00006442 return true;
6443}
6444
Auke Kok9d5c8242008-01-24 02:22:38 -08006445/**
Alexander Duyckcd392f52011-08-26 07:43:59 +00006446 * igb_alloc_rx_buffers - Replace used receive buffers; packet split
Auke Kok9d5c8242008-01-24 02:22:38 -08006447 * @adapter: address of board private structure
6448 **/
Alexander Duyckcd392f52011-08-26 07:43:59 +00006449void igb_alloc_rx_buffers(struct igb_ring *rx_ring, u16 cleaned_count)
Auke Kok9d5c8242008-01-24 02:22:38 -08006450{
Auke Kok9d5c8242008-01-24 02:22:38 -08006451 union e1000_adv_rx_desc *rx_desc;
Alexander Duyck06034642011-08-26 07:44:22 +00006452 struct igb_rx_buffer *bi;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006453 u16 i = rx_ring->next_to_use;
Auke Kok9d5c8242008-01-24 02:22:38 -08006454
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006455 /* nothing to do */
6456 if (!cleaned_count)
6457 return;
6458
Alexander Duyck601369062011-08-26 07:44:05 +00006459 rx_desc = IGB_RX_DESC(rx_ring, i);
Alexander Duyck06034642011-08-26 07:44:22 +00006460 bi = &rx_ring->rx_buffer_info[i];
Alexander Duyckc023cd82011-08-26 07:43:43 +00006461 i -= rx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08006462
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006463 do {
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006464 if (!igb_alloc_mapped_page(rx_ring, bi))
Alexander Duyckc023cd82011-08-26 07:43:43 +00006465 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08006466
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006467 /*
6468 * Refresh the desc even if buffer_addrs didn't change
6469 * because each write-back erases this info.
6470 */
6471 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
Auke Kok9d5c8242008-01-24 02:22:38 -08006472
Alexander Duyckc023cd82011-08-26 07:43:43 +00006473 rx_desc++;
6474 bi++;
Auke Kok9d5c8242008-01-24 02:22:38 -08006475 i++;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006476 if (unlikely(!i)) {
Alexander Duyck601369062011-08-26 07:44:05 +00006477 rx_desc = IGB_RX_DESC(rx_ring, 0);
Alexander Duyck06034642011-08-26 07:44:22 +00006478 bi = rx_ring->rx_buffer_info;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006479 i -= rx_ring->count;
6480 }
6481
6482 /* clear the hdr_addr for the next_to_use descriptor */
6483 rx_desc->read.hdr_addr = 0;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006484
6485 cleaned_count--;
6486 } while (cleaned_count);
Auke Kok9d5c8242008-01-24 02:22:38 -08006487
Alexander Duyckc023cd82011-08-26 07:43:43 +00006488 i += rx_ring->count;
6489
Auke Kok9d5c8242008-01-24 02:22:38 -08006490 if (rx_ring->next_to_use != i) {
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006491 /* record the next descriptor to use */
Auke Kok9d5c8242008-01-24 02:22:38 -08006492 rx_ring->next_to_use = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08006493
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006494 /* update next to alloc since we have filled the ring */
6495 rx_ring->next_to_alloc = i;
6496
6497 /*
6498 * Force memory writes to complete before letting h/w
Auke Kok9d5c8242008-01-24 02:22:38 -08006499 * know there are new descriptors to fetch. (Only
6500 * applicable for weak-ordered memory model archs,
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006501 * such as IA-64).
6502 */
Auke Kok9d5c8242008-01-24 02:22:38 -08006503 wmb();
Alexander Duyckfce99e32009-10-27 15:51:27 +00006504 writel(i, rx_ring->tail);
Auke Kok9d5c8242008-01-24 02:22:38 -08006505 }
6506}
6507
6508/**
6509 * igb_mii_ioctl -
6510 * @netdev:
6511 * @ifreq:
6512 * @cmd:
6513 **/
6514static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6515{
6516 struct igb_adapter *adapter = netdev_priv(netdev);
6517 struct mii_ioctl_data *data = if_mii(ifr);
6518
6519 if (adapter->hw.phy.media_type != e1000_media_type_copper)
6520 return -EOPNOTSUPP;
6521
6522 switch (cmd) {
6523 case SIOCGMIIPHY:
6524 data->phy_id = adapter->hw.phy.addr;
6525 break;
6526 case SIOCGMIIREG:
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08006527 if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
6528 &data->val_out))
Auke Kok9d5c8242008-01-24 02:22:38 -08006529 return -EIO;
6530 break;
6531 case SIOCSMIIREG:
6532 default:
6533 return -EOPNOTSUPP;
6534 }
6535 return 0;
6536}
6537
6538/**
6539 * igb_ioctl -
6540 * @netdev:
6541 * @ifreq:
6542 * @cmd:
6543 **/
6544static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6545{
6546 switch (cmd) {
6547 case SIOCGMIIPHY:
6548 case SIOCGMIIREG:
6549 case SIOCSMIIREG:
6550 return igb_mii_ioctl(netdev, ifr, cmd);
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00006551 case SIOCSHWTSTAMP:
Matthew Vicka79f4f82012-08-10 05:40:44 +00006552 return igb_ptp_hwtstamp_ioctl(netdev, ifr, cmd);
Auke Kok9d5c8242008-01-24 02:22:38 -08006553 default:
6554 return -EOPNOTSUPP;
6555 }
6556}
6557
Alexander Duyck009bc062009-07-23 18:08:35 +00006558s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6559{
6560 struct igb_adapter *adapter = hw->back;
Alexander Duyck009bc062009-07-23 18:08:35 +00006561
Jiang Liu23d028c2012-08-20 13:32:20 -06006562 if (pcie_capability_read_word(adapter->pdev, reg, value))
Alexander Duyck009bc062009-07-23 18:08:35 +00006563 return -E1000_ERR_CONFIG;
6564
Alexander Duyck009bc062009-07-23 18:08:35 +00006565 return 0;
6566}
6567
6568s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6569{
6570 struct igb_adapter *adapter = hw->back;
Alexander Duyck009bc062009-07-23 18:08:35 +00006571
Jiang Liu23d028c2012-08-20 13:32:20 -06006572 if (pcie_capability_write_word(adapter->pdev, reg, *value))
Alexander Duyck009bc062009-07-23 18:08:35 +00006573 return -E1000_ERR_CONFIG;
6574
Alexander Duyck009bc062009-07-23 18:08:35 +00006575 return 0;
6576}
6577
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006578static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features)
Auke Kok9d5c8242008-01-24 02:22:38 -08006579{
6580 struct igb_adapter *adapter = netdev_priv(netdev);
6581 struct e1000_hw *hw = &adapter->hw;
6582 u32 ctrl, rctl;
Alexander Duyck5faf0302011-08-26 07:46:08 +00006583 bool enable = !!(features & NETIF_F_HW_VLAN_RX);
Auke Kok9d5c8242008-01-24 02:22:38 -08006584
Alexander Duyck5faf0302011-08-26 07:46:08 +00006585 if (enable) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006586 /* enable VLAN tag insert/strip */
6587 ctrl = rd32(E1000_CTRL);
6588 ctrl |= E1000_CTRL_VME;
6589 wr32(E1000_CTRL, ctrl);
6590
Alexander Duyck51466232009-10-27 23:47:35 +00006591 /* Disable CFI check */
Auke Kok9d5c8242008-01-24 02:22:38 -08006592 rctl = rd32(E1000_RCTL);
Auke Kok9d5c8242008-01-24 02:22:38 -08006593 rctl &= ~E1000_RCTL_CFIEN;
6594 wr32(E1000_RCTL, rctl);
Auke Kok9d5c8242008-01-24 02:22:38 -08006595 } else {
6596 /* disable VLAN tag insert/strip */
6597 ctrl = rd32(E1000_CTRL);
6598 ctrl &= ~E1000_CTRL_VME;
6599 wr32(E1000_CTRL, ctrl);
Auke Kok9d5c8242008-01-24 02:22:38 -08006600 }
6601
Alexander Duycke1739522009-02-19 20:39:44 -08006602 igb_rlpml_set(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006603}
6604
Jiri Pirko8e586132011-12-08 19:52:37 -05006605static int igb_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
Auke Kok9d5c8242008-01-24 02:22:38 -08006606{
6607 struct igb_adapter *adapter = netdev_priv(netdev);
6608 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006609 int pf_id = adapter->vfs_allocated_count;
Auke Kok9d5c8242008-01-24 02:22:38 -08006610
Alexander Duyck51466232009-10-27 23:47:35 +00006611 /* attempt to add filter to vlvf array */
6612 igb_vlvf_set(adapter, vid, true, pf_id);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006613
Alexander Duyck51466232009-10-27 23:47:35 +00006614 /* add the filter since PF can receive vlans w/o entry in vlvf */
6615 igb_vfta_set(hw, vid, true);
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006616
6617 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05006618
6619 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08006620}
6621
Jiri Pirko8e586132011-12-08 19:52:37 -05006622static int igb_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
Auke Kok9d5c8242008-01-24 02:22:38 -08006623{
6624 struct igb_adapter *adapter = netdev_priv(netdev);
6625 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006626 int pf_id = adapter->vfs_allocated_count;
Alexander Duyck51466232009-10-27 23:47:35 +00006627 s32 err;
Auke Kok9d5c8242008-01-24 02:22:38 -08006628
Alexander Duyck51466232009-10-27 23:47:35 +00006629 /* remove vlan from VLVF table array */
6630 err = igb_vlvf_set(adapter, vid, false, pf_id);
Auke Kok9d5c8242008-01-24 02:22:38 -08006631
Alexander Duyck51466232009-10-27 23:47:35 +00006632 /* if vid was not present in VLVF just remove it from table */
6633 if (err)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006634 igb_vfta_set(hw, vid, false);
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006635
6636 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05006637
6638 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08006639}
6640
6641static void igb_restore_vlan(struct igb_adapter *adapter)
6642{
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006643 u16 vid;
Auke Kok9d5c8242008-01-24 02:22:38 -08006644
Alexander Duyck5faf0302011-08-26 07:46:08 +00006645 igb_vlan_mode(adapter->netdev, adapter->netdev->features);
6646
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006647 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
6648 igb_vlan_rx_add_vid(adapter->netdev, vid);
Auke Kok9d5c8242008-01-24 02:22:38 -08006649}
6650
David Decotigny14ad2512011-04-27 18:32:43 +00006651int igb_set_spd_dplx(struct igb_adapter *adapter, u32 spd, u8 dplx)
Auke Kok9d5c8242008-01-24 02:22:38 -08006652{
Alexander Duyck090b1792009-10-27 23:51:55 +00006653 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08006654 struct e1000_mac_info *mac = &adapter->hw.mac;
6655
6656 mac->autoneg = 0;
6657
David Decotigny14ad2512011-04-27 18:32:43 +00006658 /* Make sure dplx is at most 1 bit and lsb of speed is not set
6659 * for the switch() below to work */
6660 if ((spd & 1) || (dplx & ~1))
6661 goto err_inval;
6662
Carolyn Wybornycd2638a2010-10-12 22:27:02 +00006663 /* Fiber NIC's only allow 1000 Gbps Full duplex */
6664 if ((adapter->hw.phy.media_type == e1000_media_type_internal_serdes) &&
David Decotigny14ad2512011-04-27 18:32:43 +00006665 spd != SPEED_1000 &&
6666 dplx != DUPLEX_FULL)
6667 goto err_inval;
Carolyn Wybornycd2638a2010-10-12 22:27:02 +00006668
David Decotigny14ad2512011-04-27 18:32:43 +00006669 switch (spd + dplx) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006670 case SPEED_10 + DUPLEX_HALF:
6671 mac->forced_speed_duplex = ADVERTISE_10_HALF;
6672 break;
6673 case SPEED_10 + DUPLEX_FULL:
6674 mac->forced_speed_duplex = ADVERTISE_10_FULL;
6675 break;
6676 case SPEED_100 + DUPLEX_HALF:
6677 mac->forced_speed_duplex = ADVERTISE_100_HALF;
6678 break;
6679 case SPEED_100 + DUPLEX_FULL:
6680 mac->forced_speed_duplex = ADVERTISE_100_FULL;
6681 break;
6682 case SPEED_1000 + DUPLEX_FULL:
6683 mac->autoneg = 1;
6684 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
6685 break;
6686 case SPEED_1000 + DUPLEX_HALF: /* not supported */
6687 default:
David Decotigny14ad2512011-04-27 18:32:43 +00006688 goto err_inval;
Auke Kok9d5c8242008-01-24 02:22:38 -08006689 }
Jesse Brandeburg8376dad2012-07-26 02:31:19 +00006690
6691 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
6692 adapter->hw.phy.mdix = AUTO_ALL_MODES;
6693
Auke Kok9d5c8242008-01-24 02:22:38 -08006694 return 0;
David Decotigny14ad2512011-04-27 18:32:43 +00006695
6696err_inval:
6697 dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n");
6698 return -EINVAL;
Auke Kok9d5c8242008-01-24 02:22:38 -08006699}
6700
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006701static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake,
6702 bool runtime)
Auke Kok9d5c8242008-01-24 02:22:38 -08006703{
6704 struct net_device *netdev = pci_get_drvdata(pdev);
6705 struct igb_adapter *adapter = netdev_priv(netdev);
6706 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07006707 u32 ctrl, rctl, status;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006708 u32 wufc = runtime ? E1000_WUFC_LNKC : adapter->wol;
Auke Kok9d5c8242008-01-24 02:22:38 -08006709#ifdef CONFIG_PM
6710 int retval = 0;
6711#endif
6712
6713 netif_device_detach(netdev);
6714
Alexander Duycka88f10e2008-07-08 15:13:38 -07006715 if (netif_running(netdev))
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006716 __igb_close(netdev, true);
Alexander Duycka88f10e2008-07-08 15:13:38 -07006717
Alexander Duyck047e0032009-10-27 15:49:27 +00006718 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006719
6720#ifdef CONFIG_PM
6721 retval = pci_save_state(pdev);
6722 if (retval)
6723 return retval;
6724#endif
6725
6726 status = rd32(E1000_STATUS);
6727 if (status & E1000_STATUS_LU)
6728 wufc &= ~E1000_WUFC_LNKC;
6729
6730 if (wufc) {
6731 igb_setup_rctl(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00006732 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08006733
6734 /* turn on all-multi mode if wake on multicast is enabled */
6735 if (wufc & E1000_WUFC_MC) {
6736 rctl = rd32(E1000_RCTL);
6737 rctl |= E1000_RCTL_MPE;
6738 wr32(E1000_RCTL, rctl);
6739 }
6740
6741 ctrl = rd32(E1000_CTRL);
6742 /* advertise wake from D3Cold */
6743 #define E1000_CTRL_ADVD3WUC 0x00100000
6744 /* phy power management enable */
6745 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
6746 ctrl |= E1000_CTRL_ADVD3WUC;
6747 wr32(E1000_CTRL, ctrl);
6748
Auke Kok9d5c8242008-01-24 02:22:38 -08006749 /* Allow time for pending master requests to run */
Alexander Duyck330a6d62009-10-27 23:51:35 +00006750 igb_disable_pcie_master(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08006751
6752 wr32(E1000_WUC, E1000_WUC_PME_EN);
6753 wr32(E1000_WUFC, wufc);
Auke Kok9d5c8242008-01-24 02:22:38 -08006754 } else {
6755 wr32(E1000_WUC, 0);
6756 wr32(E1000_WUFC, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08006757 }
6758
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006759 *enable_wake = wufc || adapter->en_mng_pt;
6760 if (!*enable_wake)
Nick Nunley88a268c2010-02-17 01:01:59 +00006761 igb_power_down_link(adapter);
6762 else
6763 igb_power_up_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006764
6765 /* Release control of h/w to f/w. If f/w is AMT enabled, this
6766 * would have already happened in close and is redundant. */
6767 igb_release_hw_control(adapter);
6768
6769 pci_disable_device(pdev);
6770
Auke Kok9d5c8242008-01-24 02:22:38 -08006771 return 0;
6772}
6773
6774#ifdef CONFIG_PM
Emil Tantilovd9dd9662012-01-28 08:10:35 +00006775#ifdef CONFIG_PM_SLEEP
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006776static int igb_suspend(struct device *dev)
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006777{
6778 int retval;
6779 bool wake;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006780 struct pci_dev *pdev = to_pci_dev(dev);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006781
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006782 retval = __igb_shutdown(pdev, &wake, 0);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006783 if (retval)
6784 return retval;
6785
6786 if (wake) {
6787 pci_prepare_to_sleep(pdev);
6788 } else {
6789 pci_wake_from_d3(pdev, false);
6790 pci_set_power_state(pdev, PCI_D3hot);
6791 }
6792
6793 return 0;
6794}
Emil Tantilovd9dd9662012-01-28 08:10:35 +00006795#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006796
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006797static int igb_resume(struct device *dev)
Auke Kok9d5c8242008-01-24 02:22:38 -08006798{
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006799 struct pci_dev *pdev = to_pci_dev(dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08006800 struct net_device *netdev = pci_get_drvdata(pdev);
6801 struct igb_adapter *adapter = netdev_priv(netdev);
6802 struct e1000_hw *hw = &adapter->hw;
6803 u32 err;
6804
6805 pci_set_power_state(pdev, PCI_D0);
6806 pci_restore_state(pdev);
Nick Nunleyb94f2d72010-02-17 01:02:19 +00006807 pci_save_state(pdev);
Taku Izumi42bfd33a2008-06-20 12:10:30 +09006808
Alexander Duyckaed5dec2009-02-06 23:16:04 +00006809 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08006810 if (err) {
6811 dev_err(&pdev->dev,
6812 "igb: Cannot enable PCI device from suspend\n");
6813 return err;
6814 }
6815 pci_set_master(pdev);
6816
6817 pci_enable_wake(pdev, PCI_D3hot, 0);
6818 pci_enable_wake(pdev, PCI_D3cold, 0);
6819
Stefan Assmann53c7d062012-12-04 06:00:12 +00006820 if (igb_init_interrupt_scheme(adapter, true)) {
Alexander Duycka88f10e2008-07-08 15:13:38 -07006821 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
6822 return -ENOMEM;
Auke Kok9d5c8242008-01-24 02:22:38 -08006823 }
6824
Auke Kok9d5c8242008-01-24 02:22:38 -08006825 igb_reset(adapter);
Alexander Duycka8564f02009-02-06 23:21:10 +00006826
6827 /* let the f/w know that the h/w is now under the control of the
6828 * driver. */
6829 igb_get_hw_control(adapter);
6830
Auke Kok9d5c8242008-01-24 02:22:38 -08006831 wr32(E1000_WUS, ~0);
6832
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006833 if (netdev->flags & IFF_UP) {
Alexander Duyck0c2cc022012-09-25 00:31:22 +00006834 rtnl_lock();
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006835 err = __igb_open(netdev, true);
Alexander Duyck0c2cc022012-09-25 00:31:22 +00006836 rtnl_unlock();
Alexander Duycka88f10e2008-07-08 15:13:38 -07006837 if (err)
6838 return err;
6839 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006840
6841 netif_device_attach(netdev);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006842 return 0;
6843}
6844
6845#ifdef CONFIG_PM_RUNTIME
6846static int igb_runtime_idle(struct device *dev)
6847{
6848 struct pci_dev *pdev = to_pci_dev(dev);
6849 struct net_device *netdev = pci_get_drvdata(pdev);
6850 struct igb_adapter *adapter = netdev_priv(netdev);
6851
6852 if (!igb_has_link(adapter))
6853 pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
6854
6855 return -EBUSY;
6856}
6857
6858static int igb_runtime_suspend(struct device *dev)
6859{
6860 struct pci_dev *pdev = to_pci_dev(dev);
6861 int retval;
6862 bool wake;
6863
6864 retval = __igb_shutdown(pdev, &wake, 1);
6865 if (retval)
6866 return retval;
6867
6868 if (wake) {
6869 pci_prepare_to_sleep(pdev);
6870 } else {
6871 pci_wake_from_d3(pdev, false);
6872 pci_set_power_state(pdev, PCI_D3hot);
6873 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006874
Auke Kok9d5c8242008-01-24 02:22:38 -08006875 return 0;
6876}
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006877
6878static int igb_runtime_resume(struct device *dev)
6879{
6880 return igb_resume(dev);
6881}
6882#endif /* CONFIG_PM_RUNTIME */
Auke Kok9d5c8242008-01-24 02:22:38 -08006883#endif
6884
6885static void igb_shutdown(struct pci_dev *pdev)
6886{
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006887 bool wake;
6888
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006889 __igb_shutdown(pdev, &wake, 0);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006890
6891 if (system_state == SYSTEM_POWER_OFF) {
6892 pci_wake_from_d3(pdev, wake);
6893 pci_set_power_state(pdev, PCI_D3hot);
6894 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006895}
6896
6897#ifdef CONFIG_NET_POLL_CONTROLLER
6898/*
6899 * Polling 'interrupt' - used by things like netconsole to send skbs
6900 * without having to re-enable interrupts. It's not called while
6901 * the interrupt routine is executing.
6902 */
6903static void igb_netpoll(struct net_device *netdev)
6904{
6905 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00006906 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00006907 struct igb_q_vector *q_vector;
Auke Kok9d5c8242008-01-24 02:22:38 -08006908 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08006909
Alexander Duyck047e0032009-10-27 15:49:27 +00006910 for (i = 0; i < adapter->num_q_vectors; i++) {
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00006911 q_vector = adapter->q_vector[i];
6912 if (adapter->msix_entries)
6913 wr32(E1000_EIMC, q_vector->eims_value);
6914 else
6915 igb_irq_disable(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00006916 napi_schedule(&q_vector->napi);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00006917 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006918}
6919#endif /* CONFIG_NET_POLL_CONTROLLER */
6920
6921/**
6922 * igb_io_error_detected - called when PCI error is detected
6923 * @pdev: Pointer to PCI device
6924 * @state: The current pci connection state
6925 *
6926 * This function is called after a PCI bus error affecting
6927 * this device has been detected.
6928 */
6929static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
6930 pci_channel_state_t state)
6931{
6932 struct net_device *netdev = pci_get_drvdata(pdev);
6933 struct igb_adapter *adapter = netdev_priv(netdev);
6934
6935 netif_device_detach(netdev);
6936
Alexander Duyck59ed6ee2009-06-30 12:46:34 +00006937 if (state == pci_channel_io_perm_failure)
6938 return PCI_ERS_RESULT_DISCONNECT;
6939
Auke Kok9d5c8242008-01-24 02:22:38 -08006940 if (netif_running(netdev))
6941 igb_down(adapter);
6942 pci_disable_device(pdev);
6943
6944 /* Request a slot slot reset. */
6945 return PCI_ERS_RESULT_NEED_RESET;
6946}
6947
6948/**
6949 * igb_io_slot_reset - called after the pci bus has been reset.
6950 * @pdev: Pointer to PCI device
6951 *
6952 * Restart the card from scratch, as if from a cold-boot. Implementation
6953 * resembles the first-half of the igb_resume routine.
6954 */
6955static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
6956{
6957 struct net_device *netdev = pci_get_drvdata(pdev);
6958 struct igb_adapter *adapter = netdev_priv(netdev);
6959 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck40a914f2008-11-27 00:24:37 -08006960 pci_ers_result_t result;
Taku Izumi42bfd33a2008-06-20 12:10:30 +09006961 int err;
Auke Kok9d5c8242008-01-24 02:22:38 -08006962
Alexander Duyckaed5dec2009-02-06 23:16:04 +00006963 if (pci_enable_device_mem(pdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006964 dev_err(&pdev->dev,
6965 "Cannot re-enable PCI device after reset.\n");
Alexander Duyck40a914f2008-11-27 00:24:37 -08006966 result = PCI_ERS_RESULT_DISCONNECT;
6967 } else {
6968 pci_set_master(pdev);
6969 pci_restore_state(pdev);
Nick Nunleyb94f2d72010-02-17 01:02:19 +00006970 pci_save_state(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08006971
6972 pci_enable_wake(pdev, PCI_D3hot, 0);
6973 pci_enable_wake(pdev, PCI_D3cold, 0);
6974
6975 igb_reset(adapter);
6976 wr32(E1000_WUS, ~0);
6977 result = PCI_ERS_RESULT_RECOVERED;
Auke Kok9d5c8242008-01-24 02:22:38 -08006978 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006979
Jeff Kirsherea943d42008-12-11 20:34:19 -08006980 err = pci_cleanup_aer_uncorrect_error_status(pdev);
6981 if (err) {
6982 dev_err(&pdev->dev, "pci_cleanup_aer_uncorrect_error_status "
6983 "failed 0x%0x\n", err);
6984 /* non-fatal, continue */
6985 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006986
Alexander Duyck40a914f2008-11-27 00:24:37 -08006987 return result;
Auke Kok9d5c8242008-01-24 02:22:38 -08006988}
6989
6990/**
6991 * igb_io_resume - called when traffic can start flowing again.
6992 * @pdev: Pointer to PCI device
6993 *
6994 * This callback is called when the error recovery driver tells us that
6995 * its OK to resume normal operation. Implementation resembles the
6996 * second-half of the igb_resume routine.
6997 */
6998static void igb_io_resume(struct pci_dev *pdev)
6999{
7000 struct net_device *netdev = pci_get_drvdata(pdev);
7001 struct igb_adapter *adapter = netdev_priv(netdev);
7002
Auke Kok9d5c8242008-01-24 02:22:38 -08007003 if (netif_running(netdev)) {
7004 if (igb_up(adapter)) {
7005 dev_err(&pdev->dev, "igb_up failed after reset\n");
7006 return;
7007 }
7008 }
7009
7010 netif_device_attach(netdev);
7011
7012 /* let the f/w know that the h/w is now under the control of the
7013 * driver. */
7014 igb_get_hw_control(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08007015}
7016
Alexander Duyck26ad9172009-10-05 06:32:49 +00007017static void igb_rar_set_qsel(struct igb_adapter *adapter, u8 *addr, u32 index,
7018 u8 qsel)
7019{
7020 u32 rar_low, rar_high;
7021 struct e1000_hw *hw = &adapter->hw;
7022
7023 /* HW expects these in little endian so we reverse the byte order
7024 * from network order (big endian) to little endian
7025 */
7026 rar_low = ((u32) addr[0] | ((u32) addr[1] << 8) |
7027 ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
7028 rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
7029
7030 /* Indicate to hardware the Address is Valid. */
7031 rar_high |= E1000_RAH_AV;
7032
7033 if (hw->mac.type == e1000_82575)
7034 rar_high |= E1000_RAH_POOL_1 * qsel;
7035 else
7036 rar_high |= E1000_RAH_POOL_1 << qsel;
7037
7038 wr32(E1000_RAL(index), rar_low);
7039 wrfl();
7040 wr32(E1000_RAH(index), rar_high);
7041 wrfl();
7042}
7043
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007044static int igb_set_vf_mac(struct igb_adapter *adapter,
7045 int vf, unsigned char *mac_addr)
7046{
7047 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00007048 /* VF MAC addresses start at end of receive addresses and moves
7049 * torwards the first, as a result a collision should not be possible */
7050 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007051
Alexander Duyck37680112009-02-19 20:40:30 -08007052 memcpy(adapter->vf_data[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007053
Alexander Duyck26ad9172009-10-05 06:32:49 +00007054 igb_rar_set_qsel(adapter, mac_addr, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007055
7056 return 0;
7057}
7058
Williams, Mitch A8151d292010-02-10 01:44:24 +00007059static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
7060{
7061 struct igb_adapter *adapter = netdev_priv(netdev);
7062 if (!is_valid_ether_addr(mac) || (vf >= adapter->vfs_allocated_count))
7063 return -EINVAL;
7064 adapter->vf_data[vf].flags |= IGB_VF_FLAG_PF_SET_MAC;
7065 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
7066 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
7067 " change effective.");
7068 if (test_bit(__IGB_DOWN, &adapter->state)) {
7069 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
7070 " but the PF device is not up.\n");
7071 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
7072 " attempting to use the VF device.\n");
7073 }
7074 return igb_set_vf_mac(adapter, vf, mac);
7075}
7076
Lior Levy17dc5662011-02-08 02:28:46 +00007077static int igb_link_mbps(int internal_link_speed)
7078{
7079 switch (internal_link_speed) {
7080 case SPEED_100:
7081 return 100;
7082 case SPEED_1000:
7083 return 1000;
7084 default:
7085 return 0;
7086 }
7087}
7088
7089static void igb_set_vf_rate_limit(struct e1000_hw *hw, int vf, int tx_rate,
7090 int link_speed)
7091{
7092 int rf_dec, rf_int;
7093 u32 bcnrc_val;
7094
7095 if (tx_rate != 0) {
7096 /* Calculate the rate factor values to set */
7097 rf_int = link_speed / tx_rate;
7098 rf_dec = (link_speed - (rf_int * tx_rate));
7099 rf_dec = (rf_dec * (1<<E1000_RTTBCNRC_RF_INT_SHIFT)) / tx_rate;
7100
7101 bcnrc_val = E1000_RTTBCNRC_RS_ENA;
7102 bcnrc_val |= ((rf_int<<E1000_RTTBCNRC_RF_INT_SHIFT) &
7103 E1000_RTTBCNRC_RF_INT_MASK);
7104 bcnrc_val |= (rf_dec & E1000_RTTBCNRC_RF_DEC_MASK);
7105 } else {
7106 bcnrc_val = 0;
7107 }
7108
7109 wr32(E1000_RTTDQSEL, vf); /* vf X uses queue X */
Lior Levyf00b0da2011-06-04 06:05:03 +00007110 /*
7111 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
7112 * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported.
7113 */
7114 wr32(E1000_RTTBCNRM, 0x14);
Lior Levy17dc5662011-02-08 02:28:46 +00007115 wr32(E1000_RTTBCNRC, bcnrc_val);
7116}
7117
7118static void igb_check_vf_rate_limit(struct igb_adapter *adapter)
7119{
7120 int actual_link_speed, i;
7121 bool reset_rate = false;
7122
7123 /* VF TX rate limit was not set or not supported */
7124 if ((adapter->vf_rate_link_speed == 0) ||
7125 (adapter->hw.mac.type != e1000_82576))
7126 return;
7127
7128 actual_link_speed = igb_link_mbps(adapter->link_speed);
7129 if (actual_link_speed != adapter->vf_rate_link_speed) {
7130 reset_rate = true;
7131 adapter->vf_rate_link_speed = 0;
7132 dev_info(&adapter->pdev->dev,
7133 "Link speed has been changed. VF Transmit "
7134 "rate is disabled\n");
7135 }
7136
7137 for (i = 0; i < adapter->vfs_allocated_count; i++) {
7138 if (reset_rate)
7139 adapter->vf_data[i].tx_rate = 0;
7140
7141 igb_set_vf_rate_limit(&adapter->hw, i,
7142 adapter->vf_data[i].tx_rate,
7143 actual_link_speed);
7144 }
7145}
7146
Williams, Mitch A8151d292010-02-10 01:44:24 +00007147static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
7148{
Lior Levy17dc5662011-02-08 02:28:46 +00007149 struct igb_adapter *adapter = netdev_priv(netdev);
7150 struct e1000_hw *hw = &adapter->hw;
7151 int actual_link_speed;
7152
7153 if (hw->mac.type != e1000_82576)
7154 return -EOPNOTSUPP;
7155
7156 actual_link_speed = igb_link_mbps(adapter->link_speed);
7157 if ((vf >= adapter->vfs_allocated_count) ||
7158 (!(rd32(E1000_STATUS) & E1000_STATUS_LU)) ||
7159 (tx_rate < 0) || (tx_rate > actual_link_speed))
7160 return -EINVAL;
7161
7162 adapter->vf_rate_link_speed = actual_link_speed;
7163 adapter->vf_data[vf].tx_rate = (u16)tx_rate;
7164 igb_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
7165
7166 return 0;
Williams, Mitch A8151d292010-02-10 01:44:24 +00007167}
7168
7169static int igb_ndo_get_vf_config(struct net_device *netdev,
7170 int vf, struct ifla_vf_info *ivi)
7171{
7172 struct igb_adapter *adapter = netdev_priv(netdev);
7173 if (vf >= adapter->vfs_allocated_count)
7174 return -EINVAL;
7175 ivi->vf = vf;
7176 memcpy(&ivi->mac, adapter->vf_data[vf].vf_mac_addresses, ETH_ALEN);
Lior Levy17dc5662011-02-08 02:28:46 +00007177 ivi->tx_rate = adapter->vf_data[vf].tx_rate;
Williams, Mitch A8151d292010-02-10 01:44:24 +00007178 ivi->vlan = adapter->vf_data[vf].pf_vlan;
7179 ivi->qos = adapter->vf_data[vf].pf_qos;
7180 return 0;
7181}
7182
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007183static void igb_vmm_control(struct igb_adapter *adapter)
7184{
7185 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck10d8e902009-10-27 15:54:04 +00007186 u32 reg;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007187
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007188 switch (hw->mac.type) {
7189 case e1000_82575:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00007190 case e1000_i210:
7191 case e1000_i211:
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007192 default:
7193 /* replication is not supported for 82575 */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007194 return;
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007195 case e1000_82576:
7196 /* notify HW that the MAC is adding vlan tags */
7197 reg = rd32(E1000_DTXCTL);
7198 reg |= E1000_DTXCTL_VLAN_ADDED;
7199 wr32(E1000_DTXCTL, reg);
7200 case e1000_82580:
7201 /* enable replication vlan tag stripping */
7202 reg = rd32(E1000_RPLOLR);
7203 reg |= E1000_RPLOLR_STRVLAN;
7204 wr32(E1000_RPLOLR, reg);
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +00007205 case e1000_i350:
7206 /* none of the above registers are supported by i350 */
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007207 break;
7208 }
Alexander Duyck10d8e902009-10-27 15:54:04 +00007209
Alexander Duyckd4960302009-10-27 15:53:45 +00007210 if (adapter->vfs_allocated_count) {
7211 igb_vmdq_set_loopback_pf(hw, true);
7212 igb_vmdq_set_replication_pf(hw, true);
Greg Rose13800462010-11-06 02:08:26 +00007213 igb_vmdq_set_anti_spoofing_pf(hw, true,
7214 adapter->vfs_allocated_count);
Alexander Duyckd4960302009-10-27 15:53:45 +00007215 } else {
7216 igb_vmdq_set_loopback_pf(hw, false);
7217 igb_vmdq_set_replication_pf(hw, false);
7218 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007219}
7220
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007221static void igb_init_dmac(struct igb_adapter *adapter, u32 pba)
7222{
7223 struct e1000_hw *hw = &adapter->hw;
7224 u32 dmac_thr;
7225 u16 hwm;
7226
7227 if (hw->mac.type > e1000_82580) {
7228 if (adapter->flags & IGB_FLAG_DMAC) {
7229 u32 reg;
7230
7231 /* force threshold to 0. */
7232 wr32(E1000_DMCTXTH, 0);
7233
7234 /*
Matthew Vicke8c626e2011-11-17 08:33:12 +00007235 * DMA Coalescing high water mark needs to be greater
7236 * than the Rx threshold. Set hwm to PBA - max frame
7237 * size in 16B units, capping it at PBA - 6KB.
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007238 */
Matthew Vicke8c626e2011-11-17 08:33:12 +00007239 hwm = 64 * pba - adapter->max_frame_size / 16;
7240 if (hwm < 64 * (pba - 6))
7241 hwm = 64 * (pba - 6);
7242 reg = rd32(E1000_FCRTC);
7243 reg &= ~E1000_FCRTC_RTH_COAL_MASK;
7244 reg |= ((hwm << E1000_FCRTC_RTH_COAL_SHIFT)
7245 & E1000_FCRTC_RTH_COAL_MASK);
7246 wr32(E1000_FCRTC, reg);
7247
7248 /*
7249 * Set the DMA Coalescing Rx threshold to PBA - 2 * max
7250 * frame size, capping it at PBA - 10KB.
7251 */
7252 dmac_thr = pba - adapter->max_frame_size / 512;
7253 if (dmac_thr < pba - 10)
7254 dmac_thr = pba - 10;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007255 reg = rd32(E1000_DMACR);
7256 reg &= ~E1000_DMACR_DMACTHR_MASK;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007257 reg |= ((dmac_thr << E1000_DMACR_DMACTHR_SHIFT)
7258 & E1000_DMACR_DMACTHR_MASK);
7259
7260 /* transition to L0x or L1 if available..*/
7261 reg |= (E1000_DMACR_DMAC_EN | E1000_DMACR_DMAC_LX_MASK);
7262
7263 /* watchdog timer= +-1000 usec in 32usec intervals */
7264 reg |= (1000 >> 5);
Matthew Vick0c02dd92012-04-14 05:20:32 +00007265
7266 /* Disable BMC-to-OS Watchdog Enable */
7267 reg &= ~E1000_DMACR_DC_BMC2OSW_EN;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007268 wr32(E1000_DMACR, reg);
7269
7270 /*
7271 * no lower threshold to disable
7272 * coalescing(smart fifb)-UTRESH=0
7273 */
7274 wr32(E1000_DMCRTRH, 0);
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007275
7276 reg = (IGB_DMCTLX_DCFLUSH_DIS | 0x4);
7277
7278 wr32(E1000_DMCTLX, reg);
7279
7280 /*
7281 * free space in tx packet buffer to wake from
7282 * DMA coal
7283 */
7284 wr32(E1000_DMCTXTH, (IGB_MIN_TXPBSIZE -
7285 (IGB_TX_BUF_4096 + adapter->max_frame_size)) >> 6);
7286
7287 /*
7288 * make low power state decision controlled
7289 * by DMA coal
7290 */
7291 reg = rd32(E1000_PCIEMISC);
7292 reg &= ~E1000_PCIEMISC_LX_DECISION;
7293 wr32(E1000_PCIEMISC, reg);
7294 } /* endif adapter->dmac is not disabled */
7295 } else if (hw->mac.type == e1000_82580) {
7296 u32 reg = rd32(E1000_PCIEMISC);
7297 wr32(E1000_PCIEMISC, reg & ~E1000_PCIEMISC_LX_DECISION);
7298 wr32(E1000_DMACR, 0);
7299 }
7300}
7301
Auke Kok9d5c8242008-01-24 02:22:38 -08007302/* igb_main.c */