blob: d838ab1ea96fcf82c486782fb4bfb70268138026 [file] [log] [blame]
Auke Kok9d5c8242008-01-24 02:22:38 -08001/*******************************************************************************
2
3 Intel(R) Gigabit Ethernet Linux driver
Akeem G. Abodunrin4b9ea462013-01-08 18:31:12 +00004 Copyright(c) 2007-2013 Intel Corporation.
Auke Kok9d5c8242008-01-24 02:22:38 -08005
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21
22 Contact Information:
23 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25
26*******************************************************************************/
27
Jeff Kirsher876d2d62011-10-21 20:01:34 +000028#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29
Auke Kok9d5c8242008-01-24 02:22:38 -080030#include <linux/module.h>
31#include <linux/types.h>
32#include <linux/init.h>
Jiri Pirkob2cb09b2011-07-21 03:27:27 +000033#include <linux/bitops.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080034#include <linux/vmalloc.h>
35#include <linux/pagemap.h>
36#include <linux/netdevice.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080037#include <linux/ipv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080039#include <net/checksum.h>
40#include <net/ip6_checksum.h>
Patrick Ohlyc6cb0902009-02-12 05:03:42 +000041#include <linux/net_tstamp.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080042#include <linux/mii.h>
43#include <linux/ethtool.h>
Jiri Pirko01789342011-08-16 06:29:00 +000044#include <linux/if.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080045#include <linux/if_vlan.h>
46#include <linux/pci.h>
Alexander Duyckc54106b2008-10-16 21:26:57 -070047#include <linux/pci-aspm.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080048#include <linux/delay.h>
49#include <linux/interrupt.h>
Alexander Duyck7d13a7d2011-08-26 07:44:32 +000050#include <linux/ip.h>
51#include <linux/tcp.h>
52#include <linux/sctp.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080053#include <linux/if_ether.h>
Alexander Duyck40a914f2008-11-27 00:24:37 -080054#include <linux/aer.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040055#include <linux/prefetch.h>
Yan, Zheng749ab2c2012-01-04 20:23:37 +000056#include <linux/pm_runtime.h>
Jeff Kirsher421e02f2008-10-17 11:08:31 -070057#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -070058#include <linux/dca.h>
59#endif
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +000060#include <linux/i2c.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080061#include "igb.h"
62
Carolyn Wyborny200e5fd2012-05-31 23:39:30 +000063#define MAJ 4
Carolyn Wyborny66999382012-12-05 02:46:05 +000064#define MIN 1
65#define BUILD 2
Carolyn Wyborny0d1fe822011-03-11 20:58:19 -080066#define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) "." \
Carolyn Wyborny929dd042011-05-26 03:02:26 +000067__stringify(BUILD) "-k"
Auke Kok9d5c8242008-01-24 02:22:38 -080068char igb_driver_name[] = "igb";
69char igb_driver_version[] = DRV_VERSION;
70static const char igb_driver_string[] =
71 "Intel(R) Gigabit Ethernet Network Driver";
Akeem G. Abodunrin4b9ea462013-01-08 18:31:12 +000072static const char igb_copyright[] =
73 "Copyright (c) 2007-2013 Intel Corporation.";
Auke Kok9d5c8242008-01-24 02:22:38 -080074
Auke Kok9d5c8242008-01-24 02:22:38 -080075static const struct e1000_info *igb_info_tbl[] = {
76 [board_82575] = &e1000_82575_info,
77};
78
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000079static DEFINE_PCI_DEVICE_TABLE(igb_pci_tbl) = {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +000080 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I211_COPPER), board_82575 },
81 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER), board_82575 },
82 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_FIBER), board_82575 },
83 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES), board_82575 },
84 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SGMII), board_82575 },
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +000085 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_COPPER), board_82575 },
86 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_FIBER), board_82575 },
87 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SERDES), board_82575 },
88 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SGMII), board_82575 },
Alexander Duyck55cac242009-11-19 12:42:21 +000089 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER), board_82575 },
90 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_FIBER), board_82575 },
Carolyn Wyborny6493d242011-01-14 05:33:46 +000091 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_QUAD_FIBER), board_82575 },
Alexander Duyck55cac242009-11-19 12:42:21 +000092 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SERDES), board_82575 },
93 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SGMII), board_82575 },
94 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER_DUAL), board_82575 },
Joseph Gasparakis308fb392010-09-22 17:56:44 +000095 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SGMII), board_82575 },
96 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SERDES), board_82575 },
Gasparakis, Joseph1b5dda32010-12-09 01:41:01 +000097 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_BACKPLANE), board_82575 },
98 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SFP), board_82575 },
Alexander Duyck2d064c02008-07-08 15:10:12 -070099 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
Alexander Duyck9eb23412009-03-13 20:42:15 +0000100 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS), board_82575 },
Alexander Duyck747d49b2009-10-05 06:33:27 +0000101 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS_SERDES), board_82575 },
Alexander Duyck2d064c02008-07-08 15:10:12 -0700102 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
103 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
Alexander Duyck4703bf72009-07-23 18:09:48 +0000104 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES_QUAD), board_82575 },
Carolyn Wybornyb894fa22010-03-19 06:07:48 +0000105 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER_ET2), board_82575 },
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +0000106 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
Auke Kok9d5c8242008-01-24 02:22:38 -0800107 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
108 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
109 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
110 /* required last entry */
111 {0, }
112};
113
114MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
115
116void igb_reset(struct igb_adapter *);
117static int igb_setup_all_tx_resources(struct igb_adapter *);
118static int igb_setup_all_rx_resources(struct igb_adapter *);
119static void igb_free_all_tx_resources(struct igb_adapter *);
120static void igb_free_all_rx_resources(struct igb_adapter *);
Alexander Duyck06cf2662009-10-27 15:53:25 +0000121static void igb_setup_mrqc(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800122static int igb_probe(struct pci_dev *, const struct pci_device_id *);
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500123static void igb_remove(struct pci_dev *pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -0800124static int igb_sw_init(struct igb_adapter *);
125static int igb_open(struct net_device *);
126static int igb_close(struct net_device *);
Stefan Assmann53c7d062012-12-04 06:00:12 +0000127static void igb_configure(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800128static void igb_configure_tx(struct igb_adapter *);
129static void igb_configure_rx(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800130static void igb_clean_all_tx_rings(struct igb_adapter *);
131static void igb_clean_all_rx_rings(struct igb_adapter *);
Mitch Williams3b644cf2008-06-27 10:59:48 -0700132static void igb_clean_tx_ring(struct igb_ring *);
133static void igb_clean_rx_ring(struct igb_ring *);
Alexander Duyckff41f8d2009-09-03 14:48:56 +0000134static void igb_set_rx_mode(struct net_device *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800135static void igb_update_phy_info(unsigned long);
136static void igb_watchdog(unsigned long);
137static void igb_watchdog_task(struct work_struct *);
Alexander Duyckcd392f52011-08-26 07:43:59 +0000138static netdev_tx_t igb_xmit_frame(struct sk_buff *skb, struct net_device *);
Eric Dumazet12dcd862010-10-15 17:27:10 +0000139static struct rtnl_link_stats64 *igb_get_stats64(struct net_device *dev,
140 struct rtnl_link_stats64 *stats);
Auke Kok9d5c8242008-01-24 02:22:38 -0800141static int igb_change_mtu(struct net_device *, int);
142static int igb_set_mac(struct net_device *, void *);
Alexander Duyck68d480c2009-10-05 06:33:08 +0000143static void igb_set_uta(struct igb_adapter *adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800144static irqreturn_t igb_intr(int irq, void *);
145static irqreturn_t igb_intr_msi(int irq, void *);
146static irqreturn_t igb_msix_other(int irq, void *);
Alexander Duyck047e0032009-10-27 15:49:27 +0000147static irqreturn_t igb_msix_ring(int irq, void *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700148#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +0000149static void igb_update_dca(struct igb_q_vector *);
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700150static void igb_setup_dca(struct igb_adapter *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700151#endif /* CONFIG_IGB_DCA */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700152static int igb_poll(struct napi_struct *, int);
Alexander Duyck13fde972011-10-05 13:35:24 +0000153static bool igb_clean_tx_irq(struct igb_q_vector *);
Alexander Duyckcd392f52011-08-26 07:43:59 +0000154static bool igb_clean_rx_irq(struct igb_q_vector *, int);
Auke Kok9d5c8242008-01-24 02:22:38 -0800155static int igb_ioctl(struct net_device *, struct ifreq *, int cmd);
156static void igb_tx_timeout(struct net_device *);
157static void igb_reset_task(struct work_struct *);
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000158static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features);
Jiri Pirko8e586132011-12-08 19:52:37 -0500159static int igb_vlan_rx_add_vid(struct net_device *, u16);
160static int igb_vlan_rx_kill_vid(struct net_device *, u16);
Auke Kok9d5c8242008-01-24 02:22:38 -0800161static void igb_restore_vlan(struct igb_adapter *);
Alexander Duyck26ad9172009-10-05 06:32:49 +0000162static void igb_rar_set_qsel(struct igb_adapter *, u8 *, u32 , u8);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800163static void igb_ping_all_vfs(struct igb_adapter *);
164static void igb_msg_task(struct igb_adapter *);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800165static void igb_vmm_control(struct igb_adapter *);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +0000166static int igb_set_vf_mac(struct igb_adapter *, int, unsigned char *);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800167static void igb_restore_vf_multicasts(struct igb_adapter *adapter);
Williams, Mitch A8151d292010-02-10 01:44:24 +0000168static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac);
169static int igb_ndo_set_vf_vlan(struct net_device *netdev,
170 int vf, u16 vlan, u8 qos);
171static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate);
172static int igb_ndo_get_vf_config(struct net_device *netdev, int vf,
173 struct ifla_vf_info *ivi);
Lior Levy17dc5662011-02-08 02:28:46 +0000174static void igb_check_vf_rate_limit(struct igb_adapter *);
RongQing Li46a01692011-10-18 22:52:35 +0000175
176#ifdef CONFIG_PCI_IOV
Greg Rose0224d662011-10-14 02:57:14 +0000177static int igb_vf_configure(struct igb_adapter *adapter, int vf);
Stefan Assmannf5571472012-08-18 04:06:11 +0000178static bool igb_vfs_are_assigned(struct igb_adapter *adapter);
RongQing Li46a01692011-10-18 22:52:35 +0000179#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800180
Auke Kok9d5c8242008-01-24 02:22:38 -0800181#ifdef CONFIG_PM
Emil Tantilovd9dd9662012-01-28 08:10:35 +0000182#ifdef CONFIG_PM_SLEEP
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000183static int igb_suspend(struct device *);
Emil Tantilovd9dd9662012-01-28 08:10:35 +0000184#endif
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000185static int igb_resume(struct device *);
186#ifdef CONFIG_PM_RUNTIME
187static int igb_runtime_suspend(struct device *dev);
188static int igb_runtime_resume(struct device *dev);
189static int igb_runtime_idle(struct device *dev);
190#endif
191static const struct dev_pm_ops igb_pm_ops = {
192 SET_SYSTEM_SLEEP_PM_OPS(igb_suspend, igb_resume)
193 SET_RUNTIME_PM_OPS(igb_runtime_suspend, igb_runtime_resume,
194 igb_runtime_idle)
195};
Auke Kok9d5c8242008-01-24 02:22:38 -0800196#endif
197static void igb_shutdown(struct pci_dev *);
Greg Rosefa44f2f2013-01-17 01:03:06 -0800198static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700199#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700200static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
201static struct notifier_block dca_notifier = {
202 .notifier_call = igb_notify_dca,
203 .next = NULL,
204 .priority = 0
205};
206#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800207#ifdef CONFIG_NET_POLL_CONTROLLER
208/* for netdump / net console */
209static void igb_netpoll(struct net_device *);
210#endif
Alexander Duyck37680112009-02-19 20:40:30 -0800211#ifdef CONFIG_PCI_IOV
Alexander Duyck2a3abf62009-04-07 14:37:52 +0000212static unsigned int max_vfs = 0;
213module_param(max_vfs, uint, 0);
214MODULE_PARM_DESC(max_vfs, "Maximum number of virtual functions to allocate "
215 "per physical function");
216#endif /* CONFIG_PCI_IOV */
217
Auke Kok9d5c8242008-01-24 02:22:38 -0800218static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
219 pci_channel_state_t);
220static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
221static void igb_io_resume(struct pci_dev *);
222
Stephen Hemminger3646f0e2012-09-07 09:33:15 -0700223static const struct pci_error_handlers igb_err_handler = {
Auke Kok9d5c8242008-01-24 02:22:38 -0800224 .error_detected = igb_io_error_detected,
225 .slot_reset = igb_io_slot_reset,
226 .resume = igb_io_resume,
227};
228
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +0000229static void igb_init_dmac(struct igb_adapter *adapter, u32 pba);
Auke Kok9d5c8242008-01-24 02:22:38 -0800230
231static struct pci_driver igb_driver = {
232 .name = igb_driver_name,
233 .id_table = igb_pci_tbl,
234 .probe = igb_probe,
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500235 .remove = igb_remove,
Auke Kok9d5c8242008-01-24 02:22:38 -0800236#ifdef CONFIG_PM
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000237 .driver.pm = &igb_pm_ops,
Auke Kok9d5c8242008-01-24 02:22:38 -0800238#endif
239 .shutdown = igb_shutdown,
Greg Rosefa44f2f2013-01-17 01:03:06 -0800240 .sriov_configure = igb_pci_sriov_configure,
Auke Kok9d5c8242008-01-24 02:22:38 -0800241 .err_handler = &igb_err_handler
242};
243
244MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
245MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
246MODULE_LICENSE("GPL");
247MODULE_VERSION(DRV_VERSION);
248
stephen hemmingerb3f4d592012-03-13 06:04:20 +0000249#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
250static int debug = -1;
251module_param(debug, int, 0);
252MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
253
Taku Izumic97ec422010-04-27 14:39:30 +0000254struct igb_reg_info {
255 u32 ofs;
256 char *name;
257};
258
259static const struct igb_reg_info igb_reg_info_tbl[] = {
260
261 /* General Registers */
262 {E1000_CTRL, "CTRL"},
263 {E1000_STATUS, "STATUS"},
264 {E1000_CTRL_EXT, "CTRL_EXT"},
265
266 /* Interrupt Registers */
267 {E1000_ICR, "ICR"},
268
269 /* RX Registers */
270 {E1000_RCTL, "RCTL"},
271 {E1000_RDLEN(0), "RDLEN"},
272 {E1000_RDH(0), "RDH"},
273 {E1000_RDT(0), "RDT"},
274 {E1000_RXDCTL(0), "RXDCTL"},
275 {E1000_RDBAL(0), "RDBAL"},
276 {E1000_RDBAH(0), "RDBAH"},
277
278 /* TX Registers */
279 {E1000_TCTL, "TCTL"},
280 {E1000_TDBAL(0), "TDBAL"},
281 {E1000_TDBAH(0), "TDBAH"},
282 {E1000_TDLEN(0), "TDLEN"},
283 {E1000_TDH(0), "TDH"},
284 {E1000_TDT(0), "TDT"},
285 {E1000_TXDCTL(0), "TXDCTL"},
286 {E1000_TDFH, "TDFH"},
287 {E1000_TDFT, "TDFT"},
288 {E1000_TDFHS, "TDFHS"},
289 {E1000_TDFPC, "TDFPC"},
290
291 /* List Terminator */
292 {}
293};
294
295/*
296 * igb_regdump - register printout routine
297 */
298static void igb_regdump(struct e1000_hw *hw, struct igb_reg_info *reginfo)
299{
300 int n = 0;
301 char rname[16];
302 u32 regs[8];
303
304 switch (reginfo->ofs) {
305 case E1000_RDLEN(0):
306 for (n = 0; n < 4; n++)
307 regs[n] = rd32(E1000_RDLEN(n));
308 break;
309 case E1000_RDH(0):
310 for (n = 0; n < 4; n++)
311 regs[n] = rd32(E1000_RDH(n));
312 break;
313 case E1000_RDT(0):
314 for (n = 0; n < 4; n++)
315 regs[n] = rd32(E1000_RDT(n));
316 break;
317 case E1000_RXDCTL(0):
318 for (n = 0; n < 4; n++)
319 regs[n] = rd32(E1000_RXDCTL(n));
320 break;
321 case E1000_RDBAL(0):
322 for (n = 0; n < 4; n++)
323 regs[n] = rd32(E1000_RDBAL(n));
324 break;
325 case E1000_RDBAH(0):
326 for (n = 0; n < 4; n++)
327 regs[n] = rd32(E1000_RDBAH(n));
328 break;
329 case E1000_TDBAL(0):
330 for (n = 0; n < 4; n++)
331 regs[n] = rd32(E1000_RDBAL(n));
332 break;
333 case E1000_TDBAH(0):
334 for (n = 0; n < 4; n++)
335 regs[n] = rd32(E1000_TDBAH(n));
336 break;
337 case E1000_TDLEN(0):
338 for (n = 0; n < 4; n++)
339 regs[n] = rd32(E1000_TDLEN(n));
340 break;
341 case E1000_TDH(0):
342 for (n = 0; n < 4; n++)
343 regs[n] = rd32(E1000_TDH(n));
344 break;
345 case E1000_TDT(0):
346 for (n = 0; n < 4; n++)
347 regs[n] = rd32(E1000_TDT(n));
348 break;
349 case E1000_TXDCTL(0):
350 for (n = 0; n < 4; n++)
351 regs[n] = rd32(E1000_TXDCTL(n));
352 break;
353 default:
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000354 pr_info("%-15s %08x\n", reginfo->name, rd32(reginfo->ofs));
Taku Izumic97ec422010-04-27 14:39:30 +0000355 return;
356 }
357
358 snprintf(rname, 16, "%s%s", reginfo->name, "[0-3]");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000359 pr_info("%-15s %08x %08x %08x %08x\n", rname, regs[0], regs[1],
360 regs[2], regs[3]);
Taku Izumic97ec422010-04-27 14:39:30 +0000361}
362
363/*
364 * igb_dump - Print registers, tx-rings and rx-rings
365 */
366static void igb_dump(struct igb_adapter *adapter)
367{
368 struct net_device *netdev = adapter->netdev;
369 struct e1000_hw *hw = &adapter->hw;
370 struct igb_reg_info *reginfo;
Taku Izumic97ec422010-04-27 14:39:30 +0000371 struct igb_ring *tx_ring;
372 union e1000_adv_tx_desc *tx_desc;
373 struct my_u0 { u64 a; u64 b; } *u0;
Taku Izumic97ec422010-04-27 14:39:30 +0000374 struct igb_ring *rx_ring;
375 union e1000_adv_rx_desc *rx_desc;
376 u32 staterr;
Alexander Duyck6ad4edf2011-08-26 07:45:26 +0000377 u16 i, n;
Taku Izumic97ec422010-04-27 14:39:30 +0000378
379 if (!netif_msg_hw(adapter))
380 return;
381
382 /* Print netdevice Info */
383 if (netdev) {
384 dev_info(&adapter->pdev->dev, "Net device Info\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000385 pr_info("Device Name state trans_start "
386 "last_rx\n");
387 pr_info("%-15s %016lX %016lX %016lX\n", netdev->name,
388 netdev->state, netdev->trans_start, netdev->last_rx);
Taku Izumic97ec422010-04-27 14:39:30 +0000389 }
390
391 /* Print Registers */
392 dev_info(&adapter->pdev->dev, "Register Dump\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000393 pr_info(" Register Name Value\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000394 for (reginfo = (struct igb_reg_info *)igb_reg_info_tbl;
395 reginfo->name; reginfo++) {
396 igb_regdump(hw, reginfo);
397 }
398
399 /* Print TX Ring Summary */
400 if (!netdev || !netif_running(netdev))
401 goto exit;
402
403 dev_info(&adapter->pdev->dev, "TX Rings Summary\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000404 pr_info("Queue [NTU] [NTC] [bi(ntc)->dma ] leng ntw timestamp\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000405 for (n = 0; n < adapter->num_tx_queues; n++) {
Alexander Duyck06034642011-08-26 07:44:22 +0000406 struct igb_tx_buffer *buffer_info;
Taku Izumic97ec422010-04-27 14:39:30 +0000407 tx_ring = adapter->tx_ring[n];
Alexander Duyck06034642011-08-26 07:44:22 +0000408 buffer_info = &tx_ring->tx_buffer_info[tx_ring->next_to_clean];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000409 pr_info(" %5d %5X %5X %016llX %04X %p %016llX\n",
410 n, tx_ring->next_to_use, tx_ring->next_to_clean,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000411 (u64)dma_unmap_addr(buffer_info, dma),
412 dma_unmap_len(buffer_info, len),
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000413 buffer_info->next_to_watch,
414 (u64)buffer_info->time_stamp);
Taku Izumic97ec422010-04-27 14:39:30 +0000415 }
416
417 /* Print TX Rings */
418 if (!netif_msg_tx_done(adapter))
419 goto rx_ring_summary;
420
421 dev_info(&adapter->pdev->dev, "TX Rings Dump\n");
422
423 /* Transmit Descriptor Formats
424 *
425 * Advanced Transmit Descriptor
426 * +--------------------------------------------------------------+
427 * 0 | Buffer Address [63:0] |
428 * +--------------------------------------------------------------+
429 * 8 | PAYLEN | PORTS |CC|IDX | STA | DCMD |DTYP|MAC|RSV| DTALEN |
430 * +--------------------------------------------------------------+
431 * 63 46 45 40 39 38 36 35 32 31 24 15 0
432 */
433
434 for (n = 0; n < adapter->num_tx_queues; n++) {
435 tx_ring = adapter->tx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000436 pr_info("------------------------------------\n");
437 pr_info("TX QUEUE INDEX = %d\n", tx_ring->queue_index);
438 pr_info("------------------------------------\n");
439 pr_info("T [desc] [address 63:0 ] [PlPOCIStDDM Ln] "
440 "[bi->dma ] leng ntw timestamp "
441 "bi->skb\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000442
443 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000444 const char *next_desc;
Alexander Duyck06034642011-08-26 07:44:22 +0000445 struct igb_tx_buffer *buffer_info;
Alexander Duyck601369062011-08-26 07:44:05 +0000446 tx_desc = IGB_TX_DESC(tx_ring, i);
Alexander Duyck06034642011-08-26 07:44:22 +0000447 buffer_info = &tx_ring->tx_buffer_info[i];
Taku Izumic97ec422010-04-27 14:39:30 +0000448 u0 = (struct my_u0 *)tx_desc;
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000449 if (i == tx_ring->next_to_use &&
450 i == tx_ring->next_to_clean)
451 next_desc = " NTC/U";
452 else if (i == tx_ring->next_to_use)
453 next_desc = " NTU";
454 else if (i == tx_ring->next_to_clean)
455 next_desc = " NTC";
456 else
457 next_desc = "";
458
459 pr_info("T [0x%03X] %016llX %016llX %016llX"
460 " %04X %p %016llX %p%s\n", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000461 le64_to_cpu(u0->a),
462 le64_to_cpu(u0->b),
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000463 (u64)dma_unmap_addr(buffer_info, dma),
464 dma_unmap_len(buffer_info, len),
Taku Izumic97ec422010-04-27 14:39:30 +0000465 buffer_info->next_to_watch,
466 (u64)buffer_info->time_stamp,
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000467 buffer_info->skb, next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000468
Emil Tantilovb6695882012-07-28 05:07:48 +0000469 if (netif_msg_pktdata(adapter) && buffer_info->skb)
Taku Izumic97ec422010-04-27 14:39:30 +0000470 print_hex_dump(KERN_INFO, "",
471 DUMP_PREFIX_ADDRESS,
Emil Tantilovb6695882012-07-28 05:07:48 +0000472 16, 1, buffer_info->skb->data,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000473 dma_unmap_len(buffer_info, len),
474 true);
Taku Izumic97ec422010-04-27 14:39:30 +0000475 }
476 }
477
478 /* Print RX Rings Summary */
479rx_ring_summary:
480 dev_info(&adapter->pdev->dev, "RX Rings Summary\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000481 pr_info("Queue [NTU] [NTC]\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000482 for (n = 0; n < adapter->num_rx_queues; n++) {
483 rx_ring = adapter->rx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000484 pr_info(" %5d %5X %5X\n",
485 n, rx_ring->next_to_use, rx_ring->next_to_clean);
Taku Izumic97ec422010-04-27 14:39:30 +0000486 }
487
488 /* Print RX Rings */
489 if (!netif_msg_rx_status(adapter))
490 goto exit;
491
492 dev_info(&adapter->pdev->dev, "RX Rings Dump\n");
493
494 /* Advanced Receive Descriptor (Read) Format
495 * 63 1 0
496 * +-----------------------------------------------------+
497 * 0 | Packet Buffer Address [63:1] |A0/NSE|
498 * +----------------------------------------------+------+
499 * 8 | Header Buffer Address [63:1] | DD |
500 * +-----------------------------------------------------+
501 *
502 *
503 * Advanced Receive Descriptor (Write-Back) Format
504 *
505 * 63 48 47 32 31 30 21 20 17 16 4 3 0
506 * +------------------------------------------------------+
507 * 0 | Packet IP |SPH| HDR_LEN | RSV|Packet| RSS |
508 * | Checksum Ident | | | | Type | Type |
509 * +------------------------------------------------------+
510 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
511 * +------------------------------------------------------+
512 * 63 48 47 32 31 20 19 0
513 */
514
515 for (n = 0; n < adapter->num_rx_queues; n++) {
516 rx_ring = adapter->rx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000517 pr_info("------------------------------------\n");
518 pr_info("RX QUEUE INDEX = %d\n", rx_ring->queue_index);
519 pr_info("------------------------------------\n");
520 pr_info("R [desc] [ PktBuf A0] [ HeadBuf DD] "
521 "[bi->dma ] [bi->skb] <-- Adv Rx Read format\n");
522 pr_info("RWB[desc] [PcsmIpSHl PtRs] [vl er S cks ln] -----"
523 "----------- [bi->skb] <-- Adv Rx Write-Back format\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000524
525 for (i = 0; i < rx_ring->count; i++) {
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000526 const char *next_desc;
Alexander Duyck06034642011-08-26 07:44:22 +0000527 struct igb_rx_buffer *buffer_info;
528 buffer_info = &rx_ring->rx_buffer_info[i];
Alexander Duyck601369062011-08-26 07:44:05 +0000529 rx_desc = IGB_RX_DESC(rx_ring, i);
Taku Izumic97ec422010-04-27 14:39:30 +0000530 u0 = (struct my_u0 *)rx_desc;
531 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000532
533 if (i == rx_ring->next_to_use)
534 next_desc = " NTU";
535 else if (i == rx_ring->next_to_clean)
536 next_desc = " NTC";
537 else
538 next_desc = "";
539
Taku Izumic97ec422010-04-27 14:39:30 +0000540 if (staterr & E1000_RXD_STAT_DD) {
541 /* Descriptor Done */
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000542 pr_info("%s[0x%03X] %016llX %016llX ---------------- %s\n",
543 "RWB", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000544 le64_to_cpu(u0->a),
545 le64_to_cpu(u0->b),
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000546 next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000547 } else {
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000548 pr_info("%s[0x%03X] %016llX %016llX %016llX %s\n",
549 "R ", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000550 le64_to_cpu(u0->a),
551 le64_to_cpu(u0->b),
552 (u64)buffer_info->dma,
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000553 next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000554
Emil Tantilovb6695882012-07-28 05:07:48 +0000555 if (netif_msg_pktdata(adapter) &&
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000556 buffer_info->dma && buffer_info->page) {
Alexander Duyck44390ca2011-08-26 07:43:38 +0000557 print_hex_dump(KERN_INFO, "",
558 DUMP_PREFIX_ADDRESS,
559 16, 1,
Emil Tantilovb6695882012-07-28 05:07:48 +0000560 page_address(buffer_info->page) +
561 buffer_info->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +0000562 IGB_RX_BUFSZ, true);
Taku Izumic97ec422010-04-27 14:39:30 +0000563 }
564 }
Taku Izumic97ec422010-04-27 14:39:30 +0000565 }
566 }
567
568exit:
569 return;
570}
571
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +0000572/* igb_get_i2c_data - Reads the I2C SDA data bit
573 * @hw: pointer to hardware structure
574 * @i2cctl: Current value of I2CCTL register
575 *
576 * Returns the I2C data bit value
577 */
578static int igb_get_i2c_data(void *data)
579{
580 struct igb_adapter *adapter = (struct igb_adapter *)data;
581 struct e1000_hw *hw = &adapter->hw;
582 s32 i2cctl = rd32(E1000_I2CPARAMS);
583
584 return ((i2cctl & E1000_I2C_DATA_IN) != 0);
585}
586
587/* igb_set_i2c_data - Sets the I2C data bit
588 * @data: pointer to hardware structure
589 * @state: I2C data value (0 or 1) to set
590 *
591 * Sets the I2C data bit
592 */
593static void igb_set_i2c_data(void *data, int state)
594{
595 struct igb_adapter *adapter = (struct igb_adapter *)data;
596 struct e1000_hw *hw = &adapter->hw;
597 s32 i2cctl = rd32(E1000_I2CPARAMS);
598
599 if (state)
600 i2cctl |= E1000_I2C_DATA_OUT;
601 else
602 i2cctl &= ~E1000_I2C_DATA_OUT;
603
604 i2cctl &= ~E1000_I2C_DATA_OE_N;
605 i2cctl |= E1000_I2C_CLK_OE_N;
606 wr32(E1000_I2CPARAMS, i2cctl);
607 wrfl();
608
609}
610
611/* igb_set_i2c_clk - Sets the I2C SCL clock
612 * @data: pointer to hardware structure
613 * @state: state to set clock
614 *
615 * Sets the I2C clock line to state
616 */
617static void igb_set_i2c_clk(void *data, int state)
618{
619 struct igb_adapter *adapter = (struct igb_adapter *)data;
620 struct e1000_hw *hw = &adapter->hw;
621 s32 i2cctl = rd32(E1000_I2CPARAMS);
622
623 if (state) {
624 i2cctl |= E1000_I2C_CLK_OUT;
625 i2cctl &= ~E1000_I2C_CLK_OE_N;
626 } else {
627 i2cctl &= ~E1000_I2C_CLK_OUT;
628 i2cctl &= ~E1000_I2C_CLK_OE_N;
629 }
630 wr32(E1000_I2CPARAMS, i2cctl);
631 wrfl();
632}
633
634/* igb_get_i2c_clk - Gets the I2C SCL clock state
635 * @data: pointer to hardware structure
636 *
637 * Gets the I2C clock state
638 */
639static int igb_get_i2c_clk(void *data)
640{
641 struct igb_adapter *adapter = (struct igb_adapter *)data;
642 struct e1000_hw *hw = &adapter->hw;
643 s32 i2cctl = rd32(E1000_I2CPARAMS);
644
645 return ((i2cctl & E1000_I2C_CLK_IN) != 0);
646}
647
648static const struct i2c_algo_bit_data igb_i2c_algo = {
649 .setsda = igb_set_i2c_data,
650 .setscl = igb_set_i2c_clk,
651 .getsda = igb_get_i2c_data,
652 .getscl = igb_get_i2c_clk,
653 .udelay = 5,
654 .timeout = 20,
655};
656
Auke Kok9d5c8242008-01-24 02:22:38 -0800657/**
Alexander Duyckc0410762010-03-25 13:10:08 +0000658 * igb_get_hw_dev - return device
Auke Kok9d5c8242008-01-24 02:22:38 -0800659 * used by hardware layer to print debugging information
660 **/
Alexander Duyckc0410762010-03-25 13:10:08 +0000661struct net_device *igb_get_hw_dev(struct e1000_hw *hw)
Auke Kok9d5c8242008-01-24 02:22:38 -0800662{
663 struct igb_adapter *adapter = hw->back;
Alexander Duyckc0410762010-03-25 13:10:08 +0000664 return adapter->netdev;
Auke Kok9d5c8242008-01-24 02:22:38 -0800665}
Patrick Ohly38c845c2009-02-12 05:03:41 +0000666
667/**
Auke Kok9d5c8242008-01-24 02:22:38 -0800668 * igb_init_module - Driver Registration Routine
669 *
670 * igb_init_module is the first routine called when the driver is
671 * loaded. All it does is register with the PCI subsystem.
672 **/
673static int __init igb_init_module(void)
674{
675 int ret;
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000676 pr_info("%s - version %s\n",
Auke Kok9d5c8242008-01-24 02:22:38 -0800677 igb_driver_string, igb_driver_version);
678
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000679 pr_info("%s\n", igb_copyright);
Auke Kok9d5c8242008-01-24 02:22:38 -0800680
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700681#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700682 dca_register_notify(&dca_notifier);
683#endif
Alexander Duyckbbd98fe2009-01-31 00:52:30 -0800684 ret = pci_register_driver(&igb_driver);
Auke Kok9d5c8242008-01-24 02:22:38 -0800685 return ret;
686}
687
688module_init(igb_init_module);
689
690/**
691 * igb_exit_module - Driver Exit Cleanup Routine
692 *
693 * igb_exit_module is called just before the driver is removed
694 * from memory.
695 **/
696static void __exit igb_exit_module(void)
697{
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700698#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700699 dca_unregister_notify(&dca_notifier);
700#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800701 pci_unregister_driver(&igb_driver);
702}
703
704module_exit(igb_exit_module);
705
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800706#define Q_IDX_82576(i) (((i & 0x1) << 3) + (i >> 1))
707/**
708 * igb_cache_ring_register - Descriptor ring to register mapping
709 * @adapter: board private structure to initialize
710 *
711 * Once we know the feature-set enabled for the device, we'll cache
712 * the register offset the descriptor ring is assigned to.
713 **/
714static void igb_cache_ring_register(struct igb_adapter *adapter)
715{
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000716 int i = 0, j = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000717 u32 rbase_offset = adapter->vfs_allocated_count;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800718
719 switch (adapter->hw.mac.type) {
720 case e1000_82576:
721 /* The queues are allocated for virtualization such that VF 0
722 * is allocated queues 0 and 8, VF 1 queues 1 and 9, etc.
723 * In order to avoid collision we start at the first free queue
724 * and continue consuming queues in the same sequence
725 */
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000726 if (adapter->vfs_allocated_count) {
Alexander Duycka99955f2009-11-12 18:37:19 +0000727 for (; i < adapter->rss_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000728 adapter->rx_ring[i]->reg_idx = rbase_offset +
729 Q_IDX_82576(i);
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000730 }
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800731 case e1000_82575:
Alexander Duyck55cac242009-11-19 12:42:21 +0000732 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000733 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000734 case e1000_i210:
735 case e1000_i211:
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800736 default:
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000737 for (; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000738 adapter->rx_ring[i]->reg_idx = rbase_offset + i;
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000739 for (; j < adapter->num_tx_queues; j++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000740 adapter->tx_ring[j]->reg_idx = rbase_offset + j;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800741 break;
742 }
743}
744
Alexander Duyck4be000c2011-08-26 07:45:52 +0000745/**
746 * igb_write_ivar - configure ivar for given MSI-X vector
747 * @hw: pointer to the HW structure
748 * @msix_vector: vector number we are allocating to a given ring
749 * @index: row index of IVAR register to write within IVAR table
750 * @offset: column offset of in IVAR, should be multiple of 8
751 *
752 * This function is intended to handle the writing of the IVAR register
753 * for adapters 82576 and newer. The IVAR table consists of 2 columns,
754 * each containing an cause allocation for an Rx and Tx ring, and a
755 * variable number of rows depending on the number of queues supported.
756 **/
757static void igb_write_ivar(struct e1000_hw *hw, int msix_vector,
758 int index, int offset)
759{
760 u32 ivar = array_rd32(E1000_IVAR0, index);
761
762 /* clear any bits that are currently set */
763 ivar &= ~((u32)0xFF << offset);
764
765 /* write vector and valid bit */
766 ivar |= (msix_vector | E1000_IVAR_VALID) << offset;
767
768 array_wr32(E1000_IVAR0, index, ivar);
769}
770
Auke Kok9d5c8242008-01-24 02:22:38 -0800771#define IGB_N0_QUEUE -1
Alexander Duyck047e0032009-10-27 15:49:27 +0000772static void igb_assign_vector(struct igb_q_vector *q_vector, int msix_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -0800773{
Alexander Duyck047e0032009-10-27 15:49:27 +0000774 struct igb_adapter *adapter = q_vector->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -0800775 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck047e0032009-10-27 15:49:27 +0000776 int rx_queue = IGB_N0_QUEUE;
777 int tx_queue = IGB_N0_QUEUE;
Alexander Duyck4be000c2011-08-26 07:45:52 +0000778 u32 msixbm = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000779
Alexander Duyck0ba82992011-08-26 07:45:47 +0000780 if (q_vector->rx.ring)
781 rx_queue = q_vector->rx.ring->reg_idx;
782 if (q_vector->tx.ring)
783 tx_queue = q_vector->tx.ring->reg_idx;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700784
785 switch (hw->mac.type) {
786 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800787 /* The 82575 assigns vectors using a bitmask, which matches the
788 bitmask for the EICR/EIMS/EIMC registers. To assign one
789 or more queues to a vector, we write the appropriate bits
790 into the MSIXBM register for that vector. */
Alexander Duyck047e0032009-10-27 15:49:27 +0000791 if (rx_queue > IGB_N0_QUEUE)
Auke Kok9d5c8242008-01-24 02:22:38 -0800792 msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
Alexander Duyck047e0032009-10-27 15:49:27 +0000793 if (tx_queue > IGB_N0_QUEUE)
Auke Kok9d5c8242008-01-24 02:22:38 -0800794 msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
Alexander Duyckfeeb2722010-02-03 21:59:51 +0000795 if (!adapter->msix_entries && msix_vector == 0)
796 msixbm |= E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800797 array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
Alexander Duyck047e0032009-10-27 15:49:27 +0000798 q_vector->eims_value = msixbm;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700799 break;
800 case e1000_82576:
Alexander Duyck4be000c2011-08-26 07:45:52 +0000801 /*
802 * 82576 uses a table that essentially consists of 2 columns
803 * with 8 rows. The ordering is column-major so we use the
804 * lower 3 bits as the row index, and the 4th bit as the
805 * column offset.
806 */
807 if (rx_queue > IGB_N0_QUEUE)
808 igb_write_ivar(hw, msix_vector,
809 rx_queue & 0x7,
810 (rx_queue & 0x8) << 1);
811 if (tx_queue > IGB_N0_QUEUE)
812 igb_write_ivar(hw, msix_vector,
813 tx_queue & 0x7,
814 ((tx_queue & 0x8) << 1) + 8);
Alexander Duyck047e0032009-10-27 15:49:27 +0000815 q_vector->eims_value = 1 << msix_vector;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700816 break;
Alexander Duyck55cac242009-11-19 12:42:21 +0000817 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000818 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000819 case e1000_i210:
820 case e1000_i211:
Alexander Duyck4be000c2011-08-26 07:45:52 +0000821 /*
822 * On 82580 and newer adapters the scheme is similar to 82576
823 * however instead of ordering column-major we have things
824 * ordered row-major. So we traverse the table by using
825 * bit 0 as the column offset, and the remaining bits as the
826 * row index.
827 */
828 if (rx_queue > IGB_N0_QUEUE)
829 igb_write_ivar(hw, msix_vector,
830 rx_queue >> 1,
831 (rx_queue & 0x1) << 4);
832 if (tx_queue > IGB_N0_QUEUE)
833 igb_write_ivar(hw, msix_vector,
834 tx_queue >> 1,
835 ((tx_queue & 0x1) << 4) + 8);
Alexander Duyck55cac242009-11-19 12:42:21 +0000836 q_vector->eims_value = 1 << msix_vector;
837 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700838 default:
839 BUG();
840 break;
841 }
Alexander Duyck26b39272010-02-17 01:00:41 +0000842
843 /* add q_vector eims value to global eims_enable_mask */
844 adapter->eims_enable_mask |= q_vector->eims_value;
845
846 /* configure q_vector to set itr on first interrupt */
847 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -0800848}
849
850/**
851 * igb_configure_msix - Configure MSI-X hardware
852 *
853 * igb_configure_msix sets up the hardware to properly
854 * generate MSI-X interrupts.
855 **/
856static void igb_configure_msix(struct igb_adapter *adapter)
857{
858 u32 tmp;
859 int i, vector = 0;
860 struct e1000_hw *hw = &adapter->hw;
861
862 adapter->eims_enable_mask = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -0800863
864 /* set vector for other causes, i.e. link changes */
Alexander Duyck2d064c02008-07-08 15:10:12 -0700865 switch (hw->mac.type) {
866 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800867 tmp = rd32(E1000_CTRL_EXT);
868 /* enable MSI-X PBA support*/
869 tmp |= E1000_CTRL_EXT_PBA_CLR;
870
871 /* Auto-Mask interrupts upon ICR read. */
872 tmp |= E1000_CTRL_EXT_EIAME;
873 tmp |= E1000_CTRL_EXT_IRCA;
874
875 wr32(E1000_CTRL_EXT, tmp);
Alexander Duyck047e0032009-10-27 15:49:27 +0000876
877 /* enable msix_other interrupt */
878 array_wr32(E1000_MSIXBM(0), vector++,
879 E1000_EIMS_OTHER);
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700880 adapter->eims_other = E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800881
Alexander Duyck2d064c02008-07-08 15:10:12 -0700882 break;
883
884 case e1000_82576:
Alexander Duyck55cac242009-11-19 12:42:21 +0000885 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000886 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000887 case e1000_i210:
888 case e1000_i211:
Alexander Duyck047e0032009-10-27 15:49:27 +0000889 /* Turn on MSI-X capability first, or our settings
890 * won't stick. And it will take days to debug. */
891 wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
892 E1000_GPIE_PBA | E1000_GPIE_EIAME |
893 E1000_GPIE_NSICR);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700894
Alexander Duyck047e0032009-10-27 15:49:27 +0000895 /* enable msix_other interrupt */
896 adapter->eims_other = 1 << vector;
897 tmp = (vector++ | E1000_IVAR_VALID) << 8;
898
899 wr32(E1000_IVAR_MISC, tmp);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700900 break;
901 default:
902 /* do nothing, since nothing else supports MSI-X */
903 break;
904 } /* switch (hw->mac.type) */
Alexander Duyck047e0032009-10-27 15:49:27 +0000905
906 adapter->eims_enable_mask |= adapter->eims_other;
907
Alexander Duyck26b39272010-02-17 01:00:41 +0000908 for (i = 0; i < adapter->num_q_vectors; i++)
909 igb_assign_vector(adapter->q_vector[i], vector++);
Alexander Duyck047e0032009-10-27 15:49:27 +0000910
Auke Kok9d5c8242008-01-24 02:22:38 -0800911 wrfl();
912}
913
914/**
915 * igb_request_msix - Initialize MSI-X interrupts
916 *
917 * igb_request_msix allocates MSI-X vectors and requests interrupts from the
918 * kernel.
919 **/
920static int igb_request_msix(struct igb_adapter *adapter)
921{
922 struct net_device *netdev = adapter->netdev;
Alexander Duyck047e0032009-10-27 15:49:27 +0000923 struct e1000_hw *hw = &adapter->hw;
Stefan Assmann52285b72012-12-04 06:00:17 +0000924 int i, err = 0, vector = 0, free_vector = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -0800925
Auke Kok9d5c8242008-01-24 02:22:38 -0800926 err = request_irq(adapter->msix_entries[vector].vector,
Joe Perchesa0607fd2009-11-18 23:29:17 -0800927 igb_msix_other, 0, netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800928 if (err)
Stefan Assmann52285b72012-12-04 06:00:17 +0000929 goto err_out;
Alexander Duyck047e0032009-10-27 15:49:27 +0000930
931 for (i = 0; i < adapter->num_q_vectors; i++) {
932 struct igb_q_vector *q_vector = adapter->q_vector[i];
933
Stefan Assmann52285b72012-12-04 06:00:17 +0000934 vector++;
935
Alexander Duyck047e0032009-10-27 15:49:27 +0000936 q_vector->itr_register = hw->hw_addr + E1000_EITR(vector);
937
Alexander Duyck0ba82992011-08-26 07:45:47 +0000938 if (q_vector->rx.ring && q_vector->tx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000939 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000940 q_vector->rx.ring->queue_index);
941 else if (q_vector->tx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000942 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000943 q_vector->tx.ring->queue_index);
944 else if (q_vector->rx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000945 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000946 q_vector->rx.ring->queue_index);
Alexander Duyck047e0032009-10-27 15:49:27 +0000947 else
948 sprintf(q_vector->name, "%s-unused", netdev->name);
949
950 err = request_irq(adapter->msix_entries[vector].vector,
Joe Perchesa0607fd2009-11-18 23:29:17 -0800951 igb_msix_ring, 0, q_vector->name,
Alexander Duyck047e0032009-10-27 15:49:27 +0000952 q_vector);
953 if (err)
Stefan Assmann52285b72012-12-04 06:00:17 +0000954 goto err_free;
Alexander Duyck047e0032009-10-27 15:49:27 +0000955 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800956
Auke Kok9d5c8242008-01-24 02:22:38 -0800957 igb_configure_msix(adapter);
958 return 0;
Stefan Assmann52285b72012-12-04 06:00:17 +0000959
960err_free:
961 /* free already assigned IRQs */
962 free_irq(adapter->msix_entries[free_vector++].vector, adapter);
963
964 vector--;
965 for (i = 0; i < vector; i++) {
966 free_irq(adapter->msix_entries[free_vector++].vector,
967 adapter->q_vector[i]);
968 }
969err_out:
Auke Kok9d5c8242008-01-24 02:22:38 -0800970 return err;
971}
972
973static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
974{
975 if (adapter->msix_entries) {
976 pci_disable_msix(adapter->pdev);
977 kfree(adapter->msix_entries);
978 adapter->msix_entries = NULL;
Alexander Duyck047e0032009-10-27 15:49:27 +0000979 } else if (adapter->flags & IGB_FLAG_HAS_MSI) {
Auke Kok9d5c8242008-01-24 02:22:38 -0800980 pci_disable_msi(adapter->pdev);
Alexander Duyck047e0032009-10-27 15:49:27 +0000981 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800982}
983
Alexander Duyck047e0032009-10-27 15:49:27 +0000984/**
Alexander Duyck5536d212012-09-25 00:31:17 +0000985 * igb_free_q_vector - Free memory allocated for specific interrupt vector
986 * @adapter: board private structure to initialize
987 * @v_idx: Index of vector to be freed
988 *
989 * This function frees the memory allocated to the q_vector. In addition if
990 * NAPI is enabled it will delete any references to the NAPI struct prior
991 * to freeing the q_vector.
992 **/
993static void igb_free_q_vector(struct igb_adapter *adapter, int v_idx)
994{
995 struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
996
997 if (q_vector->tx.ring)
998 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
999
1000 if (q_vector->rx.ring)
1001 adapter->tx_ring[q_vector->rx.ring->queue_index] = NULL;
1002
1003 adapter->q_vector[v_idx] = NULL;
1004 netif_napi_del(&q_vector->napi);
1005
1006 /*
1007 * ixgbe_get_stats64() might access the rings on this vector,
1008 * we must wait a grace period before freeing it.
1009 */
1010 kfree_rcu(q_vector, rcu);
1011}
1012
1013/**
Alexander Duyck047e0032009-10-27 15:49:27 +00001014 * igb_free_q_vectors - Free memory allocated for interrupt vectors
1015 * @adapter: board private structure to initialize
1016 *
1017 * This function frees the memory allocated to the q_vectors. In addition if
1018 * NAPI is enabled it will delete any references to the NAPI struct prior
1019 * to freeing the q_vector.
1020 **/
1021static void igb_free_q_vectors(struct igb_adapter *adapter)
1022{
Alexander Duyck5536d212012-09-25 00:31:17 +00001023 int v_idx = adapter->num_q_vectors;
Alexander Duyck047e0032009-10-27 15:49:27 +00001024
Alexander Duyck5536d212012-09-25 00:31:17 +00001025 adapter->num_tx_queues = 0;
1026 adapter->num_rx_queues = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +00001027 adapter->num_q_vectors = 0;
Alexander Duyck5536d212012-09-25 00:31:17 +00001028
1029 while (v_idx--)
1030 igb_free_q_vector(adapter, v_idx);
Alexander Duyck047e0032009-10-27 15:49:27 +00001031}
1032
1033/**
1034 * igb_clear_interrupt_scheme - reset the device to a state of no interrupts
1035 *
1036 * This function resets the device so that it has 0 rx queues, tx queues, and
1037 * MSI-X interrupts allocated.
1038 */
1039static void igb_clear_interrupt_scheme(struct igb_adapter *adapter)
1040{
Alexander Duyck047e0032009-10-27 15:49:27 +00001041 igb_free_q_vectors(adapter);
1042 igb_reset_interrupt_capability(adapter);
1043}
Auke Kok9d5c8242008-01-24 02:22:38 -08001044
1045/**
1046 * igb_set_interrupt_capability - set MSI or MSI-X if supported
1047 *
1048 * Attempt to configure interrupts using the best available
1049 * capabilities of the hardware and kernel.
1050 **/
Stefan Assmann53c7d062012-12-04 06:00:12 +00001051static void igb_set_interrupt_capability(struct igb_adapter *adapter, bool msix)
Auke Kok9d5c8242008-01-24 02:22:38 -08001052{
1053 int err;
1054 int numvecs, i;
1055
Stefan Assmann53c7d062012-12-04 06:00:12 +00001056 if (!msix)
1057 goto msi_only;
1058
Alexander Duyck83b71802009-02-06 23:15:45 +00001059 /* Number of supported queues. */
Alexander Duycka99955f2009-11-12 18:37:19 +00001060 adapter->num_rx_queues = adapter->rss_queues;
Greg Rose5fa85172010-07-01 13:38:16 +00001061 if (adapter->vfs_allocated_count)
1062 adapter->num_tx_queues = 1;
1063 else
1064 adapter->num_tx_queues = adapter->rss_queues;
Alexander Duyck83b71802009-02-06 23:15:45 +00001065
Alexander Duyck047e0032009-10-27 15:49:27 +00001066 /* start with one vector for every rx queue */
1067 numvecs = adapter->num_rx_queues;
1068
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001069 /* if tx handler is separate add 1 for every tx queue */
Alexander Duycka99955f2009-11-12 18:37:19 +00001070 if (!(adapter->flags & IGB_FLAG_QUEUE_PAIRS))
1071 numvecs += adapter->num_tx_queues;
Alexander Duyck047e0032009-10-27 15:49:27 +00001072
1073 /* store the number of vectors reserved for queues */
1074 adapter->num_q_vectors = numvecs;
1075
1076 /* add 1 vector for link status interrupts */
1077 numvecs++;
Auke Kok9d5c8242008-01-24 02:22:38 -08001078 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
1079 GFP_KERNEL);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001080
Auke Kok9d5c8242008-01-24 02:22:38 -08001081 if (!adapter->msix_entries)
1082 goto msi_only;
1083
1084 for (i = 0; i < numvecs; i++)
1085 adapter->msix_entries[i].entry = i;
1086
1087 err = pci_enable_msix(adapter->pdev,
1088 adapter->msix_entries,
1089 numvecs);
1090 if (err == 0)
Alexander Duyck0c2cc022012-09-25 00:31:22 +00001091 return;
Auke Kok9d5c8242008-01-24 02:22:38 -08001092
1093 igb_reset_interrupt_capability(adapter);
1094
1095 /* If we can't do MSI-X, try MSI */
1096msi_only:
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001097#ifdef CONFIG_PCI_IOV
1098 /* disable SR-IOV for non MSI-X configurations */
1099 if (adapter->vf_data) {
1100 struct e1000_hw *hw = &adapter->hw;
1101 /* disable iov and allow time for transactions to clear */
1102 pci_disable_sriov(adapter->pdev);
1103 msleep(500);
1104
1105 kfree(adapter->vf_data);
1106 adapter->vf_data = NULL;
1107 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001108 wrfl();
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001109 msleep(100);
1110 dev_info(&adapter->pdev->dev, "IOV Disabled\n");
1111 }
1112#endif
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00001113 adapter->vfs_allocated_count = 0;
Alexander Duycka99955f2009-11-12 18:37:19 +00001114 adapter->rss_queues = 1;
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00001115 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
Auke Kok9d5c8242008-01-24 02:22:38 -08001116 adapter->num_rx_queues = 1;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001117 adapter->num_tx_queues = 1;
Alexander Duyck047e0032009-10-27 15:49:27 +00001118 adapter->num_q_vectors = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08001119 if (!pci_enable_msi(adapter->pdev))
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001120 adapter->flags |= IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -08001121}
1122
Alexander Duyck5536d212012-09-25 00:31:17 +00001123static void igb_add_ring(struct igb_ring *ring,
1124 struct igb_ring_container *head)
1125{
1126 head->ring = ring;
1127 head->count++;
1128}
1129
1130/**
1131 * igb_alloc_q_vector - Allocate memory for a single interrupt vector
1132 * @adapter: board private structure to initialize
1133 * @v_count: q_vectors allocated on adapter, used for ring interleaving
1134 * @v_idx: index of vector in adapter struct
1135 * @txr_count: total number of Tx rings to allocate
1136 * @txr_idx: index of first Tx ring to allocate
1137 * @rxr_count: total number of Rx rings to allocate
1138 * @rxr_idx: index of first Rx ring to allocate
1139 *
1140 * We allocate one q_vector. If allocation fails we return -ENOMEM.
1141 **/
1142static int igb_alloc_q_vector(struct igb_adapter *adapter,
1143 int v_count, int v_idx,
1144 int txr_count, int txr_idx,
1145 int rxr_count, int rxr_idx)
1146{
1147 struct igb_q_vector *q_vector;
1148 struct igb_ring *ring;
1149 int ring_count, size;
1150
1151 /* igb only supports 1 Tx and/or 1 Rx queue per vector */
1152 if (txr_count > 1 || rxr_count > 1)
1153 return -ENOMEM;
1154
1155 ring_count = txr_count + rxr_count;
1156 size = sizeof(struct igb_q_vector) +
1157 (sizeof(struct igb_ring) * ring_count);
1158
1159 /* allocate q_vector and rings */
1160 q_vector = kzalloc(size, GFP_KERNEL);
1161 if (!q_vector)
1162 return -ENOMEM;
1163
1164 /* initialize NAPI */
1165 netif_napi_add(adapter->netdev, &q_vector->napi,
1166 igb_poll, 64);
1167
1168 /* tie q_vector and adapter together */
1169 adapter->q_vector[v_idx] = q_vector;
1170 q_vector->adapter = adapter;
1171
1172 /* initialize work limits */
1173 q_vector->tx.work_limit = adapter->tx_work_limit;
1174
1175 /* initialize ITR configuration */
1176 q_vector->itr_register = adapter->hw.hw_addr + E1000_EITR(0);
1177 q_vector->itr_val = IGB_START_ITR;
1178
1179 /* initialize pointer to rings */
1180 ring = q_vector->ring;
1181
Alexander Duyck4e2276672013-02-12 02:31:01 +00001182 /* intialize ITR */
1183 if (rxr_count) {
1184 /* rx or rx/tx vector */
1185 if (!adapter->rx_itr_setting || adapter->rx_itr_setting > 3)
1186 q_vector->itr_val = adapter->rx_itr_setting;
1187 } else {
1188 /* tx only vector */
1189 if (!adapter->tx_itr_setting || adapter->tx_itr_setting > 3)
1190 q_vector->itr_val = adapter->tx_itr_setting;
1191 }
1192
Alexander Duyck5536d212012-09-25 00:31:17 +00001193 if (txr_count) {
1194 /* assign generic ring traits */
1195 ring->dev = &adapter->pdev->dev;
1196 ring->netdev = adapter->netdev;
1197
1198 /* configure backlink on ring */
1199 ring->q_vector = q_vector;
1200
1201 /* update q_vector Tx values */
1202 igb_add_ring(ring, &q_vector->tx);
1203
1204 /* For 82575, context index must be unique per ring. */
1205 if (adapter->hw.mac.type == e1000_82575)
1206 set_bit(IGB_RING_FLAG_TX_CTX_IDX, &ring->flags);
1207
1208 /* apply Tx specific ring traits */
1209 ring->count = adapter->tx_ring_count;
1210 ring->queue_index = txr_idx;
1211
1212 /* assign ring to adapter */
1213 adapter->tx_ring[txr_idx] = ring;
1214
1215 /* push pointer to next ring */
1216 ring++;
1217 }
1218
1219 if (rxr_count) {
1220 /* assign generic ring traits */
1221 ring->dev = &adapter->pdev->dev;
1222 ring->netdev = adapter->netdev;
1223
1224 /* configure backlink on ring */
1225 ring->q_vector = q_vector;
1226
1227 /* update q_vector Rx values */
1228 igb_add_ring(ring, &q_vector->rx);
1229
1230 /* set flag indicating ring supports SCTP checksum offload */
1231 if (adapter->hw.mac.type >= e1000_82576)
1232 set_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags);
1233
1234 /*
1235 * On i350, i210, and i211, loopback VLAN packets
1236 * have the tag byte-swapped.
1237 * */
1238 if (adapter->hw.mac.type >= e1000_i350)
1239 set_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &ring->flags);
1240
1241 /* apply Rx specific ring traits */
1242 ring->count = adapter->rx_ring_count;
1243 ring->queue_index = rxr_idx;
1244
1245 /* assign ring to adapter */
1246 adapter->rx_ring[rxr_idx] = ring;
1247 }
1248
1249 return 0;
1250}
1251
1252
Auke Kok9d5c8242008-01-24 02:22:38 -08001253/**
Alexander Duyck047e0032009-10-27 15:49:27 +00001254 * igb_alloc_q_vectors - Allocate memory for interrupt vectors
1255 * @adapter: board private structure to initialize
1256 *
1257 * We allocate one q_vector per queue interrupt. If allocation fails we
1258 * return -ENOMEM.
1259 **/
1260static int igb_alloc_q_vectors(struct igb_adapter *adapter)
1261{
Alexander Duyck5536d212012-09-25 00:31:17 +00001262 int q_vectors = adapter->num_q_vectors;
1263 int rxr_remaining = adapter->num_rx_queues;
1264 int txr_remaining = adapter->num_tx_queues;
1265 int rxr_idx = 0, txr_idx = 0, v_idx = 0;
1266 int err;
Alexander Duyck047e0032009-10-27 15:49:27 +00001267
Alexander Duyck5536d212012-09-25 00:31:17 +00001268 if (q_vectors >= (rxr_remaining + txr_remaining)) {
1269 for (; rxr_remaining; v_idx++) {
1270 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1271 0, 0, 1, rxr_idx);
1272
1273 if (err)
1274 goto err_out;
1275
1276 /* update counts and index */
1277 rxr_remaining--;
1278 rxr_idx++;
1279 }
1280 }
1281
1282 for (; v_idx < q_vectors; v_idx++) {
1283 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
1284 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
1285 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1286 tqpv, txr_idx, rqpv, rxr_idx);
1287
1288 if (err)
Alexander Duyck047e0032009-10-27 15:49:27 +00001289 goto err_out;
Alexander Duyck5536d212012-09-25 00:31:17 +00001290
1291 /* update counts and index */
1292 rxr_remaining -= rqpv;
1293 txr_remaining -= tqpv;
1294 rxr_idx++;
1295 txr_idx++;
Alexander Duyck047e0032009-10-27 15:49:27 +00001296 }
Alexander Duyck81c2fc22011-08-26 07:45:20 +00001297
Alexander Duyck047e0032009-10-27 15:49:27 +00001298 return 0;
1299
1300err_out:
Alexander Duyck5536d212012-09-25 00:31:17 +00001301 adapter->num_tx_queues = 0;
1302 adapter->num_rx_queues = 0;
1303 adapter->num_q_vectors = 0;
1304
1305 while (v_idx--)
1306 igb_free_q_vector(adapter, v_idx);
1307
Alexander Duyck047e0032009-10-27 15:49:27 +00001308 return -ENOMEM;
1309}
1310
Alexander Duyck047e0032009-10-27 15:49:27 +00001311/**
1312 * igb_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
1313 *
1314 * This function initializes the interrupts and allocates all of the queues.
1315 **/
Stefan Assmann53c7d062012-12-04 06:00:12 +00001316static int igb_init_interrupt_scheme(struct igb_adapter *adapter, bool msix)
Alexander Duyck047e0032009-10-27 15:49:27 +00001317{
1318 struct pci_dev *pdev = adapter->pdev;
1319 int err;
1320
Stefan Assmann53c7d062012-12-04 06:00:12 +00001321 igb_set_interrupt_capability(adapter, msix);
Alexander Duyck047e0032009-10-27 15:49:27 +00001322
1323 err = igb_alloc_q_vectors(adapter);
1324 if (err) {
1325 dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
1326 goto err_alloc_q_vectors;
1327 }
1328
Alexander Duyck5536d212012-09-25 00:31:17 +00001329 igb_cache_ring_register(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00001330
1331 return 0;
Alexander Duyck5536d212012-09-25 00:31:17 +00001332
Alexander Duyck047e0032009-10-27 15:49:27 +00001333err_alloc_q_vectors:
1334 igb_reset_interrupt_capability(adapter);
1335 return err;
1336}
1337
1338/**
Auke Kok9d5c8242008-01-24 02:22:38 -08001339 * igb_request_irq - initialize interrupts
1340 *
1341 * Attempts to configure interrupts using the best available
1342 * capabilities of the hardware and kernel.
1343 **/
1344static int igb_request_irq(struct igb_adapter *adapter)
1345{
1346 struct net_device *netdev = adapter->netdev;
Alexander Duyck047e0032009-10-27 15:49:27 +00001347 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08001348 int err = 0;
1349
1350 if (adapter->msix_entries) {
1351 err = igb_request_msix(adapter);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001352 if (!err)
Auke Kok9d5c8242008-01-24 02:22:38 -08001353 goto request_done;
Auke Kok9d5c8242008-01-24 02:22:38 -08001354 /* fall back to MSI */
Alexander Duyck5536d212012-09-25 00:31:17 +00001355 igb_free_all_tx_resources(adapter);
1356 igb_free_all_rx_resources(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001357
Alexander Duyck047e0032009-10-27 15:49:27 +00001358 igb_clear_interrupt_scheme(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001359 err = igb_init_interrupt_scheme(adapter, false);
1360 if (err)
Alexander Duyck047e0032009-10-27 15:49:27 +00001361 goto request_done;
Stefan Assmann53c7d062012-12-04 06:00:12 +00001362
Alexander Duyck047e0032009-10-27 15:49:27 +00001363 igb_setup_all_tx_resources(adapter);
1364 igb_setup_all_rx_resources(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001365 igb_configure(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001366 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001367
Alexander Duyckc74d5882011-08-26 07:46:45 +00001368 igb_assign_vector(adapter->q_vector[0], 0);
1369
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001370 if (adapter->flags & IGB_FLAG_HAS_MSI) {
Alexander Duyckc74d5882011-08-26 07:46:45 +00001371 err = request_irq(pdev->irq, igb_intr_msi, 0,
Alexander Duyck047e0032009-10-27 15:49:27 +00001372 netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001373 if (!err)
1374 goto request_done;
Alexander Duyck047e0032009-10-27 15:49:27 +00001375
Auke Kok9d5c8242008-01-24 02:22:38 -08001376 /* fall back to legacy interrupts */
1377 igb_reset_interrupt_capability(adapter);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001378 adapter->flags &= ~IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -08001379 }
1380
Alexander Duyckc74d5882011-08-26 07:46:45 +00001381 err = request_irq(pdev->irq, igb_intr, IRQF_SHARED,
Alexander Duyck047e0032009-10-27 15:49:27 +00001382 netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001383
Andy Gospodarek6cb5e572008-02-15 14:05:25 -08001384 if (err)
Alexander Duyckc74d5882011-08-26 07:46:45 +00001385 dev_err(&pdev->dev, "Error %d getting interrupt\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08001386 err);
Auke Kok9d5c8242008-01-24 02:22:38 -08001387
1388request_done:
1389 return err;
1390}
1391
1392static void igb_free_irq(struct igb_adapter *adapter)
1393{
Auke Kok9d5c8242008-01-24 02:22:38 -08001394 if (adapter->msix_entries) {
1395 int vector = 0, i;
1396
Alexander Duyck047e0032009-10-27 15:49:27 +00001397 free_irq(adapter->msix_entries[vector++].vector, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001398
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001399 for (i = 0; i < adapter->num_q_vectors; i++)
Alexander Duyck047e0032009-10-27 15:49:27 +00001400 free_irq(adapter->msix_entries[vector++].vector,
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001401 adapter->q_vector[i]);
Alexander Duyck047e0032009-10-27 15:49:27 +00001402 } else {
1403 free_irq(adapter->pdev->irq, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001404 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001405}
1406
1407/**
1408 * igb_irq_disable - Mask off interrupt generation on the NIC
1409 * @adapter: board private structure
1410 **/
1411static void igb_irq_disable(struct igb_adapter *adapter)
1412{
1413 struct e1000_hw *hw = &adapter->hw;
1414
Alexander Duyck25568a52009-10-27 23:49:59 +00001415 /*
1416 * we need to be careful when disabling interrupts. The VFs are also
1417 * mapped into these registers and so clearing the bits can cause
1418 * issues on the VF drivers so we only need to clear what we set
1419 */
Auke Kok9d5c8242008-01-24 02:22:38 -08001420 if (adapter->msix_entries) {
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001421 u32 regval = rd32(E1000_EIAM);
1422 wr32(E1000_EIAM, regval & ~adapter->eims_enable_mask);
1423 wr32(E1000_EIMC, adapter->eims_enable_mask);
1424 regval = rd32(E1000_EIAC);
1425 wr32(E1000_EIAC, regval & ~adapter->eims_enable_mask);
Auke Kok9d5c8242008-01-24 02:22:38 -08001426 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001427
1428 wr32(E1000_IAM, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08001429 wr32(E1000_IMC, ~0);
1430 wrfl();
Emil Tantilov81a61852010-08-02 14:40:52 +00001431 if (adapter->msix_entries) {
1432 int i;
1433 for (i = 0; i < adapter->num_q_vectors; i++)
1434 synchronize_irq(adapter->msix_entries[i].vector);
1435 } else {
1436 synchronize_irq(adapter->pdev->irq);
1437 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001438}
1439
1440/**
1441 * igb_irq_enable - Enable default interrupt generation settings
1442 * @adapter: board private structure
1443 **/
1444static void igb_irq_enable(struct igb_adapter *adapter)
1445{
1446 struct e1000_hw *hw = &adapter->hw;
1447
1448 if (adapter->msix_entries) {
Alexander Duyck06218a82011-08-26 07:46:55 +00001449 u32 ims = E1000_IMS_LSC | E1000_IMS_DOUTSYNC | E1000_IMS_DRSTA;
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001450 u32 regval = rd32(E1000_EIAC);
1451 wr32(E1000_EIAC, regval | adapter->eims_enable_mask);
1452 regval = rd32(E1000_EIAM);
1453 wr32(E1000_EIAM, regval | adapter->eims_enable_mask);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001454 wr32(E1000_EIMS, adapter->eims_enable_mask);
Alexander Duyck25568a52009-10-27 23:49:59 +00001455 if (adapter->vfs_allocated_count) {
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001456 wr32(E1000_MBVFIMR, 0xFF);
Alexander Duyck25568a52009-10-27 23:49:59 +00001457 ims |= E1000_IMS_VMMB;
1458 }
1459 wr32(E1000_IMS, ims);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001460 } else {
Alexander Duyck55cac242009-11-19 12:42:21 +00001461 wr32(E1000_IMS, IMS_ENABLE_MASK |
1462 E1000_IMS_DRSTA);
1463 wr32(E1000_IAM, IMS_ENABLE_MASK |
1464 E1000_IMS_DRSTA);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001465 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001466}
1467
1468static void igb_update_mng_vlan(struct igb_adapter *adapter)
1469{
Alexander Duyck51466232009-10-27 23:47:35 +00001470 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08001471 u16 vid = adapter->hw.mng_cookie.vlan_id;
1472 u16 old_vid = adapter->mng_vlan_id;
Auke Kok9d5c8242008-01-24 02:22:38 -08001473
Alexander Duyck51466232009-10-27 23:47:35 +00001474 if (hw->mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
1475 /* add VID to filter table */
1476 igb_vfta_set(hw, vid, true);
1477 adapter->mng_vlan_id = vid;
1478 } else {
1479 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
1480 }
1481
1482 if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
1483 (vid != old_vid) &&
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001484 !test_bit(old_vid, adapter->active_vlans)) {
Alexander Duyck51466232009-10-27 23:47:35 +00001485 /* remove VID from filter table */
1486 igb_vfta_set(hw, old_vid, false);
Auke Kok9d5c8242008-01-24 02:22:38 -08001487 }
1488}
1489
1490/**
1491 * igb_release_hw_control - release control of the h/w to f/w
1492 * @adapter: address of board private structure
1493 *
1494 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
1495 * For ASF and Pass Through versions of f/w this means that the
1496 * driver is no longer loaded.
1497 *
1498 **/
1499static void igb_release_hw_control(struct igb_adapter *adapter)
1500{
1501 struct e1000_hw *hw = &adapter->hw;
1502 u32 ctrl_ext;
1503
1504 /* Let firmware take over control of h/w */
1505 ctrl_ext = rd32(E1000_CTRL_EXT);
1506 wr32(E1000_CTRL_EXT,
1507 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
1508}
1509
Auke Kok9d5c8242008-01-24 02:22:38 -08001510/**
1511 * igb_get_hw_control - get control of the h/w from f/w
1512 * @adapter: address of board private structure
1513 *
1514 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
1515 * For ASF and Pass Through versions of f/w this means that
1516 * the driver is loaded.
1517 *
1518 **/
1519static void igb_get_hw_control(struct igb_adapter *adapter)
1520{
1521 struct e1000_hw *hw = &adapter->hw;
1522 u32 ctrl_ext;
1523
1524 /* Let firmware know the driver has taken over */
1525 ctrl_ext = rd32(E1000_CTRL_EXT);
1526 wr32(E1000_CTRL_EXT,
1527 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
1528}
1529
Auke Kok9d5c8242008-01-24 02:22:38 -08001530/**
1531 * igb_configure - configure the hardware for RX and TX
1532 * @adapter: private board structure
1533 **/
1534static void igb_configure(struct igb_adapter *adapter)
1535{
1536 struct net_device *netdev = adapter->netdev;
1537 int i;
1538
1539 igb_get_hw_control(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001540 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001541
1542 igb_restore_vlan(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001543
Alexander Duyck85b430b2009-10-27 15:50:29 +00001544 igb_setup_tctl(adapter);
Alexander Duyck06cf2662009-10-27 15:53:25 +00001545 igb_setup_mrqc(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001546 igb_setup_rctl(adapter);
Alexander Duyck85b430b2009-10-27 15:50:29 +00001547
1548 igb_configure_tx(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001549 igb_configure_rx(adapter);
Alexander Duyck662d7202008-06-27 11:00:29 -07001550
1551 igb_rx_fifo_flush_82575(&adapter->hw);
1552
Alexander Duyckc493ea42009-03-20 00:16:50 +00001553 /* call igb_desc_unused which always leaves
Auke Kok9d5c8242008-01-24 02:22:38 -08001554 * at least 1 descriptor unused to make sure
1555 * next_to_use != next_to_clean */
1556 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00001557 struct igb_ring *ring = adapter->rx_ring[i];
Alexander Duyckcd392f52011-08-26 07:43:59 +00001558 igb_alloc_rx_buffers(ring, igb_desc_unused(ring));
Auke Kok9d5c8242008-01-24 02:22:38 -08001559 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001560}
1561
Nick Nunley88a268c2010-02-17 01:01:59 +00001562/**
1563 * igb_power_up_link - Power up the phy/serdes link
1564 * @adapter: address of board private structure
1565 **/
1566void igb_power_up_link(struct igb_adapter *adapter)
1567{
Akeem G. Abodunrin76886592012-07-17 04:51:18 +00001568 igb_reset_phy(&adapter->hw);
1569
Nick Nunley88a268c2010-02-17 01:01:59 +00001570 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1571 igb_power_up_phy_copper(&adapter->hw);
1572 else
1573 igb_power_up_serdes_link_82575(&adapter->hw);
1574}
1575
1576/**
1577 * igb_power_down_link - Power down the phy/serdes link
1578 * @adapter: address of board private structure
1579 */
1580static void igb_power_down_link(struct igb_adapter *adapter)
1581{
1582 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1583 igb_power_down_phy_copper_82575(&adapter->hw);
1584 else
1585 igb_shutdown_serdes_link_82575(&adapter->hw);
1586}
Auke Kok9d5c8242008-01-24 02:22:38 -08001587
1588/**
1589 * igb_up - Open the interface and prepare it to handle traffic
1590 * @adapter: board private structure
1591 **/
Auke Kok9d5c8242008-01-24 02:22:38 -08001592int igb_up(struct igb_adapter *adapter)
1593{
1594 struct e1000_hw *hw = &adapter->hw;
1595 int i;
1596
1597 /* hardware has been reset, we need to reload some things */
1598 igb_configure(adapter);
1599
1600 clear_bit(__IGB_DOWN, &adapter->state);
1601
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001602 for (i = 0; i < adapter->num_q_vectors; i++)
1603 napi_enable(&(adapter->q_vector[i]->napi));
1604
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001605 if (adapter->msix_entries)
Auke Kok9d5c8242008-01-24 02:22:38 -08001606 igb_configure_msix(adapter);
Alexander Duyckfeeb2722010-02-03 21:59:51 +00001607 else
1608 igb_assign_vector(adapter->q_vector[0], 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08001609
1610 /* Clear any pending interrupts. */
1611 rd32(E1000_ICR);
1612 igb_irq_enable(adapter);
1613
Alexander Duyckd4960302009-10-27 15:53:45 +00001614 /* notify VFs that reset has been completed */
1615 if (adapter->vfs_allocated_count) {
1616 u32 reg_data = rd32(E1000_CTRL_EXT);
1617 reg_data |= E1000_CTRL_EXT_PFRSTD;
1618 wr32(E1000_CTRL_EXT, reg_data);
1619 }
1620
Jesse Brandeburg4cb9be72009-04-21 18:42:05 +00001621 netif_tx_start_all_queues(adapter->netdev);
1622
Alexander Duyck25568a52009-10-27 23:49:59 +00001623 /* start the watchdog. */
1624 hw->mac.get_link_status = 1;
1625 schedule_work(&adapter->watchdog_task);
1626
Auke Kok9d5c8242008-01-24 02:22:38 -08001627 return 0;
1628}
1629
1630void igb_down(struct igb_adapter *adapter)
1631{
Auke Kok9d5c8242008-01-24 02:22:38 -08001632 struct net_device *netdev = adapter->netdev;
Alexander Duyck330a6d62009-10-27 23:51:35 +00001633 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08001634 u32 tctl, rctl;
1635 int i;
1636
1637 /* signal that we're down so the interrupt handler does not
1638 * reschedule our watchdog timer */
1639 set_bit(__IGB_DOWN, &adapter->state);
1640
1641 /* disable receives in the hardware */
1642 rctl = rd32(E1000_RCTL);
1643 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
1644 /* flush and sleep below */
1645
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001646 netif_tx_stop_all_queues(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001647
1648 /* disable transmits in the hardware */
1649 tctl = rd32(E1000_TCTL);
1650 tctl &= ~E1000_TCTL_EN;
1651 wr32(E1000_TCTL, tctl);
1652 /* flush both disables and wait for them to finish */
1653 wrfl();
1654 msleep(10);
1655
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001656 for (i = 0; i < adapter->num_q_vectors; i++)
1657 napi_disable(&(adapter->q_vector[i]->napi));
Auke Kok9d5c8242008-01-24 02:22:38 -08001658
Auke Kok9d5c8242008-01-24 02:22:38 -08001659 igb_irq_disable(adapter);
1660
1661 del_timer_sync(&adapter->watchdog_timer);
1662 del_timer_sync(&adapter->phy_info_timer);
1663
Auke Kok9d5c8242008-01-24 02:22:38 -08001664 netif_carrier_off(netdev);
Alexander Duyck04fe6352009-02-06 23:22:32 +00001665
1666 /* record the stats before reset*/
Eric Dumazet12dcd862010-10-15 17:27:10 +00001667 spin_lock(&adapter->stats64_lock);
1668 igb_update_stats(adapter, &adapter->stats64);
1669 spin_unlock(&adapter->stats64_lock);
Alexander Duyck04fe6352009-02-06 23:22:32 +00001670
Auke Kok9d5c8242008-01-24 02:22:38 -08001671 adapter->link_speed = 0;
1672 adapter->link_duplex = 0;
1673
Jeff Kirsher30236822008-06-24 17:01:15 -07001674 if (!pci_channel_offline(adapter->pdev))
1675 igb_reset(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001676 igb_clean_all_tx_rings(adapter);
1677 igb_clean_all_rx_rings(adapter);
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00001678#ifdef CONFIG_IGB_DCA
1679
1680 /* since we reset the hardware DCA settings were cleared */
1681 igb_setup_dca(adapter);
1682#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08001683}
1684
1685void igb_reinit_locked(struct igb_adapter *adapter)
1686{
1687 WARN_ON(in_interrupt());
1688 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
1689 msleep(1);
1690 igb_down(adapter);
1691 igb_up(adapter);
1692 clear_bit(__IGB_RESETTING, &adapter->state);
1693}
1694
1695void igb_reset(struct igb_adapter *adapter)
1696{
Alexander Duyck090b1792009-10-27 23:51:55 +00001697 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08001698 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001699 struct e1000_mac_info *mac = &hw->mac;
1700 struct e1000_fc_info *fc = &hw->fc;
Matthew Vickd48507f2012-11-08 04:03:58 +00001701 u32 pba = 0, tx_space, min_tx_space, min_rx_space, hwm;
Auke Kok9d5c8242008-01-24 02:22:38 -08001702
1703 /* Repartition Pba for greater than 9k mtu
1704 * To take effect CTRL.RST is required.
1705 */
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001706 switch (mac->type) {
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +00001707 case e1000_i350:
Alexander Duyck55cac242009-11-19 12:42:21 +00001708 case e1000_82580:
1709 pba = rd32(E1000_RXPBS);
1710 pba = igb_rxpbs_adjust_82580(pba);
1711 break;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001712 case e1000_82576:
Alexander Duyckd249be52009-10-27 23:46:38 +00001713 pba = rd32(E1000_RXPBS);
1714 pba &= E1000_RXPBS_SIZE_MASK_82576;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001715 break;
1716 case e1000_82575:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001717 case e1000_i210:
1718 case e1000_i211:
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001719 default:
1720 pba = E1000_PBA_34K;
1721 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001722 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001723
Alexander Duyck2d064c02008-07-08 15:10:12 -07001724 if ((adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) &&
1725 (mac->type < e1000_82576)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001726 /* adjust PBA for jumbo frames */
1727 wr32(E1000_PBA, pba);
1728
1729 /* To maintain wire speed transmits, the Tx FIFO should be
1730 * large enough to accommodate two full transmit packets,
1731 * rounded up to the next 1KB and expressed in KB. Likewise,
1732 * the Rx FIFO should be large enough to accommodate at least
1733 * one full receive packet and is similarly rounded up and
1734 * expressed in KB. */
1735 pba = rd32(E1000_PBA);
1736 /* upper 16 bits has Tx packet buffer allocation size in KB */
1737 tx_space = pba >> 16;
1738 /* lower 16 bits has Rx packet buffer allocation size in KB */
1739 pba &= 0xffff;
1740 /* the tx fifo also stores 16 bytes of information about the tx
1741 * but don't include ethernet FCS because hardware appends it */
1742 min_tx_space = (adapter->max_frame_size +
Alexander Duyck85e8d002009-02-16 00:00:20 -08001743 sizeof(union e1000_adv_tx_desc) -
Auke Kok9d5c8242008-01-24 02:22:38 -08001744 ETH_FCS_LEN) * 2;
1745 min_tx_space = ALIGN(min_tx_space, 1024);
1746 min_tx_space >>= 10;
1747 /* software strips receive CRC, so leave room for it */
1748 min_rx_space = adapter->max_frame_size;
1749 min_rx_space = ALIGN(min_rx_space, 1024);
1750 min_rx_space >>= 10;
1751
1752 /* If current Tx allocation is less than the min Tx FIFO size,
1753 * and the min Tx FIFO size is less than the current Rx FIFO
1754 * allocation, take space away from current Rx allocation */
1755 if (tx_space < min_tx_space &&
1756 ((min_tx_space - tx_space) < pba)) {
1757 pba = pba - (min_tx_space - tx_space);
1758
1759 /* if short on rx space, rx wins and must trump tx
1760 * adjustment */
1761 if (pba < min_rx_space)
1762 pba = min_rx_space;
1763 }
Alexander Duyck2d064c02008-07-08 15:10:12 -07001764 wr32(E1000_PBA, pba);
Auke Kok9d5c8242008-01-24 02:22:38 -08001765 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001766
1767 /* flow control settings */
1768 /* The high water mark must be low enough to fit one full frame
1769 * (or the size used for early receive) above it in the Rx FIFO.
1770 * Set it to the lower of:
1771 * - 90% of the Rx FIFO size, or
1772 * - the full Rx FIFO size minus one full frame */
1773 hwm = min(((pba << 10) * 9 / 10),
Alexander Duyck2d064c02008-07-08 15:10:12 -07001774 ((pba << 10) - 2 * adapter->max_frame_size));
Auke Kok9d5c8242008-01-24 02:22:38 -08001775
Matthew Vickd48507f2012-11-08 04:03:58 +00001776 fc->high_water = hwm & 0xFFFFFFF0; /* 16-byte granularity */
Alexander Duyckd405ea32009-12-23 13:21:27 +00001777 fc->low_water = fc->high_water - 16;
Auke Kok9d5c8242008-01-24 02:22:38 -08001778 fc->pause_time = 0xFFFF;
1779 fc->send_xon = 1;
Alexander Duyck0cce1192009-07-23 18:10:24 +00001780 fc->current_mode = fc->requested_mode;
Auke Kok9d5c8242008-01-24 02:22:38 -08001781
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001782 /* disable receive for all VFs and wait one second */
1783 if (adapter->vfs_allocated_count) {
1784 int i;
1785 for (i = 0 ; i < adapter->vfs_allocated_count; i++)
Greg Rose8fa7e0f2010-11-06 05:43:21 +00001786 adapter->vf_data[i].flags &= IGB_VF_FLAG_PF_SET_MAC;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001787
1788 /* ping all the active vfs to let them know we are going down */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00001789 igb_ping_all_vfs(adapter);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001790
1791 /* disable transmits and receives */
1792 wr32(E1000_VFRE, 0);
1793 wr32(E1000_VFTE, 0);
1794 }
1795
Auke Kok9d5c8242008-01-24 02:22:38 -08001796 /* Allow time for pending master requests to run */
Alexander Duyck330a6d62009-10-27 23:51:35 +00001797 hw->mac.ops.reset_hw(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001798 wr32(E1000_WUC, 0);
1799
Alexander Duyck330a6d62009-10-27 23:51:35 +00001800 if (hw->mac.ops.init_hw(hw))
Alexander Duyck090b1792009-10-27 23:51:55 +00001801 dev_err(&pdev->dev, "Hardware Error\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08001802
Matthew Vicka27416b2012-04-18 02:57:44 +00001803 /*
1804 * Flow control settings reset on hardware reset, so guarantee flow
1805 * control is off when forcing speed.
1806 */
1807 if (!hw->mac.autoneg)
1808 igb_force_mac_fc(hw);
1809
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00001810 igb_init_dmac(adapter, pba);
Carolyn Wybornye4288932012-12-07 03:01:42 +00001811#ifdef CONFIG_IGB_HWMON
1812 /* Re-initialize the thermal sensor on i350 devices. */
1813 if (!test_bit(__IGB_DOWN, &adapter->state)) {
1814 if (mac->type == e1000_i350 && hw->bus.func == 0) {
1815 /* If present, re-initialize the external thermal sensor
1816 * interface.
1817 */
1818 if (adapter->ets)
1819 mac->ops.init_thermal_sensor_thresh(hw);
1820 }
1821 }
1822#endif
Nick Nunley88a268c2010-02-17 01:01:59 +00001823 if (!netif_running(adapter->netdev))
1824 igb_power_down_link(adapter);
1825
Auke Kok9d5c8242008-01-24 02:22:38 -08001826 igb_update_mng_vlan(adapter);
1827
1828 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
1829 wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
1830
Matthew Vick1f6e8172012-08-18 07:26:33 +00001831 /* Re-enable PTP, where applicable. */
1832 igb_ptp_reset(adapter);
Matthew Vick1f6e8172012-08-18 07:26:33 +00001833
Alexander Duyck330a6d62009-10-27 23:51:35 +00001834 igb_get_phy_info(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001835}
1836
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001837static netdev_features_t igb_fix_features(struct net_device *netdev,
1838 netdev_features_t features)
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001839{
1840 /*
1841 * Since there is no support for separate rx/tx vlan accel
1842 * enable/disable make sure tx flag is always in same state as rx.
1843 */
1844 if (features & NETIF_F_HW_VLAN_RX)
1845 features |= NETIF_F_HW_VLAN_TX;
1846 else
1847 features &= ~NETIF_F_HW_VLAN_TX;
1848
1849 return features;
1850}
1851
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001852static int igb_set_features(struct net_device *netdev,
1853 netdev_features_t features)
Michał Mirosławac52caa2011-06-08 08:38:01 +00001854{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001855 netdev_features_t changed = netdev->features ^ features;
Ben Greear89eaefb2012-03-06 09:41:58 +00001856 struct igb_adapter *adapter = netdev_priv(netdev);
Michał Mirosławac52caa2011-06-08 08:38:01 +00001857
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001858 if (changed & NETIF_F_HW_VLAN_RX)
1859 igb_vlan_mode(netdev, features);
1860
Ben Greear89eaefb2012-03-06 09:41:58 +00001861 if (!(changed & NETIF_F_RXALL))
1862 return 0;
1863
1864 netdev->features = features;
1865
1866 if (netif_running(netdev))
1867 igb_reinit_locked(adapter);
1868 else
1869 igb_reset(adapter);
1870
Michał Mirosławac52caa2011-06-08 08:38:01 +00001871 return 0;
1872}
1873
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001874static const struct net_device_ops igb_netdev_ops = {
Alexander Duyck559e9c42009-10-27 23:52:50 +00001875 .ndo_open = igb_open,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001876 .ndo_stop = igb_close,
Alexander Duyckcd392f52011-08-26 07:43:59 +00001877 .ndo_start_xmit = igb_xmit_frame,
Eric Dumazet12dcd862010-10-15 17:27:10 +00001878 .ndo_get_stats64 = igb_get_stats64,
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001879 .ndo_set_rx_mode = igb_set_rx_mode,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001880 .ndo_set_mac_address = igb_set_mac,
1881 .ndo_change_mtu = igb_change_mtu,
1882 .ndo_do_ioctl = igb_ioctl,
1883 .ndo_tx_timeout = igb_tx_timeout,
1884 .ndo_validate_addr = eth_validate_addr,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001885 .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
1886 .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
Williams, Mitch A8151d292010-02-10 01:44:24 +00001887 .ndo_set_vf_mac = igb_ndo_set_vf_mac,
1888 .ndo_set_vf_vlan = igb_ndo_set_vf_vlan,
1889 .ndo_set_vf_tx_rate = igb_ndo_set_vf_bw,
1890 .ndo_get_vf_config = igb_ndo_get_vf_config,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001891#ifdef CONFIG_NET_POLL_CONTROLLER
1892 .ndo_poll_controller = igb_netpoll,
1893#endif
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001894 .ndo_fix_features = igb_fix_features,
1895 .ndo_set_features = igb_set_features,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001896};
1897
Taku Izumi42bfd33a2008-06-20 12:10:30 +09001898/**
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001899 * igb_set_fw_version - Configure version string for ethtool
1900 * @adapter: adapter struct
1901 *
1902 **/
1903void igb_set_fw_version(struct igb_adapter *adapter)
1904{
1905 struct e1000_hw *hw = &adapter->hw;
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001906 struct e1000_fw_version fw;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001907
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001908 igb_get_fw_version(hw, &fw);
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001909
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001910 switch (hw->mac.type) {
1911 case e1000_i211:
1912 snprintf(adapter->fw_version, sizeof(adapter->fw_version),
1913 "%2d.%2d-%d",
1914 fw.invm_major, fw.invm_minor, fw.invm_img_type);
1915 break;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001916
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001917 default:
1918 /* if option is rom valid, display its version too */
1919 if (fw.or_valid) {
1920 snprintf(adapter->fw_version,
1921 sizeof(adapter->fw_version),
1922 "%d.%d, 0x%08x, %d.%d.%d",
1923 fw.eep_major, fw.eep_minor, fw.etrack_id,
1924 fw.or_major, fw.or_build, fw.or_patch);
1925 /* no option rom */
1926 } else {
1927 snprintf(adapter->fw_version,
1928 sizeof(adapter->fw_version),
1929 "%d.%d, 0x%08x",
1930 fw.eep_major, fw.eep_minor, fw.etrack_id);
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001931 }
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001932 break;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001933 }
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001934 return;
1935}
1936
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00001937/* igb_init_i2c - Init I2C interface
1938 * @adapter: pointer to adapter structure
1939 *
1940 */
1941static s32 igb_init_i2c(struct igb_adapter *adapter)
1942{
1943 s32 status = E1000_SUCCESS;
1944
1945 /* I2C interface supported on i350 devices */
1946 if (adapter->hw.mac.type != e1000_i350)
1947 return E1000_SUCCESS;
1948
1949 /* Initialize the i2c bus which is controlled by the registers.
1950 * This bus will use the i2c_algo_bit structue that implements
1951 * the protocol through toggling of the 4 bits in the register.
1952 */
1953 adapter->i2c_adap.owner = THIS_MODULE;
1954 adapter->i2c_algo = igb_i2c_algo;
1955 adapter->i2c_algo.data = adapter;
1956 adapter->i2c_adap.algo_data = &adapter->i2c_algo;
1957 adapter->i2c_adap.dev.parent = &adapter->pdev->dev;
1958 strlcpy(adapter->i2c_adap.name, "igb BB",
1959 sizeof(adapter->i2c_adap.name));
1960 status = i2c_bit_add_bus(&adapter->i2c_adap);
1961 return status;
1962}
1963
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001964/**
Auke Kok9d5c8242008-01-24 02:22:38 -08001965 * igb_probe - Device Initialization Routine
1966 * @pdev: PCI device information struct
1967 * @ent: entry in igb_pci_tbl
1968 *
1969 * Returns 0 on success, negative on failure
1970 *
1971 * igb_probe initializes an adapter identified by a pci_dev structure.
1972 * The OS initialization, configuring of the adapter private structure,
1973 * and a hardware reset occur.
1974 **/
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001975static int igb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Auke Kok9d5c8242008-01-24 02:22:38 -08001976{
1977 struct net_device *netdev;
1978 struct igb_adapter *adapter;
1979 struct e1000_hw *hw;
Alexander Duyck4337e992009-10-27 23:48:31 +00001980 u16 eeprom_data = 0;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00001981 s32 ret_val;
Alexander Duyck4337e992009-10-27 23:48:31 +00001982 static int global_quad_port_a; /* global quad port a indication */
Auke Kok9d5c8242008-01-24 02:22:38 -08001983 const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
1984 unsigned long mmio_start, mmio_len;
David S. Miller2d6a5e92009-03-17 15:01:30 -07001985 int err, pci_using_dac;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00001986 u8 part_str[E1000_PBANUM_LENGTH];
Auke Kok9d5c8242008-01-24 02:22:38 -08001987
Andy Gospodarekbded64a2010-07-21 06:40:31 +00001988 /* Catch broken hardware that put the wrong VF device ID in
1989 * the PCIe SR-IOV capability.
1990 */
1991 if (pdev->is_virtfn) {
1992 WARN(1, KERN_ERR "%s (%hx:%hx) should not be a VF!\n",
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001993 pci_name(pdev), pdev->vendor, pdev->device);
Andy Gospodarekbded64a2010-07-21 06:40:31 +00001994 return -EINVAL;
1995 }
1996
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001997 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001998 if (err)
1999 return err;
2000
2001 pci_using_dac = 0;
Alexander Duyck59d71982010-04-27 13:09:25 +00002002 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08002003 if (!err) {
Alexander Duyck59d71982010-04-27 13:09:25 +00002004 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08002005 if (!err)
2006 pci_using_dac = 1;
2007 } else {
Alexander Duyck59d71982010-04-27 13:09:25 +00002008 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08002009 if (err) {
Alexander Duyck59d71982010-04-27 13:09:25 +00002010 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08002011 if (err) {
2012 dev_err(&pdev->dev, "No usable DMA "
2013 "configuration, aborting\n");
2014 goto err_dma;
2015 }
2016 }
2017 }
2018
Alexander Duyckaed5dec2009-02-06 23:16:04 +00002019 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
2020 IORESOURCE_MEM),
2021 igb_driver_name);
Auke Kok9d5c8242008-01-24 02:22:38 -08002022 if (err)
2023 goto err_pci_reg;
2024
Frans Pop19d5afd2009-10-02 10:04:12 -07002025 pci_enable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08002026
Auke Kok9d5c8242008-01-24 02:22:38 -08002027 pci_set_master(pdev);
Auke Kokc682fc22008-04-23 11:09:34 -07002028 pci_save_state(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002029
2030 err = -ENOMEM;
Alexander Duyck1bfaf072009-02-19 20:39:23 -08002031 netdev = alloc_etherdev_mq(sizeof(struct igb_adapter),
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00002032 IGB_MAX_TX_QUEUES);
Auke Kok9d5c8242008-01-24 02:22:38 -08002033 if (!netdev)
2034 goto err_alloc_etherdev;
2035
2036 SET_NETDEV_DEV(netdev, &pdev->dev);
2037
2038 pci_set_drvdata(pdev, netdev);
2039 adapter = netdev_priv(netdev);
2040 adapter->netdev = netdev;
2041 adapter->pdev = pdev;
2042 hw = &adapter->hw;
2043 hw->back = adapter;
stephen hemmingerb3f4d592012-03-13 06:04:20 +00002044 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
Auke Kok9d5c8242008-01-24 02:22:38 -08002045
2046 mmio_start = pci_resource_start(pdev, 0);
2047 mmio_len = pci_resource_len(pdev, 0);
2048
2049 err = -EIO;
Alexander Duyck28b07592009-02-06 23:20:31 +00002050 hw->hw_addr = ioremap(mmio_start, mmio_len);
2051 if (!hw->hw_addr)
Auke Kok9d5c8242008-01-24 02:22:38 -08002052 goto err_ioremap;
2053
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08002054 netdev->netdev_ops = &igb_netdev_ops;
Auke Kok9d5c8242008-01-24 02:22:38 -08002055 igb_set_ethtool_ops(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002056 netdev->watchdog_timeo = 5 * HZ;
Auke Kok9d5c8242008-01-24 02:22:38 -08002057
2058 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
2059
2060 netdev->mem_start = mmio_start;
2061 netdev->mem_end = mmio_start + mmio_len;
2062
Auke Kok9d5c8242008-01-24 02:22:38 -08002063 /* PCI config space info */
2064 hw->vendor_id = pdev->vendor;
2065 hw->device_id = pdev->device;
2066 hw->revision_id = pdev->revision;
2067 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2068 hw->subsystem_device_id = pdev->subsystem_device;
2069
Auke Kok9d5c8242008-01-24 02:22:38 -08002070 /* Copy the default MAC, PHY and NVM function pointers */
2071 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
2072 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
2073 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
2074 /* Initialize skew-specific constants */
2075 err = ei->get_invariants(hw);
2076 if (err)
Alexander Duyck450c87c2009-02-06 23:22:11 +00002077 goto err_sw_init;
Auke Kok9d5c8242008-01-24 02:22:38 -08002078
Alexander Duyck450c87c2009-02-06 23:22:11 +00002079 /* setup the private structure */
Auke Kok9d5c8242008-01-24 02:22:38 -08002080 err = igb_sw_init(adapter);
2081 if (err)
2082 goto err_sw_init;
2083
2084 igb_get_bus_info_pcie(hw);
2085
2086 hw->phy.autoneg_wait_to_complete = false;
Auke Kok9d5c8242008-01-24 02:22:38 -08002087
2088 /* Copper options */
2089 if (hw->phy.media_type == e1000_media_type_copper) {
2090 hw->phy.mdix = AUTO_ALL_MODES;
2091 hw->phy.disable_polarity_correction = false;
2092 hw->phy.ms_type = e1000_ms_hw_default;
2093 }
2094
2095 if (igb_check_reset_block(hw))
2096 dev_info(&pdev->dev,
2097 "PHY reset is blocked due to SOL/IDER session.\n");
2098
Alexander Duyck077887c2011-08-26 07:46:29 +00002099 /*
2100 * features is initialized to 0 in allocation, it might have bits
2101 * set by igb_sw_init so we should use an or instead of an
2102 * assignment.
2103 */
2104 netdev->features |= NETIF_F_SG |
2105 NETIF_F_IP_CSUM |
2106 NETIF_F_IPV6_CSUM |
2107 NETIF_F_TSO |
2108 NETIF_F_TSO6 |
2109 NETIF_F_RXHASH |
2110 NETIF_F_RXCSUM |
2111 NETIF_F_HW_VLAN_RX |
2112 NETIF_F_HW_VLAN_TX;
Michał Mirosławac52caa2011-06-08 08:38:01 +00002113
Alexander Duyck077887c2011-08-26 07:46:29 +00002114 /* copy netdev features into list of user selectable features */
2115 netdev->hw_features |= netdev->features;
Ben Greear89eaefb2012-03-06 09:41:58 +00002116 netdev->hw_features |= NETIF_F_RXALL;
Auke Kok9d5c8242008-01-24 02:22:38 -08002117
Alexander Duyck077887c2011-08-26 07:46:29 +00002118 /* set this bit last since it cannot be part of hw_features */
2119 netdev->features |= NETIF_F_HW_VLAN_FILTER;
2120
2121 netdev->vlan_features |= NETIF_F_TSO |
2122 NETIF_F_TSO6 |
2123 NETIF_F_IP_CSUM |
2124 NETIF_F_IPV6_CSUM |
2125 NETIF_F_SG;
Jeff Kirsher48f29ff2008-06-05 04:06:27 -07002126
Ben Greear6b8f0922012-03-06 09:41:53 +00002127 netdev->priv_flags |= IFF_SUPP_NOFCS;
2128
Yi Zou7b872a52010-09-22 17:57:58 +00002129 if (pci_using_dac) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002130 netdev->features |= NETIF_F_HIGHDMA;
Yi Zou7b872a52010-09-22 17:57:58 +00002131 netdev->vlan_features |= NETIF_F_HIGHDMA;
2132 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002133
Michał Mirosławac52caa2011-06-08 08:38:01 +00002134 if (hw->mac.type >= e1000_82576) {
2135 netdev->hw_features |= NETIF_F_SCTP_CSUM;
Jesse Brandeburgb9473562009-04-27 22:36:13 +00002136 netdev->features |= NETIF_F_SCTP_CSUM;
Michał Mirosławac52caa2011-06-08 08:38:01 +00002137 }
Jesse Brandeburgb9473562009-04-27 22:36:13 +00002138
Jiri Pirko01789342011-08-16 06:29:00 +00002139 netdev->priv_flags |= IFF_UNICAST_FLT;
2140
Alexander Duyck330a6d62009-10-27 23:51:35 +00002141 adapter->en_mng_pt = igb_enable_mng_pass_thru(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002142
2143 /* before reading the NVM, reset the controller to put the device in a
2144 * known good starting state */
2145 hw->mac.ops.reset_hw(hw);
2146
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002147 /*
2148 * make sure the NVM is good , i211 parts have special NVM that
2149 * doesn't contain a checksum
2150 */
2151 if (hw->mac.type != e1000_i211) {
2152 if (hw->nvm.ops.validate(hw) < 0) {
2153 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
2154 err = -EIO;
2155 goto err_eeprom;
2156 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002157 }
2158
2159 /* copy the MAC address out of the NVM */
2160 if (hw->mac.ops.read_mac_addr(hw))
2161 dev_err(&pdev->dev, "NVM Read Error\n");
2162
2163 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
Auke Kok9d5c8242008-01-24 02:22:38 -08002164
Jiri Pirkoaaeb6cd2013-01-08 01:38:26 +00002165 if (!is_valid_ether_addr(netdev->dev_addr)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002166 dev_err(&pdev->dev, "Invalid MAC Address\n");
2167 err = -EIO;
2168 goto err_eeprom;
2169 }
2170
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00002171 /* get firmware version for ethtool -i */
2172 igb_set_fw_version(adapter);
2173
Joe Perchesc061b182010-08-23 18:20:03 +00002174 setup_timer(&adapter->watchdog_timer, igb_watchdog,
Alexander Duyck0e340482009-03-20 00:17:08 +00002175 (unsigned long) adapter);
Joe Perchesc061b182010-08-23 18:20:03 +00002176 setup_timer(&adapter->phy_info_timer, igb_update_phy_info,
Alexander Duyck0e340482009-03-20 00:17:08 +00002177 (unsigned long) adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002178
2179 INIT_WORK(&adapter->reset_task, igb_reset_task);
2180 INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
2181
Alexander Duyck450c87c2009-02-06 23:22:11 +00002182 /* Initialize link properties that are user-changeable */
Auke Kok9d5c8242008-01-24 02:22:38 -08002183 adapter->fc_autoneg = true;
2184 hw->mac.autoneg = true;
2185 hw->phy.autoneg_advertised = 0x2f;
2186
Alexander Duyck0cce1192009-07-23 18:10:24 +00002187 hw->fc.requested_mode = e1000_fc_default;
2188 hw->fc.current_mode = e1000_fc_default;
Auke Kok9d5c8242008-01-24 02:22:38 -08002189
Auke Kok9d5c8242008-01-24 02:22:38 -08002190 igb_validate_mdi_setting(hw);
2191
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002192 /* By default, support wake on port A */
Alexander Duycka2cf8b62009-03-13 20:41:17 +00002193 if (hw->bus.func == 0)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002194 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2195
2196 /* Check the NVM for wake support on non-port A ports */
2197 if (hw->mac.type >= e1000_82580)
Alexander Duyck55cac242009-11-19 12:42:21 +00002198 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A +
2199 NVM_82580_LAN_FUNC_OFFSET(hw->bus.func), 1,
2200 &eeprom_data);
Alexander Duycka2cf8b62009-03-13 20:41:17 +00002201 else if (hw->bus.func == 1)
2202 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
Auke Kok9d5c8242008-01-24 02:22:38 -08002203
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002204 if (eeprom_data & IGB_EEPROM_APME)
2205 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002206
2207 /* now that we have the eeprom settings, apply the special cases where
2208 * the eeprom may be wrong or the board simply won't support wake on
2209 * lan on a particular port */
2210 switch (pdev->device) {
2211 case E1000_DEV_ID_82575GB_QUAD_COPPER:
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002212 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002213 break;
2214 case E1000_DEV_ID_82575EB_FIBER_SERDES:
Alexander Duyck2d064c02008-07-08 15:10:12 -07002215 case E1000_DEV_ID_82576_FIBER:
2216 case E1000_DEV_ID_82576_SERDES:
Auke Kok9d5c8242008-01-24 02:22:38 -08002217 /* Wake events only supported on port A for dual fiber
2218 * regardless of eeprom setting */
2219 if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002220 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002221 break;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002222 case E1000_DEV_ID_82576_QUAD_COPPER:
Stefan Assmannd5aa2252010-04-09 09:51:34 +00002223 case E1000_DEV_ID_82576_QUAD_COPPER_ET2:
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002224 /* if quad port adapter, disable WoL on all but port A */
2225 if (global_quad_port_a != 0)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002226 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002227 else
2228 adapter->flags |= IGB_FLAG_QUAD_PORT_A;
2229 /* Reset for multiple quad port adapters */
2230 if (++global_quad_port_a == 4)
2231 global_quad_port_a = 0;
2232 break;
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002233 default:
2234 /* If the device can't wake, don't set software support */
2235 if (!device_can_wakeup(&adapter->pdev->dev))
2236 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002237 }
2238
2239 /* initialize the wol settings based on the eeprom settings */
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002240 if (adapter->flags & IGB_FLAG_WOL_SUPPORTED)
2241 adapter->wol |= E1000_WUFC_MAG;
2242
2243 /* Some vendors want WoL disabled by default, but still supported */
2244 if ((hw->mac.type == e1000_i350) &&
2245 (pdev->subsystem_vendor == PCI_VENDOR_ID_HP)) {
2246 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2247 adapter->wol = 0;
2248 }
2249
2250 device_set_wakeup_enable(&adapter->pdev->dev,
2251 adapter->flags & IGB_FLAG_WOL_SUPPORTED);
Auke Kok9d5c8242008-01-24 02:22:38 -08002252
2253 /* reset the hardware with the new settings */
2254 igb_reset(adapter);
2255
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00002256 /* Init the I2C interface */
2257 err = igb_init_i2c(adapter);
2258 if (err) {
2259 dev_err(&pdev->dev, "failed to init i2c interface\n");
2260 goto err_eeprom;
2261 }
2262
Auke Kok9d5c8242008-01-24 02:22:38 -08002263 /* let the f/w know that the h/w is now under the control of the
2264 * driver. */
2265 igb_get_hw_control(adapter);
2266
Auke Kok9d5c8242008-01-24 02:22:38 -08002267 strcpy(netdev->name, "eth%d");
2268 err = register_netdev(netdev);
2269 if (err)
2270 goto err_register;
2271
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00002272 /* carrier off reporting is important to ethtool even BEFORE open */
2273 netif_carrier_off(netdev);
2274
Jeff Kirsher421e02f2008-10-17 11:08:31 -07002275#ifdef CONFIG_IGB_DCA
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08002276 if (dca_add_requester(&pdev->dev) == 0) {
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002277 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002278 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002279 igb_setup_dca(adapter);
2280 }
Alexander Duyckc5b9bd52009-10-27 23:46:01 +00002281
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002282#endif
Carolyn Wybornye4288932012-12-07 03:01:42 +00002283#ifdef CONFIG_IGB_HWMON
2284 /* Initialize the thermal sensor on i350 devices. */
2285 if (hw->mac.type == e1000_i350 && hw->bus.func == 0) {
2286 u16 ets_word;
Matthew Vick3c89f6d2012-08-10 05:40:43 +00002287
Carolyn Wybornye4288932012-12-07 03:01:42 +00002288 /*
2289 * Read the NVM to determine if this i350 device supports an
2290 * external thermal sensor.
2291 */
2292 hw->nvm.ops.read(hw, NVM_ETS_CFG, 1, &ets_word);
2293 if (ets_word != 0x0000 && ets_word != 0xFFFF)
2294 adapter->ets = true;
2295 else
2296 adapter->ets = false;
2297 if (igb_sysfs_init(adapter))
2298 dev_err(&pdev->dev,
2299 "failed to allocate sysfs resources\n");
2300 } else {
2301 adapter->ets = false;
2302 }
2303#endif
Anders Berggren673b8b72011-02-04 07:32:32 +00002304 /* do hw tstamp init after resetting */
Richard Cochran7ebae812012-03-16 10:55:37 +00002305 igb_ptp_init(adapter);
Anders Berggren673b8b72011-02-04 07:32:32 +00002306
Auke Kok9d5c8242008-01-24 02:22:38 -08002307 dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
2308 /* print bus type/speed/width info */
Johannes Berg7c510e42008-10-27 17:47:26 -07002309 dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08002310 netdev->name,
Alexander Duyck559e9c42009-10-27 23:52:50 +00002311 ((hw->bus.speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
Alexander Duyckff846f52010-04-27 01:02:40 +00002312 (hw->bus.speed == e1000_bus_speed_5000) ? "5.0Gb/s" :
Alexander Duyck559e9c42009-10-27 23:52:50 +00002313 "unknown"),
Alexander Duyck59c3de82009-03-31 20:38:00 +00002314 ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
2315 (hw->bus.width == e1000_bus_width_pcie_x2) ? "Width x2" :
2316 (hw->bus.width == e1000_bus_width_pcie_x1) ? "Width x1" :
2317 "unknown"),
Johannes Berg7c510e42008-10-27 17:47:26 -07002318 netdev->dev_addr);
Auke Kok9d5c8242008-01-24 02:22:38 -08002319
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00002320 ret_val = igb_read_part_string(hw, part_str, E1000_PBANUM_LENGTH);
2321 if (ret_val)
2322 strcpy(part_str, "Unknown");
2323 dev_info(&pdev->dev, "%s: PBA No: %s\n", netdev->name, part_str);
Auke Kok9d5c8242008-01-24 02:22:38 -08002324 dev_info(&pdev->dev,
2325 "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
2326 adapter->msix_entries ? "MSI-X" :
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002327 (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
Auke Kok9d5c8242008-01-24 02:22:38 -08002328 adapter->num_rx_queues, adapter->num_tx_queues);
Carolyn Wyborny09b068d2011-03-11 20:42:13 -08002329 switch (hw->mac.type) {
2330 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002331 case e1000_i210:
2332 case e1000_i211:
Carolyn Wyborny09b068d2011-03-11 20:42:13 -08002333 igb_set_eee_i350(hw);
2334 break;
2335 default:
2336 break;
2337 }
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002338
2339 pm_runtime_put_noidle(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002340 return 0;
2341
2342err_register:
2343 igb_release_hw_control(adapter);
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00002344 memset(&adapter->i2c_adap, 0, sizeof(adapter->i2c_adap));
Auke Kok9d5c8242008-01-24 02:22:38 -08002345err_eeprom:
2346 if (!igb_check_reset_block(hw))
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08002347 igb_reset_phy(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002348
2349 if (hw->flash_address)
2350 iounmap(hw->flash_address);
Auke Kok9d5c8242008-01-24 02:22:38 -08002351err_sw_init:
Alexander Duyck047e0032009-10-27 15:49:27 +00002352 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002353 iounmap(hw->hw_addr);
2354err_ioremap:
2355 free_netdev(netdev);
2356err_alloc_etherdev:
Alexander Duyck559e9c42009-10-27 23:52:50 +00002357 pci_release_selected_regions(pdev,
2358 pci_select_bars(pdev, IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08002359err_pci_reg:
2360err_dma:
2361 pci_disable_device(pdev);
2362 return err;
2363}
2364
Greg Rosefa44f2f2013-01-17 01:03:06 -08002365#ifdef CONFIG_PCI_IOV
2366static int igb_disable_sriov(struct pci_dev *pdev)
2367{
2368 struct net_device *netdev = pci_get_drvdata(pdev);
2369 struct igb_adapter *adapter = netdev_priv(netdev);
2370 struct e1000_hw *hw = &adapter->hw;
2371
2372 /* reclaim resources allocated to VFs */
2373 if (adapter->vf_data) {
2374 /* disable iov and allow time for transactions to clear */
2375 if (igb_vfs_are_assigned(adapter)) {
2376 dev_warn(&pdev->dev,
2377 "Cannot deallocate SR-IOV virtual functions while they are assigned - VFs will not be deallocated\n");
2378 return -EPERM;
2379 } else {
2380 pci_disable_sriov(pdev);
2381 msleep(500);
2382 }
2383
2384 kfree(adapter->vf_data);
2385 adapter->vf_data = NULL;
2386 adapter->vfs_allocated_count = 0;
2387 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
2388 wrfl();
2389 msleep(100);
2390 dev_info(&pdev->dev, "IOV Disabled\n");
2391
2392 /* Re-enable DMA Coalescing flag since IOV is turned off */
2393 adapter->flags |= IGB_FLAG_DMAC;
2394 }
2395
2396 return 0;
2397}
2398
2399static int igb_enable_sriov(struct pci_dev *pdev, int num_vfs)
2400{
2401 struct net_device *netdev = pci_get_drvdata(pdev);
2402 struct igb_adapter *adapter = netdev_priv(netdev);
2403 int old_vfs = pci_num_vf(pdev);
2404 int err = 0;
2405 int i;
2406
2407 if (!num_vfs)
2408 goto out;
2409 else if (old_vfs && old_vfs == num_vfs)
2410 goto out;
2411 else if (old_vfs && old_vfs != num_vfs)
2412 err = igb_disable_sriov(pdev);
2413
2414 if (err)
2415 goto out;
2416
2417 if (num_vfs > 7) {
2418 err = -EPERM;
2419 goto out;
2420 }
2421
2422 adapter->vfs_allocated_count = num_vfs;
2423
2424 adapter->vf_data = kcalloc(adapter->vfs_allocated_count,
2425 sizeof(struct vf_data_storage), GFP_KERNEL);
2426
2427 /* if allocation failed then we do not support SR-IOV */
2428 if (!adapter->vf_data) {
2429 adapter->vfs_allocated_count = 0;
2430 dev_err(&pdev->dev,
2431 "Unable to allocate memory for VF Data Storage\n");
2432 err = -ENOMEM;
2433 goto out;
2434 }
2435
2436 err = pci_enable_sriov(pdev, adapter->vfs_allocated_count);
2437 if (err)
2438 goto err_out;
2439
2440 dev_info(&pdev->dev, "%d VFs allocated\n",
2441 adapter->vfs_allocated_count);
2442 for (i = 0; i < adapter->vfs_allocated_count; i++)
2443 igb_vf_configure(adapter, i);
2444
2445 /* DMA Coalescing is not supported in IOV mode. */
2446 adapter->flags &= ~IGB_FLAG_DMAC;
2447 goto out;
2448
2449err_out:
2450 kfree(adapter->vf_data);
2451 adapter->vf_data = NULL;
2452 adapter->vfs_allocated_count = 0;
2453out:
2454 return err;
2455}
2456
2457#endif
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00002458/*
2459 * igb_remove_i2c - Cleanup I2C interface
2460 * @adapter: pointer to adapter structure
2461 *
2462 */
2463static void igb_remove_i2c(struct igb_adapter *adapter)
2464{
2465
2466 /* free the adapter bus structure */
2467 i2c_del_adapter(&adapter->i2c_adap);
2468}
2469
Auke Kok9d5c8242008-01-24 02:22:38 -08002470/**
2471 * igb_remove - Device Removal Routine
2472 * @pdev: PCI device information struct
2473 *
2474 * igb_remove is called by the PCI subsystem to alert the driver
2475 * that it should release a PCI device. The could be caused by a
2476 * Hot-Plug event, or because the driver is going to be removed from
2477 * memory.
2478 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002479static void igb_remove(struct pci_dev *pdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08002480{
2481 struct net_device *netdev = pci_get_drvdata(pdev);
2482 struct igb_adapter *adapter = netdev_priv(netdev);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002483 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08002484
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002485 pm_runtime_get_noresume(&pdev->dev);
Carolyn Wybornye4288932012-12-07 03:01:42 +00002486#ifdef CONFIG_IGB_HWMON
2487 igb_sysfs_exit(adapter);
2488#endif
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00002489 igb_remove_i2c(adapter);
Matthew Vicka79f4f82012-08-10 05:40:44 +00002490 igb_ptp_stop(adapter);
Tejun Heo760141a2010-12-12 16:45:14 +01002491 /*
2492 * The watchdog timer may be rescheduled, so explicitly
2493 * disable watchdog from being rescheduled.
2494 */
Auke Kok9d5c8242008-01-24 02:22:38 -08002495 set_bit(__IGB_DOWN, &adapter->state);
2496 del_timer_sync(&adapter->watchdog_timer);
2497 del_timer_sync(&adapter->phy_info_timer);
2498
Tejun Heo760141a2010-12-12 16:45:14 +01002499 cancel_work_sync(&adapter->reset_task);
2500 cancel_work_sync(&adapter->watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08002501
Jeff Kirsher421e02f2008-10-17 11:08:31 -07002502#ifdef CONFIG_IGB_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002503 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002504 dev_info(&pdev->dev, "DCA disabled\n");
2505 dca_remove_requester(&pdev->dev);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002506 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08002507 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002508 }
2509#endif
2510
Auke Kok9d5c8242008-01-24 02:22:38 -08002511 /* Release control of h/w to f/w. If f/w is AMT enabled, this
2512 * would have already happened in close and is redundant. */
2513 igb_release_hw_control(adapter);
2514
2515 unregister_netdev(netdev);
2516
Alexander Duyck047e0032009-10-27 15:49:27 +00002517 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002518
Alexander Duyck37680112009-02-19 20:40:30 -08002519#ifdef CONFIG_PCI_IOV
Greg Rosefa44f2f2013-01-17 01:03:06 -08002520 igb_disable_sriov(pdev);
Alexander Duyck37680112009-02-19 20:40:30 -08002521#endif
Alexander Duyck559e9c42009-10-27 23:52:50 +00002522
Alexander Duyck28b07592009-02-06 23:20:31 +00002523 iounmap(hw->hw_addr);
2524 if (hw->flash_address)
2525 iounmap(hw->flash_address);
Alexander Duyck559e9c42009-10-27 23:52:50 +00002526 pci_release_selected_regions(pdev,
2527 pci_select_bars(pdev, IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08002528
Carolyn Wyborny1128c752011-10-14 00:13:49 +00002529 kfree(adapter->shadow_vfta);
Auke Kok9d5c8242008-01-24 02:22:38 -08002530 free_netdev(netdev);
2531
Frans Pop19d5afd2009-10-02 10:04:12 -07002532 pci_disable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08002533
Auke Kok9d5c8242008-01-24 02:22:38 -08002534 pci_disable_device(pdev);
2535}
2536
2537/**
Alexander Duycka6b623e2009-10-27 23:47:53 +00002538 * igb_probe_vfs - Initialize vf data storage and add VFs to pci config space
2539 * @adapter: board private structure to initialize
2540 *
2541 * This function initializes the vf specific data storage and then attempts to
2542 * allocate the VFs. The reason for ordering it this way is because it is much
2543 * mor expensive time wise to disable SR-IOV than it is to allocate and free
2544 * the memory for the VFs.
2545 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002546static void igb_probe_vfs(struct igb_adapter *adapter)
Alexander Duycka6b623e2009-10-27 23:47:53 +00002547{
2548#ifdef CONFIG_PCI_IOV
2549 struct pci_dev *pdev = adapter->pdev;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002550 struct e1000_hw *hw = &adapter->hw;
Alexander Duycka6b623e2009-10-27 23:47:53 +00002551
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002552 /* Virtualization features not supported on i210 family. */
2553 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211))
2554 return;
2555
Greg Rosefa44f2f2013-01-17 01:03:06 -08002556 pci_sriov_set_totalvfs(pdev, 7);
Alex Williamsond5e51a12013-03-13 15:50:29 +00002557 igb_enable_sriov(pdev, max_vfs);
Alexander Duycka6b623e2009-10-27 23:47:53 +00002558
Alexander Duycka6b623e2009-10-27 23:47:53 +00002559#endif /* CONFIG_PCI_IOV */
2560}
2561
Greg Rosefa44f2f2013-01-17 01:03:06 -08002562static void igb_init_queue_configuration(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002563{
2564 struct e1000_hw *hw = &adapter->hw;
Matthew Vick374a5422012-05-18 04:54:58 +00002565 u32 max_rss_queues;
Auke Kok9d5c8242008-01-24 02:22:38 -08002566
Matthew Vick374a5422012-05-18 04:54:58 +00002567 /* Determine the maximum number of RSS queues supported. */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002568 switch (hw->mac.type) {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002569 case e1000_i211:
Matthew Vick374a5422012-05-18 04:54:58 +00002570 max_rss_queues = IGB_MAX_RX_QUEUES_I211;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002571 break;
Matthew Vick374a5422012-05-18 04:54:58 +00002572 case e1000_82575:
2573 case e1000_i210:
2574 max_rss_queues = IGB_MAX_RX_QUEUES_82575;
2575 break;
2576 case e1000_i350:
2577 /* I350 cannot do RSS and SR-IOV at the same time */
2578 if (!!adapter->vfs_allocated_count) {
2579 max_rss_queues = 1;
2580 break;
2581 }
2582 /* fall through */
2583 case e1000_82576:
2584 if (!!adapter->vfs_allocated_count) {
2585 max_rss_queues = 2;
2586 break;
2587 }
2588 /* fall through */
2589 case e1000_82580:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002590 default:
Matthew Vick374a5422012-05-18 04:54:58 +00002591 max_rss_queues = IGB_MAX_RX_QUEUES;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002592 break;
2593 }
Alexander Duycka99955f2009-11-12 18:37:19 +00002594
Matthew Vick374a5422012-05-18 04:54:58 +00002595 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
2596
2597 /* Determine if we need to pair queues. */
2598 switch (hw->mac.type) {
2599 case e1000_82575:
2600 case e1000_i211:
2601 /* Device supports enough interrupts without queue pairing. */
2602 break;
2603 case e1000_82576:
2604 /*
2605 * If VFs are going to be allocated with RSS queues then we
2606 * should pair the queues in order to conserve interrupts due
2607 * to limited supply.
2608 */
2609 if ((adapter->rss_queues > 1) &&
2610 (adapter->vfs_allocated_count > 6))
2611 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2612 /* fall through */
2613 case e1000_82580:
2614 case e1000_i350:
2615 case e1000_i210:
2616 default:
2617 /*
2618 * If rss_queues > half of max_rss_queues, pair the queues in
2619 * order to conserve interrupts due to limited supply.
2620 */
2621 if (adapter->rss_queues > (max_rss_queues / 2))
2622 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2623 break;
2624 }
Greg Rosefa44f2f2013-01-17 01:03:06 -08002625}
2626
2627/**
2628 * igb_sw_init - Initialize general software structures (struct igb_adapter)
2629 * @adapter: board private structure to initialize
2630 *
2631 * igb_sw_init initializes the Adapter private data structure.
2632 * Fields are initialized based on PCI device information and
2633 * OS network device settings (MTU size).
2634 **/
2635static int igb_sw_init(struct igb_adapter *adapter)
2636{
2637 struct e1000_hw *hw = &adapter->hw;
2638 struct net_device *netdev = adapter->netdev;
2639 struct pci_dev *pdev = adapter->pdev;
2640
2641 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
2642
2643 /* set default ring sizes */
2644 adapter->tx_ring_count = IGB_DEFAULT_TXD;
2645 adapter->rx_ring_count = IGB_DEFAULT_RXD;
2646
2647 /* set default ITR values */
2648 adapter->rx_itr_setting = IGB_DEFAULT_ITR;
2649 adapter->tx_itr_setting = IGB_DEFAULT_ITR;
2650
2651 /* set default work limits */
2652 adapter->tx_work_limit = IGB_DEFAULT_TX_WORK;
2653
2654 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
2655 VLAN_HLEN;
2656 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
2657
2658 spin_lock_init(&adapter->stats64_lock);
2659#ifdef CONFIG_PCI_IOV
2660 switch (hw->mac.type) {
2661 case e1000_82576:
2662 case e1000_i350:
2663 if (max_vfs > 7) {
2664 dev_warn(&pdev->dev,
2665 "Maximum of 7 VFs per PF, using max\n");
Alex Williamsond0f63ac2013-03-13 15:50:24 +00002666 max_vfs = adapter->vfs_allocated_count = 7;
Greg Rosefa44f2f2013-01-17 01:03:06 -08002667 } else
2668 adapter->vfs_allocated_count = max_vfs;
2669 if (adapter->vfs_allocated_count)
2670 dev_warn(&pdev->dev,
2671 "Enabling SR-IOV VFs using the module parameter is deprecated - please use the pci sysfs interface.\n");
2672 break;
2673 default:
2674 break;
2675 }
2676#endif /* CONFIG_PCI_IOV */
2677
2678 igb_init_queue_configuration(adapter);
Alexander Duycka99955f2009-11-12 18:37:19 +00002679
Carolyn Wyborny1128c752011-10-14 00:13:49 +00002680 /* Setup and initialize a copy of the hw vlan table array */
Joe Perchesb2adaca2013-02-03 17:43:58 +00002681 adapter->shadow_vfta = kcalloc(E1000_VLAN_FILTER_TBL_SIZE, sizeof(u32),
2682 GFP_ATOMIC);
Carolyn Wyborny1128c752011-10-14 00:13:49 +00002683
Alexander Duycka6b623e2009-10-27 23:47:53 +00002684 /* This call may decrease the number of queues */
Stefan Assmann53c7d062012-12-04 06:00:12 +00002685 if (igb_init_interrupt_scheme(adapter, true)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002686 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
2687 return -ENOMEM;
2688 }
2689
Alexander Duycka6b623e2009-10-27 23:47:53 +00002690 igb_probe_vfs(adapter);
2691
Auke Kok9d5c8242008-01-24 02:22:38 -08002692 /* Explicitly disable IRQ since the NIC can be in any state. */
2693 igb_irq_disable(adapter);
2694
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002695 if (hw->mac.type >= e1000_i350)
Carolyn Wyborny831ec0b2011-03-11 20:43:54 -08002696 adapter->flags &= ~IGB_FLAG_DMAC;
2697
Auke Kok9d5c8242008-01-24 02:22:38 -08002698 set_bit(__IGB_DOWN, &adapter->state);
2699 return 0;
2700}
2701
2702/**
2703 * igb_open - Called when a network interface is made active
2704 * @netdev: network interface device structure
2705 *
2706 * Returns 0 on success, negative value on failure
2707 *
2708 * The open entry point is called when a network interface is made
2709 * active by the system (IFF_UP). At this point all resources needed
2710 * for transmit and receive operations are allocated, the interrupt
2711 * handler is registered with the OS, the watchdog timer is started,
2712 * and the stack is notified that the interface is ready.
2713 **/
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002714static int __igb_open(struct net_device *netdev, bool resuming)
Auke Kok9d5c8242008-01-24 02:22:38 -08002715{
2716 struct igb_adapter *adapter = netdev_priv(netdev);
2717 struct e1000_hw *hw = &adapter->hw;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002718 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002719 int err;
2720 int i;
2721
2722 /* disallow open during test */
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002723 if (test_bit(__IGB_TESTING, &adapter->state)) {
2724 WARN_ON(resuming);
Auke Kok9d5c8242008-01-24 02:22:38 -08002725 return -EBUSY;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002726 }
2727
2728 if (!resuming)
2729 pm_runtime_get_sync(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002730
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00002731 netif_carrier_off(netdev);
2732
Auke Kok9d5c8242008-01-24 02:22:38 -08002733 /* allocate transmit descriptors */
2734 err = igb_setup_all_tx_resources(adapter);
2735 if (err)
2736 goto err_setup_tx;
2737
2738 /* allocate receive descriptors */
2739 err = igb_setup_all_rx_resources(adapter);
2740 if (err)
2741 goto err_setup_rx;
2742
Nick Nunley88a268c2010-02-17 01:01:59 +00002743 igb_power_up_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002744
Auke Kok9d5c8242008-01-24 02:22:38 -08002745 /* before we allocate an interrupt, we must be ready to handle it.
2746 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
2747 * as soon as we call pci_request_irq, so we have to setup our
2748 * clean_rx handler before we do so. */
2749 igb_configure(adapter);
2750
2751 err = igb_request_irq(adapter);
2752 if (err)
2753 goto err_req_irq;
2754
Alexander Duyck0c2cc022012-09-25 00:31:22 +00002755 /* Notify the stack of the actual queue counts. */
2756 err = netif_set_real_num_tx_queues(adapter->netdev,
2757 adapter->num_tx_queues);
2758 if (err)
2759 goto err_set_queues;
2760
2761 err = netif_set_real_num_rx_queues(adapter->netdev,
2762 adapter->num_rx_queues);
2763 if (err)
2764 goto err_set_queues;
2765
Auke Kok9d5c8242008-01-24 02:22:38 -08002766 /* From here on the code is the same as igb_up() */
2767 clear_bit(__IGB_DOWN, &adapter->state);
2768
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00002769 for (i = 0; i < adapter->num_q_vectors; i++)
2770 napi_enable(&(adapter->q_vector[i]->napi));
Auke Kok9d5c8242008-01-24 02:22:38 -08002771
2772 /* Clear any pending interrupts. */
2773 rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07002774
2775 igb_irq_enable(adapter);
2776
Alexander Duyckd4960302009-10-27 15:53:45 +00002777 /* notify VFs that reset has been completed */
2778 if (adapter->vfs_allocated_count) {
2779 u32 reg_data = rd32(E1000_CTRL_EXT);
2780 reg_data |= E1000_CTRL_EXT_PFRSTD;
2781 wr32(E1000_CTRL_EXT, reg_data);
2782 }
2783
Jeff Kirsherd55b53f2008-07-18 04:33:03 -07002784 netif_tx_start_all_queues(netdev);
2785
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002786 if (!resuming)
2787 pm_runtime_put(&pdev->dev);
2788
Alexander Duyck25568a52009-10-27 23:49:59 +00002789 /* start the watchdog. */
2790 hw->mac.get_link_status = 1;
2791 schedule_work(&adapter->watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08002792
2793 return 0;
2794
Alexander Duyck0c2cc022012-09-25 00:31:22 +00002795err_set_queues:
2796 igb_free_irq(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002797err_req_irq:
2798 igb_release_hw_control(adapter);
Nick Nunley88a268c2010-02-17 01:01:59 +00002799 igb_power_down_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002800 igb_free_all_rx_resources(adapter);
2801err_setup_rx:
2802 igb_free_all_tx_resources(adapter);
2803err_setup_tx:
2804 igb_reset(adapter);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002805 if (!resuming)
2806 pm_runtime_put(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002807
2808 return err;
2809}
2810
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002811static int igb_open(struct net_device *netdev)
2812{
2813 return __igb_open(netdev, false);
2814}
2815
Auke Kok9d5c8242008-01-24 02:22:38 -08002816/**
2817 * igb_close - Disables a network interface
2818 * @netdev: network interface device structure
2819 *
2820 * Returns 0, this is not allowed to fail
2821 *
2822 * The close entry point is called when an interface is de-activated
2823 * by the OS. The hardware is still under the driver's control, but
2824 * needs to be disabled. A global MAC reset is issued to stop the
2825 * hardware, and all transmit and receive resources are freed.
2826 **/
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002827static int __igb_close(struct net_device *netdev, bool suspending)
Auke Kok9d5c8242008-01-24 02:22:38 -08002828{
2829 struct igb_adapter *adapter = netdev_priv(netdev);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002830 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002831
2832 WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
Auke Kok9d5c8242008-01-24 02:22:38 -08002833
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002834 if (!suspending)
2835 pm_runtime_get_sync(&pdev->dev);
2836
2837 igb_down(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002838 igb_free_irq(adapter);
2839
2840 igb_free_all_tx_resources(adapter);
2841 igb_free_all_rx_resources(adapter);
2842
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002843 if (!suspending)
2844 pm_runtime_put_sync(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002845 return 0;
2846}
2847
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002848static int igb_close(struct net_device *netdev)
2849{
2850 return __igb_close(netdev, false);
2851}
2852
Auke Kok9d5c8242008-01-24 02:22:38 -08002853/**
2854 * igb_setup_tx_resources - allocate Tx resources (Descriptors)
Auke Kok9d5c8242008-01-24 02:22:38 -08002855 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2856 *
2857 * Return 0 on success, negative on failure
2858 **/
Alexander Duyck80785292009-10-27 15:51:47 +00002859int igb_setup_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002860{
Alexander Duyck59d71982010-04-27 13:09:25 +00002861 struct device *dev = tx_ring->dev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002862 int size;
2863
Alexander Duyck06034642011-08-26 07:44:22 +00002864 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002865
2866 tx_ring->tx_buffer_info = vzalloc(size);
Alexander Duyck06034642011-08-26 07:44:22 +00002867 if (!tx_ring->tx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08002868 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -08002869
2870 /* round up to nearest 4K */
Alexander Duyck85e8d002009-02-16 00:00:20 -08002871 tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08002872 tx_ring->size = ALIGN(tx_ring->size, 4096);
2873
Alexander Duyck5536d212012-09-25 00:31:17 +00002874 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
2875 &tx_ring->dma, GFP_KERNEL);
Auke Kok9d5c8242008-01-24 02:22:38 -08002876 if (!tx_ring->desc)
2877 goto err;
2878
Auke Kok9d5c8242008-01-24 02:22:38 -08002879 tx_ring->next_to_use = 0;
2880 tx_ring->next_to_clean = 0;
Alexander Duyck81c2fc22011-08-26 07:45:20 +00002881
Auke Kok9d5c8242008-01-24 02:22:38 -08002882 return 0;
2883
2884err:
Alexander Duyck06034642011-08-26 07:44:22 +00002885 vfree(tx_ring->tx_buffer_info);
Alexander Duyckf33005a2012-09-13 06:27:55 +00002886 tx_ring->tx_buffer_info = NULL;
2887 dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08002888 return -ENOMEM;
2889}
2890
2891/**
2892 * igb_setup_all_tx_resources - wrapper to allocate Tx resources
2893 * (Descriptors) for all queues
2894 * @adapter: board private structure
2895 *
2896 * Return 0 on success, negative on failure
2897 **/
2898static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
2899{
Alexander Duyck439705e2009-10-27 23:49:20 +00002900 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002901 int i, err = 0;
2902
2903 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00002904 err = igb_setup_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002905 if (err) {
Alexander Duyck439705e2009-10-27 23:49:20 +00002906 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08002907 "Allocation for Tx Queue %u failed\n", i);
2908 for (i--; i >= 0; i--)
Alexander Duyck3025a442010-02-17 01:02:39 +00002909 igb_free_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002910 break;
2911 }
2912 }
2913
2914 return err;
2915}
2916
2917/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002918 * igb_setup_tctl - configure the transmit control registers
2919 * @adapter: Board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08002920 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002921void igb_setup_tctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002922{
Auke Kok9d5c8242008-01-24 02:22:38 -08002923 struct e1000_hw *hw = &adapter->hw;
2924 u32 tctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08002925
Alexander Duyck85b430b2009-10-27 15:50:29 +00002926 /* disable queue 0 which is enabled by default on 82575 and 82576 */
2927 wr32(E1000_TXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08002928
2929 /* Program the Transmit Control Register */
Auke Kok9d5c8242008-01-24 02:22:38 -08002930 tctl = rd32(E1000_TCTL);
2931 tctl &= ~E1000_TCTL_CT;
2932 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
2933 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2934
2935 igb_config_collision_dist(hw);
2936
Auke Kok9d5c8242008-01-24 02:22:38 -08002937 /* Enable transmits */
2938 tctl |= E1000_TCTL_EN;
2939
2940 wr32(E1000_TCTL, tctl);
2941}
2942
2943/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002944 * igb_configure_tx_ring - Configure transmit ring after Reset
2945 * @adapter: board private structure
2946 * @ring: tx ring to configure
2947 *
2948 * Configure a transmit ring after a reset.
2949 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002950void igb_configure_tx_ring(struct igb_adapter *adapter,
2951 struct igb_ring *ring)
Alexander Duyck85b430b2009-10-27 15:50:29 +00002952{
2953 struct e1000_hw *hw = &adapter->hw;
Alexander Duycka74420e2011-08-26 07:43:27 +00002954 u32 txdctl = 0;
Alexander Duyck85b430b2009-10-27 15:50:29 +00002955 u64 tdba = ring->dma;
2956 int reg_idx = ring->reg_idx;
2957
2958 /* disable the queue */
Alexander Duycka74420e2011-08-26 07:43:27 +00002959 wr32(E1000_TXDCTL(reg_idx), 0);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002960 wrfl();
2961 mdelay(10);
2962
2963 wr32(E1000_TDLEN(reg_idx),
2964 ring->count * sizeof(union e1000_adv_tx_desc));
2965 wr32(E1000_TDBAL(reg_idx),
2966 tdba & 0x00000000ffffffffULL);
2967 wr32(E1000_TDBAH(reg_idx), tdba >> 32);
2968
Alexander Duyckfce99e32009-10-27 15:51:27 +00002969 ring->tail = hw->hw_addr + E1000_TDT(reg_idx);
Alexander Duycka74420e2011-08-26 07:43:27 +00002970 wr32(E1000_TDH(reg_idx), 0);
Alexander Duyckfce99e32009-10-27 15:51:27 +00002971 writel(0, ring->tail);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002972
2973 txdctl |= IGB_TX_PTHRESH;
2974 txdctl |= IGB_TX_HTHRESH << 8;
2975 txdctl |= IGB_TX_WTHRESH << 16;
2976
2977 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
2978 wr32(E1000_TXDCTL(reg_idx), txdctl);
2979}
2980
2981/**
2982 * igb_configure_tx - Configure transmit Unit after Reset
2983 * @adapter: board private structure
2984 *
2985 * Configure the Tx unit of the MAC after a reset.
2986 **/
2987static void igb_configure_tx(struct igb_adapter *adapter)
2988{
2989 int i;
2990
2991 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00002992 igb_configure_tx_ring(adapter, adapter->tx_ring[i]);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002993}
2994
2995/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002996 * igb_setup_rx_resources - allocate Rx resources (Descriptors)
Auke Kok9d5c8242008-01-24 02:22:38 -08002997 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2998 *
2999 * Returns 0 on success, negative on failure
3000 **/
Alexander Duyck80785292009-10-27 15:51:47 +00003001int igb_setup_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003002{
Alexander Duyck59d71982010-04-27 13:09:25 +00003003 struct device *dev = rx_ring->dev;
Alexander Duyckf33005a2012-09-13 06:27:55 +00003004 int size;
Auke Kok9d5c8242008-01-24 02:22:38 -08003005
Alexander Duyck06034642011-08-26 07:44:22 +00003006 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
Alexander Duyckf33005a2012-09-13 06:27:55 +00003007
3008 rx_ring->rx_buffer_info = vzalloc(size);
Alexander Duyck06034642011-08-26 07:44:22 +00003009 if (!rx_ring->rx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003010 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -08003011
Auke Kok9d5c8242008-01-24 02:22:38 -08003012 /* Round up to nearest 4K */
Alexander Duyckf33005a2012-09-13 06:27:55 +00003013 rx_ring->size = rx_ring->count * sizeof(union e1000_adv_rx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08003014 rx_ring->size = ALIGN(rx_ring->size, 4096);
3015
Alexander Duyck5536d212012-09-25 00:31:17 +00003016 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
3017 &rx_ring->dma, GFP_KERNEL);
Auke Kok9d5c8242008-01-24 02:22:38 -08003018 if (!rx_ring->desc)
3019 goto err;
3020
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003021 rx_ring->next_to_alloc = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003022 rx_ring->next_to_clean = 0;
3023 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003024
Auke Kok9d5c8242008-01-24 02:22:38 -08003025 return 0;
3026
3027err:
Alexander Duyck06034642011-08-26 07:44:22 +00003028 vfree(rx_ring->rx_buffer_info);
3029 rx_ring->rx_buffer_info = NULL;
Alexander Duyckf33005a2012-09-13 06:27:55 +00003030 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08003031 return -ENOMEM;
3032}
3033
3034/**
3035 * igb_setup_all_rx_resources - wrapper to allocate Rx resources
3036 * (Descriptors) for all queues
3037 * @adapter: board private structure
3038 *
3039 * Return 0 on success, negative on failure
3040 **/
3041static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
3042{
Alexander Duyck439705e2009-10-27 23:49:20 +00003043 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08003044 int i, err = 0;
3045
3046 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00003047 err = igb_setup_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003048 if (err) {
Alexander Duyck439705e2009-10-27 23:49:20 +00003049 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08003050 "Allocation for Rx Queue %u failed\n", i);
3051 for (i--; i >= 0; i--)
Alexander Duyck3025a442010-02-17 01:02:39 +00003052 igb_free_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003053 break;
3054 }
3055 }
3056
3057 return err;
3058}
3059
3060/**
Alexander Duyck06cf2662009-10-27 15:53:25 +00003061 * igb_setup_mrqc - configure the multiple receive queue control registers
3062 * @adapter: Board private structure
3063 **/
3064static void igb_setup_mrqc(struct igb_adapter *adapter)
3065{
3066 struct e1000_hw *hw = &adapter->hw;
3067 u32 mrqc, rxcsum;
Alexander Duyck797fd4b2012-09-13 06:28:11 +00003068 u32 j, num_rx_queues, shift = 0;
Alexander Duycka57fe232012-09-13 06:28:16 +00003069 static const u32 rsskey[10] = { 0xDA565A6D, 0xC20E5B25, 0x3D256741,
3070 0xB08FA343, 0xCB2BCAD0, 0xB4307BAE,
3071 0xA32DCB77, 0x0CF23080, 0x3BB7426A,
3072 0xFA01ACBE };
Alexander Duyck06cf2662009-10-27 15:53:25 +00003073
3074 /* Fill out hash function seeds */
Alexander Duycka57fe232012-09-13 06:28:16 +00003075 for (j = 0; j < 10; j++)
3076 wr32(E1000_RSSRK(j), rsskey[j]);
Alexander Duyck06cf2662009-10-27 15:53:25 +00003077
Alexander Duycka99955f2009-11-12 18:37:19 +00003078 num_rx_queues = adapter->rss_queues;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003079
Alexander Duyck797fd4b2012-09-13 06:28:11 +00003080 switch (hw->mac.type) {
3081 case e1000_82575:
3082 shift = 6;
3083 break;
3084 case e1000_82576:
3085 /* 82576 supports 2 RSS queues for SR-IOV */
3086 if (adapter->vfs_allocated_count) {
Alexander Duyck06cf2662009-10-27 15:53:25 +00003087 shift = 3;
3088 num_rx_queues = 2;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003089 }
Alexander Duyck797fd4b2012-09-13 06:28:11 +00003090 break;
3091 default:
3092 break;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003093 }
3094
Alexander Duyck797fd4b2012-09-13 06:28:11 +00003095 /*
3096 * Populate the indirection table 4 entries at a time. To do this
3097 * we are generating the results for n and n+2 and then interleaving
3098 * those with the results with n+1 and n+3.
3099 */
3100 for (j = 0; j < 32; j++) {
3101 /* first pass generates n and n+2 */
3102 u32 base = ((j * 0x00040004) + 0x00020000) * num_rx_queues;
3103 u32 reta = (base & 0x07800780) >> (7 - shift);
3104
3105 /* second pass generates n+1 and n+3 */
3106 base += 0x00010001 * num_rx_queues;
3107 reta |= (base & 0x07800780) << (1 + shift);
3108
3109 wr32(E1000_RETA(j), reta);
Alexander Duyck06cf2662009-10-27 15:53:25 +00003110 }
3111
3112 /*
3113 * Disable raw packet checksumming so that RSS hash is placed in
3114 * descriptor on writeback. No need to enable TCP/UDP/IP checksum
3115 * offloads as they are enabled by default
3116 */
3117 rxcsum = rd32(E1000_RXCSUM);
3118 rxcsum |= E1000_RXCSUM_PCSD;
3119
3120 if (adapter->hw.mac.type >= e1000_82576)
3121 /* Enable Receive Checksum Offload for SCTP */
3122 rxcsum |= E1000_RXCSUM_CRCOFL;
3123
3124 /* Don't need to set TUOFL or IPOFL, they default to 1 */
3125 wr32(E1000_RXCSUM, rxcsum);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003126
Akeem G. Abodunrin039454a2012-11-13 04:03:21 +00003127 /* Generate RSS hash based on packet types, TCP/UDP
3128 * port numbers and/or IPv4/v6 src and dst addresses
3129 */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003130 mrqc = E1000_MRQC_RSS_FIELD_IPV4 |
3131 E1000_MRQC_RSS_FIELD_IPV4_TCP |
3132 E1000_MRQC_RSS_FIELD_IPV6 |
3133 E1000_MRQC_RSS_FIELD_IPV6_TCP |
3134 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003135
Akeem G. Abodunrin039454a2012-11-13 04:03:21 +00003136 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV4_UDP)
3137 mrqc |= E1000_MRQC_RSS_FIELD_IPV4_UDP;
3138 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV6_UDP)
3139 mrqc |= E1000_MRQC_RSS_FIELD_IPV6_UDP;
3140
Alexander Duyck06cf2662009-10-27 15:53:25 +00003141 /* If VMDq is enabled then we set the appropriate mode for that, else
3142 * we default to RSS so that an RSS hash is calculated per packet even
3143 * if we are only using one queue */
3144 if (adapter->vfs_allocated_count) {
3145 if (hw->mac.type > e1000_82575) {
3146 /* Set the default pool for the PF's first queue */
3147 u32 vtctl = rd32(E1000_VT_CTL);
3148 vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
3149 E1000_VT_CTL_DISABLE_DEF_POOL);
3150 vtctl |= adapter->vfs_allocated_count <<
3151 E1000_VT_CTL_DEFAULT_POOL_SHIFT;
3152 wr32(E1000_VT_CTL, vtctl);
3153 }
Alexander Duycka99955f2009-11-12 18:37:19 +00003154 if (adapter->rss_queues > 1)
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003155 mrqc |= E1000_MRQC_ENABLE_VMDQ_RSS_2Q;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003156 else
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003157 mrqc |= E1000_MRQC_ENABLE_VMDQ;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003158 } else {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003159 if (hw->mac.type != e1000_i211)
3160 mrqc |= E1000_MRQC_ENABLE_RSS_4Q;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003161 }
3162 igb_vmm_control(adapter);
3163
Alexander Duyck06cf2662009-10-27 15:53:25 +00003164 wr32(E1000_MRQC, mrqc);
3165}
3166
3167/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003168 * igb_setup_rctl - configure the receive control registers
3169 * @adapter: Board private structure
3170 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00003171void igb_setup_rctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08003172{
3173 struct e1000_hw *hw = &adapter->hw;
3174 u32 rctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08003175
3176 rctl = rd32(E1000_RCTL);
3177
3178 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
Alexander Duyck69d728b2008-11-25 01:04:03 -08003179 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
Auke Kok9d5c8242008-01-24 02:22:38 -08003180
Alexander Duyck69d728b2008-11-25 01:04:03 -08003181 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
Alexander Duyck28b07592009-02-06 23:20:31 +00003182 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
Auke Kok9d5c8242008-01-24 02:22:38 -08003183
Auke Kok87cb7e82008-07-08 15:08:29 -07003184 /*
3185 * enable stripping of CRC. It's unlikely this will break BMC
3186 * redirection as it did with e1000. Newer features require
3187 * that the HW strips the CRC.
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003188 */
Auke Kok87cb7e82008-07-08 15:08:29 -07003189 rctl |= E1000_RCTL_SECRC;
Auke Kok9d5c8242008-01-24 02:22:38 -08003190
Alexander Duyck559e9c42009-10-27 23:52:50 +00003191 /* disable store bad packets and clear size bits. */
Alexander Duyckec54d7d2009-01-31 00:52:57 -08003192 rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
Auke Kok9d5c8242008-01-24 02:22:38 -08003193
Alexander Duyck6ec43fe2009-10-27 15:50:48 +00003194 /* enable LPE to prevent packets larger than max_frame_size */
3195 rctl |= E1000_RCTL_LPE;
Auke Kok9d5c8242008-01-24 02:22:38 -08003196
Alexander Duyck952f72a2009-10-27 15:51:07 +00003197 /* disable queue 0 to prevent tail write w/o re-config */
3198 wr32(E1000_RXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08003199
Alexander Duycke1739522009-02-19 20:39:44 -08003200 /* Attention!!! For SR-IOV PF driver operations you must enable
3201 * queue drop for all VF and PF queues to prevent head of line blocking
3202 * if an un-trusted VF does not provide descriptors to hardware.
3203 */
3204 if (adapter->vfs_allocated_count) {
Alexander Duycke1739522009-02-19 20:39:44 -08003205 /* set all queue drop enable bits */
3206 wr32(E1000_QDE, ALL_QUEUES);
Alexander Duycke1739522009-02-19 20:39:44 -08003207 }
3208
Ben Greear89eaefb2012-03-06 09:41:58 +00003209 /* This is useful for sniffing bad packets. */
3210 if (adapter->netdev->features & NETIF_F_RXALL) {
3211 /* UPE and MPE will be handled by normal PROMISC logic
3212 * in e1000e_set_rx_mode */
3213 rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
3214 E1000_RCTL_BAM | /* RX All Bcast Pkts */
3215 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
3216
3217 rctl &= ~(E1000_RCTL_VFE | /* Disable VLAN filter */
3218 E1000_RCTL_DPF | /* Allow filtered pause */
3219 E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
3220 /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
3221 * and that breaks VLANs.
3222 */
3223 }
3224
Auke Kok9d5c8242008-01-24 02:22:38 -08003225 wr32(E1000_RCTL, rctl);
3226}
3227
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003228static inline int igb_set_vf_rlpml(struct igb_adapter *adapter, int size,
3229 int vfn)
3230{
3231 struct e1000_hw *hw = &adapter->hw;
3232 u32 vmolr;
3233
3234 /* if it isn't the PF check to see if VFs are enabled and
3235 * increase the size to support vlan tags */
3236 if (vfn < adapter->vfs_allocated_count &&
3237 adapter->vf_data[vfn].vlans_enabled)
3238 size += VLAN_TAG_SIZE;
3239
3240 vmolr = rd32(E1000_VMOLR(vfn));
3241 vmolr &= ~E1000_VMOLR_RLPML_MASK;
3242 vmolr |= size | E1000_VMOLR_LPE;
3243 wr32(E1000_VMOLR(vfn), vmolr);
3244
3245 return 0;
3246}
3247
Auke Kok9d5c8242008-01-24 02:22:38 -08003248/**
Alexander Duycke1739522009-02-19 20:39:44 -08003249 * igb_rlpml_set - set maximum receive packet size
3250 * @adapter: board private structure
3251 *
3252 * Configure maximum receivable packet size.
3253 **/
3254static void igb_rlpml_set(struct igb_adapter *adapter)
3255{
Alexander Duyck153285f2011-08-26 07:43:32 +00003256 u32 max_frame_size = adapter->max_frame_size;
Alexander Duycke1739522009-02-19 20:39:44 -08003257 struct e1000_hw *hw = &adapter->hw;
3258 u16 pf_id = adapter->vfs_allocated_count;
3259
Alexander Duycke1739522009-02-19 20:39:44 -08003260 if (pf_id) {
3261 igb_set_vf_rlpml(adapter, max_frame_size, pf_id);
Alexander Duyck153285f2011-08-26 07:43:32 +00003262 /*
3263 * If we're in VMDQ or SR-IOV mode, then set global RLPML
3264 * to our max jumbo frame size, in case we need to enable
3265 * jumbo frames on one of the rings later.
3266 * This will not pass over-length frames into the default
3267 * queue because it's gated by the VMOLR.RLPML.
3268 */
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003269 max_frame_size = MAX_JUMBO_FRAME_SIZE;
Alexander Duycke1739522009-02-19 20:39:44 -08003270 }
3271
3272 wr32(E1000_RLPML, max_frame_size);
3273}
3274
Williams, Mitch A8151d292010-02-10 01:44:24 +00003275static inline void igb_set_vmolr(struct igb_adapter *adapter,
3276 int vfn, bool aupe)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003277{
3278 struct e1000_hw *hw = &adapter->hw;
3279 u32 vmolr;
3280
3281 /*
3282 * This register exists only on 82576 and newer so if we are older then
3283 * we should exit and do nothing
3284 */
3285 if (hw->mac.type < e1000_82576)
3286 return;
3287
3288 vmolr = rd32(E1000_VMOLR(vfn));
Williams, Mitch A8151d292010-02-10 01:44:24 +00003289 vmolr |= E1000_VMOLR_STRVLAN; /* Strip vlan tags */
3290 if (aupe)
3291 vmolr |= E1000_VMOLR_AUPE; /* Accept untagged packets */
3292 else
3293 vmolr &= ~(E1000_VMOLR_AUPE); /* Tagged packets ONLY */
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003294
3295 /* clear all bits that might not be set */
3296 vmolr &= ~(E1000_VMOLR_BAM | E1000_VMOLR_RSSE);
3297
Alexander Duycka99955f2009-11-12 18:37:19 +00003298 if (adapter->rss_queues > 1 && vfn == adapter->vfs_allocated_count)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003299 vmolr |= E1000_VMOLR_RSSE; /* enable RSS */
3300 /*
3301 * for VMDq only allow the VFs and pool 0 to accept broadcast and
3302 * multicast packets
3303 */
3304 if (vfn <= adapter->vfs_allocated_count)
3305 vmolr |= E1000_VMOLR_BAM; /* Accept broadcast */
3306
3307 wr32(E1000_VMOLR(vfn), vmolr);
3308}
3309
Alexander Duycke1739522009-02-19 20:39:44 -08003310/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00003311 * igb_configure_rx_ring - Configure a receive ring after Reset
3312 * @adapter: board private structure
3313 * @ring: receive ring to be configured
3314 *
3315 * Configure the Rx unit of the MAC after a reset.
3316 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00003317void igb_configure_rx_ring(struct igb_adapter *adapter,
3318 struct igb_ring *ring)
Alexander Duyck85b430b2009-10-27 15:50:29 +00003319{
3320 struct e1000_hw *hw = &adapter->hw;
3321 u64 rdba = ring->dma;
3322 int reg_idx = ring->reg_idx;
Alexander Duycka74420e2011-08-26 07:43:27 +00003323 u32 srrctl = 0, rxdctl = 0;
Alexander Duyck85b430b2009-10-27 15:50:29 +00003324
3325 /* disable the queue */
Alexander Duycka74420e2011-08-26 07:43:27 +00003326 wr32(E1000_RXDCTL(reg_idx), 0);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003327
3328 /* Set DMA base address registers */
3329 wr32(E1000_RDBAL(reg_idx),
3330 rdba & 0x00000000ffffffffULL);
3331 wr32(E1000_RDBAH(reg_idx), rdba >> 32);
3332 wr32(E1000_RDLEN(reg_idx),
3333 ring->count * sizeof(union e1000_adv_rx_desc));
3334
3335 /* initialize head and tail */
Alexander Duyckfce99e32009-10-27 15:51:27 +00003336 ring->tail = hw->hw_addr + E1000_RDT(reg_idx);
Alexander Duycka74420e2011-08-26 07:43:27 +00003337 wr32(E1000_RDH(reg_idx), 0);
Alexander Duyckfce99e32009-10-27 15:51:27 +00003338 writel(0, ring->tail);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003339
Alexander Duyck952f72a2009-10-27 15:51:07 +00003340 /* set descriptor configuration */
Alexander Duyck44390ca2011-08-26 07:43:38 +00003341 srrctl = IGB_RX_HDR_LEN << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
Alexander Duyckde78d1f2012-09-25 00:31:12 +00003342 srrctl |= IGB_RX_BUFSZ >> E1000_SRRCTL_BSIZEPKT_SHIFT;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003343 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
Alexander Duyck06218a82011-08-26 07:46:55 +00003344 if (hw->mac.type >= e1000_82580)
Nick Nunley757b77e2010-03-26 11:36:47 +00003345 srrctl |= E1000_SRRCTL_TIMESTAMP;
Nick Nunleye6bdb6f2010-02-17 01:03:38 +00003346 /* Only set Drop Enable if we are supporting multiple queues */
3347 if (adapter->vfs_allocated_count || adapter->num_rx_queues > 1)
3348 srrctl |= E1000_SRRCTL_DROP_EN;
Alexander Duyck952f72a2009-10-27 15:51:07 +00003349
3350 wr32(E1000_SRRCTL(reg_idx), srrctl);
3351
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003352 /* set filtering for VMDQ pools */
Williams, Mitch A8151d292010-02-10 01:44:24 +00003353 igb_set_vmolr(adapter, reg_idx & 0x7, true);
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003354
Alexander Duyck85b430b2009-10-27 15:50:29 +00003355 rxdctl |= IGB_RX_PTHRESH;
3356 rxdctl |= IGB_RX_HTHRESH << 8;
3357 rxdctl |= IGB_RX_WTHRESH << 16;
Alexander Duycka74420e2011-08-26 07:43:27 +00003358
3359 /* enable receive descriptor fetching */
3360 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
Alexander Duyck85b430b2009-10-27 15:50:29 +00003361 wr32(E1000_RXDCTL(reg_idx), rxdctl);
3362}
3363
Alexander Duyck74e238e2013-02-02 05:07:11 +00003364static void igb_set_rx_buffer_len(struct igb_adapter *adapter,
3365 struct igb_ring *rx_ring)
3366{
3367#define IGB_MAX_BUILD_SKB_SIZE \
3368 (SKB_WITH_OVERHEAD(IGB_RX_BUFSZ) - \
3369 (NET_SKB_PAD + NET_IP_ALIGN + IGB_TS_HDR_LEN))
3370
3371 /* set build_skb flag */
3372 if (adapter->max_frame_size <= IGB_MAX_BUILD_SKB_SIZE)
3373 set_ring_build_skb_enabled(rx_ring);
3374 else
3375 clear_ring_build_skb_enabled(rx_ring);
3376}
3377
Alexander Duyck85b430b2009-10-27 15:50:29 +00003378/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003379 * igb_configure_rx - Configure receive Unit after Reset
3380 * @adapter: board private structure
3381 *
3382 * Configure the Rx unit of the MAC after a reset.
3383 **/
3384static void igb_configure_rx(struct igb_adapter *adapter)
3385{
Hannes Eder91075842009-02-18 19:36:04 -08003386 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003387
Alexander Duyck68d480c2009-10-05 06:33:08 +00003388 /* set UTA to appropriate mode */
3389 igb_set_uta(adapter);
3390
Alexander Duyck26ad9172009-10-05 06:32:49 +00003391 /* set the correct pool for the PF default MAC address in entry 0 */
3392 igb_rar_set_qsel(adapter, adapter->hw.mac.addr, 0,
3393 adapter->vfs_allocated_count);
3394
Alexander Duyck06cf2662009-10-27 15:53:25 +00003395 /* Setup the HW Rx Head and Tail Descriptor Pointers and
3396 * the Base and Length of the Rx Descriptor Ring */
Alexander Duyck74e238e2013-02-02 05:07:11 +00003397 for (i = 0; i < adapter->num_rx_queues; i++) {
3398 struct igb_ring *rx_ring = adapter->rx_ring[i];
3399 igb_set_rx_buffer_len(adapter, rx_ring);
3400 igb_configure_rx_ring(adapter, rx_ring);
3401 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003402}
3403
3404/**
3405 * igb_free_tx_resources - Free Tx Resources per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08003406 * @tx_ring: Tx descriptor ring for a specific queue
3407 *
3408 * Free all transmit software resources
3409 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08003410void igb_free_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003411{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003412 igb_clean_tx_ring(tx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003413
Alexander Duyck06034642011-08-26 07:44:22 +00003414 vfree(tx_ring->tx_buffer_info);
3415 tx_ring->tx_buffer_info = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003416
Alexander Duyck439705e2009-10-27 23:49:20 +00003417 /* if not set, then don't free */
3418 if (!tx_ring->desc)
3419 return;
3420
Alexander Duyck59d71982010-04-27 13:09:25 +00003421 dma_free_coherent(tx_ring->dev, tx_ring->size,
3422 tx_ring->desc, tx_ring->dma);
Auke Kok9d5c8242008-01-24 02:22:38 -08003423
3424 tx_ring->desc = NULL;
3425}
3426
3427/**
3428 * igb_free_all_tx_resources - Free Tx Resources for All Queues
3429 * @adapter: board private structure
3430 *
3431 * Free all transmit software resources
3432 **/
3433static void igb_free_all_tx_resources(struct igb_adapter *adapter)
3434{
3435 int i;
3436
3437 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003438 igb_free_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003439}
3440
Alexander Duyckebe42d12011-08-26 07:45:09 +00003441void igb_unmap_and_free_tx_resource(struct igb_ring *ring,
3442 struct igb_tx_buffer *tx_buffer)
Auke Kok9d5c8242008-01-24 02:22:38 -08003443{
Alexander Duyckebe42d12011-08-26 07:45:09 +00003444 if (tx_buffer->skb) {
3445 dev_kfree_skb_any(tx_buffer->skb);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003446 if (dma_unmap_len(tx_buffer, len))
Alexander Duyckebe42d12011-08-26 07:45:09 +00003447 dma_unmap_single(ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003448 dma_unmap_addr(tx_buffer, dma),
3449 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00003450 DMA_TO_DEVICE);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003451 } else if (dma_unmap_len(tx_buffer, len)) {
Alexander Duyckebe42d12011-08-26 07:45:09 +00003452 dma_unmap_page(ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003453 dma_unmap_addr(tx_buffer, dma),
3454 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00003455 DMA_TO_DEVICE);
Alexander Duyck6366ad32009-12-02 16:47:18 +00003456 }
Alexander Duyckebe42d12011-08-26 07:45:09 +00003457 tx_buffer->next_to_watch = NULL;
3458 tx_buffer->skb = NULL;
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003459 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duyckebe42d12011-08-26 07:45:09 +00003460 /* buffer_info must be completely set up in the transmit path */
Auke Kok9d5c8242008-01-24 02:22:38 -08003461}
3462
3463/**
3464 * igb_clean_tx_ring - Free Tx Buffers
Auke Kok9d5c8242008-01-24 02:22:38 -08003465 * @tx_ring: ring to be cleaned
3466 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07003467static void igb_clean_tx_ring(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003468{
Alexander Duyck06034642011-08-26 07:44:22 +00003469 struct igb_tx_buffer *buffer_info;
Auke Kok9d5c8242008-01-24 02:22:38 -08003470 unsigned long size;
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00003471 u16 i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003472
Alexander Duyck06034642011-08-26 07:44:22 +00003473 if (!tx_ring->tx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003474 return;
3475 /* Free all the Tx ring sk_buffs */
3476
3477 for (i = 0; i < tx_ring->count; i++) {
Alexander Duyck06034642011-08-26 07:44:22 +00003478 buffer_info = &tx_ring->tx_buffer_info[i];
Alexander Duyck80785292009-10-27 15:51:47 +00003479 igb_unmap_and_free_tx_resource(tx_ring, buffer_info);
Auke Kok9d5c8242008-01-24 02:22:38 -08003480 }
3481
John Fastabenddad8a3b2012-04-23 12:22:39 +00003482 netdev_tx_reset_queue(txring_txq(tx_ring));
3483
Alexander Duyck06034642011-08-26 07:44:22 +00003484 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
3485 memset(tx_ring->tx_buffer_info, 0, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08003486
3487 /* Zero out the descriptor ring */
Auke Kok9d5c8242008-01-24 02:22:38 -08003488 memset(tx_ring->desc, 0, tx_ring->size);
3489
3490 tx_ring->next_to_use = 0;
3491 tx_ring->next_to_clean = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003492}
3493
3494/**
3495 * igb_clean_all_tx_rings - Free Tx Buffers for all queues
3496 * @adapter: board private structure
3497 **/
3498static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
3499{
3500 int i;
3501
3502 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003503 igb_clean_tx_ring(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003504}
3505
3506/**
3507 * igb_free_rx_resources - Free Rx Resources
Auke Kok9d5c8242008-01-24 02:22:38 -08003508 * @rx_ring: ring to clean the resources from
3509 *
3510 * Free all receive software resources
3511 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08003512void igb_free_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003513{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003514 igb_clean_rx_ring(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003515
Alexander Duyck06034642011-08-26 07:44:22 +00003516 vfree(rx_ring->rx_buffer_info);
3517 rx_ring->rx_buffer_info = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003518
Alexander Duyck439705e2009-10-27 23:49:20 +00003519 /* if not set, then don't free */
3520 if (!rx_ring->desc)
3521 return;
3522
Alexander Duyck59d71982010-04-27 13:09:25 +00003523 dma_free_coherent(rx_ring->dev, rx_ring->size,
3524 rx_ring->desc, rx_ring->dma);
Auke Kok9d5c8242008-01-24 02:22:38 -08003525
3526 rx_ring->desc = NULL;
3527}
3528
3529/**
3530 * igb_free_all_rx_resources - Free Rx Resources for All Queues
3531 * @adapter: board private structure
3532 *
3533 * Free all receive software resources
3534 **/
3535static void igb_free_all_rx_resources(struct igb_adapter *adapter)
3536{
3537 int i;
3538
3539 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003540 igb_free_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003541}
3542
3543/**
3544 * igb_clean_rx_ring - Free Rx Buffers per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08003545 * @rx_ring: ring to free buffers from
3546 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07003547static void igb_clean_rx_ring(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003548{
Auke Kok9d5c8242008-01-24 02:22:38 -08003549 unsigned long size;
Alexander Duyckc023cd82011-08-26 07:43:43 +00003550 u16 i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003551
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003552 if (rx_ring->skb)
3553 dev_kfree_skb(rx_ring->skb);
3554 rx_ring->skb = NULL;
3555
Alexander Duyck06034642011-08-26 07:44:22 +00003556 if (!rx_ring->rx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003557 return;
Alexander Duyck439705e2009-10-27 23:49:20 +00003558
Auke Kok9d5c8242008-01-24 02:22:38 -08003559 /* Free all the Rx ring sk_buffs */
3560 for (i = 0; i < rx_ring->count; i++) {
Alexander Duyck06034642011-08-26 07:44:22 +00003561 struct igb_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08003562
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003563 if (!buffer_info->page)
3564 continue;
3565
3566 dma_unmap_page(rx_ring->dev,
3567 buffer_info->dma,
3568 PAGE_SIZE,
3569 DMA_FROM_DEVICE);
3570 __free_page(buffer_info->page);
3571
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003572 buffer_info->page = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003573 }
3574
Alexander Duyck06034642011-08-26 07:44:22 +00003575 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
3576 memset(rx_ring->rx_buffer_info, 0, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08003577
3578 /* Zero out the descriptor ring */
3579 memset(rx_ring->desc, 0, rx_ring->size);
3580
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003581 rx_ring->next_to_alloc = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003582 rx_ring->next_to_clean = 0;
3583 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003584}
3585
3586/**
3587 * igb_clean_all_rx_rings - Free Rx Buffers for all queues
3588 * @adapter: board private structure
3589 **/
3590static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
3591{
3592 int i;
3593
3594 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003595 igb_clean_rx_ring(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003596}
3597
3598/**
3599 * igb_set_mac - Change the Ethernet Address of the NIC
3600 * @netdev: network interface device structure
3601 * @p: pointer to an address structure
3602 *
3603 * Returns 0 on success, negative on failure
3604 **/
3605static int igb_set_mac(struct net_device *netdev, void *p)
3606{
3607 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck28b07592009-02-06 23:20:31 +00003608 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08003609 struct sockaddr *addr = p;
3610
3611 if (!is_valid_ether_addr(addr->sa_data))
3612 return -EADDRNOTAVAIL;
3613
3614 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Alexander Duyck28b07592009-02-06 23:20:31 +00003615 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
Auke Kok9d5c8242008-01-24 02:22:38 -08003616
Alexander Duyck26ad9172009-10-05 06:32:49 +00003617 /* set the correct pool for the new PF MAC address in entry 0 */
3618 igb_rar_set_qsel(adapter, hw->mac.addr, 0,
3619 adapter->vfs_allocated_count);
Alexander Duycke1739522009-02-19 20:39:44 -08003620
Auke Kok9d5c8242008-01-24 02:22:38 -08003621 return 0;
3622}
3623
3624/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00003625 * igb_write_mc_addr_list - write multicast addresses to MTA
3626 * @netdev: network interface device structure
3627 *
3628 * Writes multicast address list to the MTA hash table.
3629 * Returns: -ENOMEM on failure
3630 * 0 on no addresses written
3631 * X on writing X addresses to MTA
3632 **/
3633static int igb_write_mc_addr_list(struct net_device *netdev)
3634{
3635 struct igb_adapter *adapter = netdev_priv(netdev);
3636 struct e1000_hw *hw = &adapter->hw;
Jiri Pirko22bedad32010-04-01 21:22:57 +00003637 struct netdev_hw_addr *ha;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003638 u8 *mta_list;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003639 int i;
3640
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003641 if (netdev_mc_empty(netdev)) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003642 /* nothing to program, so clear mc list */
3643 igb_update_mc_addr_list(hw, NULL, 0);
3644 igb_restore_vf_multicasts(adapter);
3645 return 0;
3646 }
3647
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003648 mta_list = kzalloc(netdev_mc_count(netdev) * 6, GFP_ATOMIC);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003649 if (!mta_list)
3650 return -ENOMEM;
3651
Alexander Duyck68d480c2009-10-05 06:33:08 +00003652 /* The shared function expects a packed array of only addresses. */
Jiri Pirko48e2f182010-02-22 09:22:26 +00003653 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00003654 netdev_for_each_mc_addr(ha, netdev)
3655 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003656
Alexander Duyck68d480c2009-10-05 06:33:08 +00003657 igb_update_mc_addr_list(hw, mta_list, i);
3658 kfree(mta_list);
3659
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003660 return netdev_mc_count(netdev);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003661}
3662
3663/**
3664 * igb_write_uc_addr_list - write unicast addresses to RAR table
3665 * @netdev: network interface device structure
3666 *
3667 * Writes unicast address list to the RAR table.
3668 * Returns: -ENOMEM on failure/insufficient address space
3669 * 0 on no addresses written
3670 * X on writing X addresses to the RAR table
3671 **/
3672static int igb_write_uc_addr_list(struct net_device *netdev)
3673{
3674 struct igb_adapter *adapter = netdev_priv(netdev);
3675 struct e1000_hw *hw = &adapter->hw;
3676 unsigned int vfn = adapter->vfs_allocated_count;
3677 unsigned int rar_entries = hw->mac.rar_entry_count - (vfn + 1);
3678 int count = 0;
3679
3680 /* return ENOMEM indicating insufficient memory for addresses */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003681 if (netdev_uc_count(netdev) > rar_entries)
Alexander Duyck68d480c2009-10-05 06:33:08 +00003682 return -ENOMEM;
3683
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003684 if (!netdev_uc_empty(netdev) && rar_entries) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003685 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003686
3687 netdev_for_each_uc_addr(ha, netdev) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003688 if (!rar_entries)
3689 break;
3690 igb_rar_set_qsel(adapter, ha->addr,
3691 rar_entries--,
3692 vfn);
3693 count++;
3694 }
3695 }
3696 /* write the addresses in reverse order to avoid write combining */
3697 for (; rar_entries > 0 ; rar_entries--) {
3698 wr32(E1000_RAH(rar_entries), 0);
3699 wr32(E1000_RAL(rar_entries), 0);
3700 }
3701 wrfl();
3702
3703 return count;
3704}
3705
3706/**
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003707 * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
Auke Kok9d5c8242008-01-24 02:22:38 -08003708 * @netdev: network interface device structure
3709 *
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003710 * The set_rx_mode entry point is called whenever the unicast or multicast
3711 * address lists or the network interface flags are updated. This routine is
3712 * responsible for configuring the hardware for proper unicast, multicast,
Auke Kok9d5c8242008-01-24 02:22:38 -08003713 * promiscuous mode, and all-multi behavior.
3714 **/
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003715static void igb_set_rx_mode(struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08003716{
3717 struct igb_adapter *adapter = netdev_priv(netdev);
3718 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003719 unsigned int vfn = adapter->vfs_allocated_count;
3720 u32 rctl, vmolr = 0;
3721 int count;
Auke Kok9d5c8242008-01-24 02:22:38 -08003722
3723 /* Check for Promiscuous and All Multicast modes */
Auke Kok9d5c8242008-01-24 02:22:38 -08003724 rctl = rd32(E1000_RCTL);
3725
Alexander Duyck68d480c2009-10-05 06:33:08 +00003726 /* clear the effected bits */
3727 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_VFE);
3728
Patrick McHardy746b9f02008-07-16 20:15:45 -07003729 if (netdev->flags & IFF_PROMISC) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003730 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003731 vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_MPME);
Patrick McHardy746b9f02008-07-16 20:15:45 -07003732 } else {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003733 if (netdev->flags & IFF_ALLMULTI) {
Patrick McHardy746b9f02008-07-16 20:15:45 -07003734 rctl |= E1000_RCTL_MPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003735 vmolr |= E1000_VMOLR_MPME;
3736 } else {
3737 /*
3738 * Write addresses to the MTA, if the attempt fails
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003739 * then we should just turn on promiscuous mode so
Alexander Duyck68d480c2009-10-05 06:33:08 +00003740 * that we can at least receive multicast traffic
3741 */
3742 count = igb_write_mc_addr_list(netdev);
3743 if (count < 0) {
3744 rctl |= E1000_RCTL_MPE;
3745 vmolr |= E1000_VMOLR_MPME;
3746 } else if (count) {
3747 vmolr |= E1000_VMOLR_ROMPE;
3748 }
3749 }
3750 /*
3751 * Write addresses to available RAR registers, if there is not
3752 * sufficient space to store all the addresses then enable
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003753 * unicast promiscuous mode
Alexander Duyck68d480c2009-10-05 06:33:08 +00003754 */
3755 count = igb_write_uc_addr_list(netdev);
3756 if (count < 0) {
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003757 rctl |= E1000_RCTL_UPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003758 vmolr |= E1000_VMOLR_ROPE;
3759 }
Patrick McHardy78ed11a2008-07-16 20:16:14 -07003760 rctl |= E1000_RCTL_VFE;
Patrick McHardy746b9f02008-07-16 20:15:45 -07003761 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003762 wr32(E1000_RCTL, rctl);
3763
Alexander Duyck68d480c2009-10-05 06:33:08 +00003764 /*
3765 * In order to support SR-IOV and eventually VMDq it is necessary to set
3766 * the VMOLR to enable the appropriate modes. Without this workaround
3767 * we will have issues with VLAN tag stripping not being done for frames
3768 * that are only arriving because we are the default pool
3769 */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003770 if ((hw->mac.type < e1000_82576) || (hw->mac.type > e1000_i350))
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003771 return;
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003772
Alexander Duyck68d480c2009-10-05 06:33:08 +00003773 vmolr |= rd32(E1000_VMOLR(vfn)) &
3774 ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE);
3775 wr32(E1000_VMOLR(vfn), vmolr);
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003776 igb_restore_vf_multicasts(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003777}
3778
Greg Rose13800462010-11-06 02:08:26 +00003779static void igb_check_wvbr(struct igb_adapter *adapter)
3780{
3781 struct e1000_hw *hw = &adapter->hw;
3782 u32 wvbr = 0;
3783
3784 switch (hw->mac.type) {
3785 case e1000_82576:
3786 case e1000_i350:
3787 if (!(wvbr = rd32(E1000_WVBR)))
3788 return;
3789 break;
3790 default:
3791 break;
3792 }
3793
3794 adapter->wvbr |= wvbr;
3795}
3796
3797#define IGB_STAGGERED_QUEUE_OFFSET 8
3798
3799static void igb_spoof_check(struct igb_adapter *adapter)
3800{
3801 int j;
3802
3803 if (!adapter->wvbr)
3804 return;
3805
3806 for(j = 0; j < adapter->vfs_allocated_count; j++) {
3807 if (adapter->wvbr & (1 << j) ||
3808 adapter->wvbr & (1 << (j + IGB_STAGGERED_QUEUE_OFFSET))) {
3809 dev_warn(&adapter->pdev->dev,
3810 "Spoof event(s) detected on VF %d\n", j);
3811 adapter->wvbr &=
3812 ~((1 << j) |
3813 (1 << (j + IGB_STAGGERED_QUEUE_OFFSET)));
3814 }
3815 }
3816}
3817
Auke Kok9d5c8242008-01-24 02:22:38 -08003818/* Need to wait a few seconds after link up to get diagnostic information from
3819 * the phy */
3820static void igb_update_phy_info(unsigned long data)
3821{
3822 struct igb_adapter *adapter = (struct igb_adapter *) data;
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08003823 igb_get_phy_info(&adapter->hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08003824}
3825
3826/**
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003827 * igb_has_link - check shared code for link and determine up/down
3828 * @adapter: pointer to driver private info
3829 **/
Nick Nunley31455352010-02-17 01:01:21 +00003830bool igb_has_link(struct igb_adapter *adapter)
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003831{
3832 struct e1000_hw *hw = &adapter->hw;
3833 bool link_active = false;
3834 s32 ret_val = 0;
3835
3836 /* get_link_status is set on LSC (link status) interrupt or
3837 * rx sequence error interrupt. get_link_status will stay
3838 * false until the e1000_check_for_link establishes link
3839 * for copper adapters ONLY
3840 */
3841 switch (hw->phy.media_type) {
3842 case e1000_media_type_copper:
3843 if (hw->mac.get_link_status) {
3844 ret_val = hw->mac.ops.check_for_link(hw);
3845 link_active = !hw->mac.get_link_status;
3846 } else {
3847 link_active = true;
3848 }
3849 break;
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003850 case e1000_media_type_internal_serdes:
3851 ret_val = hw->mac.ops.check_for_link(hw);
3852 link_active = hw->mac.serdes_has_link;
3853 break;
3854 default:
3855 case e1000_media_type_unknown:
3856 break;
3857 }
3858
3859 return link_active;
3860}
3861
Stefan Assmann563988d2011-04-05 04:27:15 +00003862static bool igb_thermal_sensor_event(struct e1000_hw *hw, u32 event)
3863{
3864 bool ret = false;
3865 u32 ctrl_ext, thstat;
3866
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003867 /* check for thermal sensor event on i350 copper only */
Stefan Assmann563988d2011-04-05 04:27:15 +00003868 if (hw->mac.type == e1000_i350) {
3869 thstat = rd32(E1000_THSTAT);
3870 ctrl_ext = rd32(E1000_CTRL_EXT);
3871
3872 if ((hw->phy.media_type == e1000_media_type_copper) &&
Akeem G. Abodunrin5c17a202013-01-29 10:15:31 +00003873 !(ctrl_ext & E1000_CTRL_EXT_LINK_MODE_SGMII))
Stefan Assmann563988d2011-04-05 04:27:15 +00003874 ret = !!(thstat & event);
Stefan Assmann563988d2011-04-05 04:27:15 +00003875 }
3876
3877 return ret;
3878}
3879
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003880/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003881 * igb_watchdog - Timer Call-back
3882 * @data: pointer to adapter cast into an unsigned long
3883 **/
3884static void igb_watchdog(unsigned long data)
3885{
3886 struct igb_adapter *adapter = (struct igb_adapter *)data;
3887 /* Do the rest outside of interrupt context */
3888 schedule_work(&adapter->watchdog_task);
3889}
3890
3891static void igb_watchdog_task(struct work_struct *work)
3892{
3893 struct igb_adapter *adapter = container_of(work,
Alexander Duyck559e9c42009-10-27 23:52:50 +00003894 struct igb_adapter,
3895 watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08003896 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08003897 struct net_device *netdev = adapter->netdev;
Stefan Assmann563988d2011-04-05 04:27:15 +00003898 u32 link;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003899 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003900
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003901 link = igb_has_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003902 if (link) {
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003903 /* Cancel scheduled suspend requests. */
3904 pm_runtime_resume(netdev->dev.parent);
3905
Auke Kok9d5c8242008-01-24 02:22:38 -08003906 if (!netif_carrier_ok(netdev)) {
3907 u32 ctrl;
Alexander Duyck330a6d62009-10-27 23:51:35 +00003908 hw->mac.ops.get_speed_and_duplex(hw,
3909 &adapter->link_speed,
3910 &adapter->link_duplex);
Auke Kok9d5c8242008-01-24 02:22:38 -08003911
3912 ctrl = rd32(E1000_CTRL);
Alexander Duyck527d47c2008-11-27 00:21:39 -08003913 /* Links status message must follow this format */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003914 printk(KERN_INFO "igb: %s NIC Link is Up %d Mbps %s "
3915 "Duplex, Flow Control: %s\n",
Alexander Duyck559e9c42009-10-27 23:52:50 +00003916 netdev->name,
3917 adapter->link_speed,
3918 adapter->link_duplex == FULL_DUPLEX ?
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003919 "Full" : "Half",
3920 (ctrl & E1000_CTRL_TFCE) &&
3921 (ctrl & E1000_CTRL_RFCE) ? "RX/TX" :
3922 (ctrl & E1000_CTRL_RFCE) ? "RX" :
3923 (ctrl & E1000_CTRL_TFCE) ? "TX" : "None");
Auke Kok9d5c8242008-01-24 02:22:38 -08003924
Stefan Assmann563988d2011-04-05 04:27:15 +00003925 /* check for thermal sensor event */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003926 if (igb_thermal_sensor_event(hw,
3927 E1000_THSTAT_LINK_THROTTLE)) {
3928 netdev_info(netdev, "The network adapter link "
3929 "speed was downshifted because it "
3930 "overheated\n");
Carolyn Wyborny7ef5ed12011-03-12 08:59:47 +00003931 }
Stefan Assmann563988d2011-04-05 04:27:15 +00003932
Emil Tantilovd07f3e32010-03-23 18:34:57 +00003933 /* adjust timeout factor according to speed/duplex */
Auke Kok9d5c8242008-01-24 02:22:38 -08003934 adapter->tx_timeout_factor = 1;
3935 switch (adapter->link_speed) {
3936 case SPEED_10:
Auke Kok9d5c8242008-01-24 02:22:38 -08003937 adapter->tx_timeout_factor = 14;
3938 break;
3939 case SPEED_100:
Auke Kok9d5c8242008-01-24 02:22:38 -08003940 /* maybe add some timeout factor ? */
3941 break;
3942 }
3943
3944 netif_carrier_on(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08003945
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003946 igb_ping_all_vfs(adapter);
Lior Levy17dc5662011-02-08 02:28:46 +00003947 igb_check_vf_rate_limit(adapter);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003948
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003949 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08003950 if (!test_bit(__IGB_DOWN, &adapter->state))
3951 mod_timer(&adapter->phy_info_timer,
3952 round_jiffies(jiffies + 2 * HZ));
3953 }
3954 } else {
3955 if (netif_carrier_ok(netdev)) {
3956 adapter->link_speed = 0;
3957 adapter->link_duplex = 0;
Stefan Assmann563988d2011-04-05 04:27:15 +00003958
3959 /* check for thermal sensor event */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003960 if (igb_thermal_sensor_event(hw,
3961 E1000_THSTAT_PWR_DOWN)) {
3962 netdev_err(netdev, "The network adapter was "
3963 "stopped because it overheated\n");
Carolyn Wyborny7ef5ed12011-03-12 08:59:47 +00003964 }
Stefan Assmann563988d2011-04-05 04:27:15 +00003965
Alexander Duyck527d47c2008-11-27 00:21:39 -08003966 /* Links status message must follow this format */
3967 printk(KERN_INFO "igb: %s NIC Link is Down\n",
3968 netdev->name);
Auke Kok9d5c8242008-01-24 02:22:38 -08003969 netif_carrier_off(netdev);
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003970
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003971 igb_ping_all_vfs(adapter);
3972
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003973 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08003974 if (!test_bit(__IGB_DOWN, &adapter->state))
3975 mod_timer(&adapter->phy_info_timer,
3976 round_jiffies(jiffies + 2 * HZ));
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003977
3978 pm_schedule_suspend(netdev->dev.parent,
3979 MSEC_PER_SEC * 5);
Auke Kok9d5c8242008-01-24 02:22:38 -08003980 }
3981 }
3982
Eric Dumazet12dcd862010-10-15 17:27:10 +00003983 spin_lock(&adapter->stats64_lock);
3984 igb_update_stats(adapter, &adapter->stats64);
3985 spin_unlock(&adapter->stats64_lock);
Auke Kok9d5c8242008-01-24 02:22:38 -08003986
Alexander Duyckdbabb062009-11-12 18:38:16 +00003987 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00003988 struct igb_ring *tx_ring = adapter->tx_ring[i];
Alexander Duyckdbabb062009-11-12 18:38:16 +00003989 if (!netif_carrier_ok(netdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003990 /* We've lost link, so the controller stops DMA,
3991 * but we've got queued Tx work that's never going
3992 * to get done, so reset controller to flush Tx.
3993 * (Do the reset outside of interrupt context). */
Alexander Duyckdbabb062009-11-12 18:38:16 +00003994 if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) {
3995 adapter->tx_timeout_count++;
3996 schedule_work(&adapter->reset_task);
3997 /* return immediately since reset is imminent */
3998 return;
3999 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004000 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004001
Alexander Duyckdbabb062009-11-12 18:38:16 +00004002 /* Force detection of hung controller every watchdog period */
Alexander Duyck6d095fa2011-08-26 07:46:19 +00004003 set_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
Alexander Duyckdbabb062009-11-12 18:38:16 +00004004 }
Alexander Duyckf7ba2052009-10-27 23:48:51 +00004005
Auke Kok9d5c8242008-01-24 02:22:38 -08004006 /* Cause software interrupt to ensure rx ring is cleaned */
Alexander Duyck7a6ea552008-08-26 04:25:03 -07004007 if (adapter->msix_entries) {
Alexander Duyck047e0032009-10-27 15:49:27 +00004008 u32 eics = 0;
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00004009 for (i = 0; i < adapter->num_q_vectors; i++)
4010 eics |= adapter->q_vector[i]->eims_value;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07004011 wr32(E1000_EICS, eics);
4012 } else {
4013 wr32(E1000_ICS, E1000_ICS_RXDMT0);
4014 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004015
Greg Rose13800462010-11-06 02:08:26 +00004016 igb_spoof_check(adapter);
Matthew Vickfc580752012-12-13 07:20:35 +00004017 igb_ptp_rx_hang(adapter);
Greg Rose13800462010-11-06 02:08:26 +00004018
Auke Kok9d5c8242008-01-24 02:22:38 -08004019 /* Reset the timer */
4020 if (!test_bit(__IGB_DOWN, &adapter->state))
4021 mod_timer(&adapter->watchdog_timer,
4022 round_jiffies(jiffies + 2 * HZ));
4023}
4024
4025enum latency_range {
4026 lowest_latency = 0,
4027 low_latency = 1,
4028 bulk_latency = 2,
4029 latency_invalid = 255
4030};
4031
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004032/**
4033 * igb_update_ring_itr - update the dynamic ITR value based on packet size
4034 *
4035 * Stores a new ITR value based on strictly on packet size. This
4036 * algorithm is less sophisticated than that used in igb_update_itr,
4037 * due to the difficulty of synchronizing statistics across multiple
Stefan Weileef35c22010-08-06 21:11:15 +02004038 * receive rings. The divisors and thresholds used by this function
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004039 * were determined based on theoretical maximum wire speed and testing
4040 * data, in order to minimize response time while increasing bulk
4041 * throughput.
4042 * This functionality is controlled by the InterruptThrottleRate module
4043 * parameter (see igb_param.c)
4044 * NOTE: This function is called only when operating in a multiqueue
4045 * receive environment.
Alexander Duyck047e0032009-10-27 15:49:27 +00004046 * @q_vector: pointer to q_vector
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004047 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00004048static void igb_update_ring_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08004049{
Alexander Duyck047e0032009-10-27 15:49:27 +00004050 int new_val = q_vector->itr_val;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004051 int avg_wire_size = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +00004052 struct igb_adapter *adapter = q_vector->adapter;
Eric Dumazet12dcd862010-10-15 17:27:10 +00004053 unsigned int packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08004054
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004055 /* For non-gigabit speeds, just fix the interrupt rate at 4000
4056 * ints/sec - ITR timer value of 120 ticks.
4057 */
4058 if (adapter->link_speed != SPEED_1000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004059 new_val = IGB_4K_ITR;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004060 goto set_itr_val;
4061 }
Alexander Duyck047e0032009-10-27 15:49:27 +00004062
Alexander Duyck0ba82992011-08-26 07:45:47 +00004063 packets = q_vector->rx.total_packets;
4064 if (packets)
4065 avg_wire_size = q_vector->rx.total_bytes / packets;
Eric Dumazet12dcd862010-10-15 17:27:10 +00004066
Alexander Duyck0ba82992011-08-26 07:45:47 +00004067 packets = q_vector->tx.total_packets;
4068 if (packets)
4069 avg_wire_size = max_t(u32, avg_wire_size,
4070 q_vector->tx.total_bytes / packets);
Alexander Duyck047e0032009-10-27 15:49:27 +00004071
4072 /* if avg_wire_size isn't set no work was done */
4073 if (!avg_wire_size)
4074 goto clear_counts;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004075
4076 /* Add 24 bytes to size to account for CRC, preamble, and gap */
4077 avg_wire_size += 24;
4078
4079 /* Don't starve jumbo frames */
4080 avg_wire_size = min(avg_wire_size, 3000);
4081
4082 /* Give a little boost to mid-size frames */
4083 if ((avg_wire_size > 300) && (avg_wire_size < 1200))
4084 new_val = avg_wire_size / 3;
4085 else
4086 new_val = avg_wire_size / 2;
4087
Alexander Duyck0ba82992011-08-26 07:45:47 +00004088 /* conservative mode (itr 3) eliminates the lowest_latency setting */
4089 if (new_val < IGB_20K_ITR &&
4090 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
4091 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
4092 new_val = IGB_20K_ITR;
Nick Nunleyabe1c362010-02-17 01:03:19 +00004093
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004094set_itr_val:
Alexander Duyck047e0032009-10-27 15:49:27 +00004095 if (new_val != q_vector->itr_val) {
4096 q_vector->itr_val = new_val;
4097 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08004098 }
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004099clear_counts:
Alexander Duyck0ba82992011-08-26 07:45:47 +00004100 q_vector->rx.total_bytes = 0;
4101 q_vector->rx.total_packets = 0;
4102 q_vector->tx.total_bytes = 0;
4103 q_vector->tx.total_packets = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004104}
4105
4106/**
4107 * igb_update_itr - update the dynamic ITR value based on statistics
4108 * Stores a new ITR value based on packets and byte
4109 * counts during the last interrupt. The advantage of per interrupt
4110 * computation is faster updates and more accurate ITR for the current
4111 * traffic pattern. Constants in this function were computed
4112 * based on theoretical maximum wire speed and thresholds were set based
4113 * on testing data as well as attempting to minimize response time
4114 * while increasing bulk throughput.
4115 * this functionality is controlled by the InterruptThrottleRate module
4116 * parameter (see igb_param.c)
4117 * NOTE: These calculations are only valid when operating in a single-
4118 * queue environment.
Alexander Duyck0ba82992011-08-26 07:45:47 +00004119 * @q_vector: pointer to q_vector
4120 * @ring_container: ring info to update the itr for
Auke Kok9d5c8242008-01-24 02:22:38 -08004121 **/
Alexander Duyck0ba82992011-08-26 07:45:47 +00004122static void igb_update_itr(struct igb_q_vector *q_vector,
4123 struct igb_ring_container *ring_container)
Auke Kok9d5c8242008-01-24 02:22:38 -08004124{
Alexander Duyck0ba82992011-08-26 07:45:47 +00004125 unsigned int packets = ring_container->total_packets;
4126 unsigned int bytes = ring_container->total_bytes;
4127 u8 itrval = ring_container->itr;
Auke Kok9d5c8242008-01-24 02:22:38 -08004128
Alexander Duyck0ba82992011-08-26 07:45:47 +00004129 /* no packets, exit with status unchanged */
Auke Kok9d5c8242008-01-24 02:22:38 -08004130 if (packets == 0)
Alexander Duyck0ba82992011-08-26 07:45:47 +00004131 return;
Auke Kok9d5c8242008-01-24 02:22:38 -08004132
Alexander Duyck0ba82992011-08-26 07:45:47 +00004133 switch (itrval) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004134 case lowest_latency:
4135 /* handle TSO and jumbo frames */
4136 if (bytes/packets > 8000)
Alexander Duyck0ba82992011-08-26 07:45:47 +00004137 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004138 else if ((packets < 5) && (bytes > 512))
Alexander Duyck0ba82992011-08-26 07:45:47 +00004139 itrval = low_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004140 break;
4141 case low_latency: /* 50 usec aka 20000 ints/s */
4142 if (bytes > 10000) {
4143 /* this if handles the TSO accounting */
4144 if (bytes/packets > 8000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004145 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004146 } else if ((packets < 10) || ((bytes/packets) > 1200)) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004147 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004148 } else if ((packets > 35)) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004149 itrval = lowest_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004150 }
4151 } else if (bytes/packets > 2000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004152 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004153 } else if (packets <= 2 && bytes < 512) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004154 itrval = lowest_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004155 }
4156 break;
4157 case bulk_latency: /* 250 usec aka 4000 ints/s */
4158 if (bytes > 25000) {
4159 if (packets > 35)
Alexander Duyck0ba82992011-08-26 07:45:47 +00004160 itrval = low_latency;
Alexander Duyck1e5c3d22009-02-12 18:17:21 +00004161 } else if (bytes < 1500) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004162 itrval = low_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004163 }
4164 break;
4165 }
4166
Alexander Duyck0ba82992011-08-26 07:45:47 +00004167 /* clear work counters since we have the values we need */
4168 ring_container->total_bytes = 0;
4169 ring_container->total_packets = 0;
4170
4171 /* write updated itr to ring container */
4172 ring_container->itr = itrval;
Auke Kok9d5c8242008-01-24 02:22:38 -08004173}
4174
Alexander Duyck0ba82992011-08-26 07:45:47 +00004175static void igb_set_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08004176{
Alexander Duyck0ba82992011-08-26 07:45:47 +00004177 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck047e0032009-10-27 15:49:27 +00004178 u32 new_itr = q_vector->itr_val;
Alexander Duyck0ba82992011-08-26 07:45:47 +00004179 u8 current_itr = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004180
4181 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
4182 if (adapter->link_speed != SPEED_1000) {
4183 current_itr = 0;
Alexander Duyck0ba82992011-08-26 07:45:47 +00004184 new_itr = IGB_4K_ITR;
Auke Kok9d5c8242008-01-24 02:22:38 -08004185 goto set_itr_now;
4186 }
4187
Alexander Duyck0ba82992011-08-26 07:45:47 +00004188 igb_update_itr(q_vector, &q_vector->tx);
4189 igb_update_itr(q_vector, &q_vector->rx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004190
Alexander Duyck0ba82992011-08-26 07:45:47 +00004191 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
Auke Kok9d5c8242008-01-24 02:22:38 -08004192
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004193 /* conservative mode (itr 3) eliminates the lowest_latency setting */
Alexander Duyck0ba82992011-08-26 07:45:47 +00004194 if (current_itr == lowest_latency &&
4195 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
4196 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004197 current_itr = low_latency;
4198
Auke Kok9d5c8242008-01-24 02:22:38 -08004199 switch (current_itr) {
4200 /* counts and packets in update_itr are dependent on these numbers */
4201 case lowest_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00004202 new_itr = IGB_70K_ITR; /* 70,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08004203 break;
4204 case low_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00004205 new_itr = IGB_20K_ITR; /* 20,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08004206 break;
4207 case bulk_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00004208 new_itr = IGB_4K_ITR; /* 4,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08004209 break;
4210 default:
4211 break;
4212 }
4213
4214set_itr_now:
Alexander Duyck047e0032009-10-27 15:49:27 +00004215 if (new_itr != q_vector->itr_val) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004216 /* this attempts to bias the interrupt rate towards Bulk
4217 * by adding intermediate steps when interrupt rate is
4218 * increasing */
Alexander Duyck047e0032009-10-27 15:49:27 +00004219 new_itr = new_itr > q_vector->itr_val ?
4220 max((new_itr * q_vector->itr_val) /
4221 (new_itr + (q_vector->itr_val >> 2)),
Alexander Duyck0ba82992011-08-26 07:45:47 +00004222 new_itr) :
Auke Kok9d5c8242008-01-24 02:22:38 -08004223 new_itr;
4224 /* Don't write the value here; it resets the adapter's
4225 * internal timer, and causes us to delay far longer than
4226 * we should between interrupts. Instead, we write the ITR
4227 * value at the beginning of the next interrupt so the timing
4228 * ends up being correct.
4229 */
Alexander Duyck047e0032009-10-27 15:49:27 +00004230 q_vector->itr_val = new_itr;
4231 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08004232 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004233}
4234
Stephen Hemmingerc50b52a2012-01-18 22:13:26 +00004235static void igb_tx_ctxtdesc(struct igb_ring *tx_ring, u32 vlan_macip_lens,
4236 u32 type_tucmd, u32 mss_l4len_idx)
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004237{
4238 struct e1000_adv_tx_context_desc *context_desc;
4239 u16 i = tx_ring->next_to_use;
4240
4241 context_desc = IGB_TX_CTXTDESC(tx_ring, i);
4242
4243 i++;
4244 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
4245
4246 /* set bits to identify this as an advanced context descriptor */
4247 type_tucmd |= E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
4248
4249 /* For 82575, context index must be unique per ring. */
Alexander Duyck866cff02011-08-26 07:45:36 +00004250 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004251 mss_l4len_idx |= tx_ring->reg_idx << 4;
4252
4253 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
4254 context_desc->seqnum_seed = 0;
4255 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
4256 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
4257}
4258
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004259static int igb_tso(struct igb_ring *tx_ring,
4260 struct igb_tx_buffer *first,
4261 u8 *hdr_len)
Auke Kok9d5c8242008-01-24 02:22:38 -08004262{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004263 struct sk_buff *skb = first->skb;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004264 u32 vlan_macip_lens, type_tucmd;
4265 u32 mss_l4len_idx, l4len;
4266
Alexander Duycked6aa102012-11-13 04:03:22 +00004267 if (skb->ip_summed != CHECKSUM_PARTIAL)
4268 return 0;
4269
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004270 if (!skb_is_gso(skb))
4271 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004272
4273 if (skb_header_cloned(skb)) {
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004274 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004275 if (err)
4276 return err;
4277 }
4278
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004279 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
4280 type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP;
Auke Kok9d5c8242008-01-24 02:22:38 -08004281
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004282 if (first->protocol == __constant_htons(ETH_P_IP)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004283 struct iphdr *iph = ip_hdr(skb);
4284 iph->tot_len = 0;
4285 iph->check = 0;
4286 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
4287 iph->daddr, 0,
4288 IPPROTO_TCP,
4289 0);
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004290 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004291 first->tx_flags |= IGB_TX_FLAGS_TSO |
4292 IGB_TX_FLAGS_CSUM |
4293 IGB_TX_FLAGS_IPV4;
Sridhar Samudrala8e1e8a42010-01-23 02:02:21 -08004294 } else if (skb_is_gso_v6(skb)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004295 ipv6_hdr(skb)->payload_len = 0;
4296 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4297 &ipv6_hdr(skb)->daddr,
4298 0, IPPROTO_TCP, 0);
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004299 first->tx_flags |= IGB_TX_FLAGS_TSO |
4300 IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08004301 }
4302
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004303 /* compute header lengths */
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004304 l4len = tcp_hdrlen(skb);
4305 *hdr_len = skb_transport_offset(skb) + l4len;
Auke Kok9d5c8242008-01-24 02:22:38 -08004306
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004307 /* update gso size and bytecount with header size */
4308 first->gso_segs = skb_shinfo(skb)->gso_segs;
4309 first->bytecount += (first->gso_segs - 1) * *hdr_len;
4310
Auke Kok9d5c8242008-01-24 02:22:38 -08004311 /* MSS L4LEN IDX */
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004312 mss_l4len_idx = l4len << E1000_ADVTXD_L4LEN_SHIFT;
4313 mss_l4len_idx |= skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT;
Auke Kok9d5c8242008-01-24 02:22:38 -08004314
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004315 /* VLAN MACLEN IPLEN */
4316 vlan_macip_lens = skb_network_header_len(skb);
4317 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004318 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
Auke Kok9d5c8242008-01-24 02:22:38 -08004319
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004320 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004321
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004322 return 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08004323}
4324
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004325static void igb_tx_csum(struct igb_ring *tx_ring, struct igb_tx_buffer *first)
Auke Kok9d5c8242008-01-24 02:22:38 -08004326{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004327 struct sk_buff *skb = first->skb;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004328 u32 vlan_macip_lens = 0;
4329 u32 mss_l4len_idx = 0;
4330 u32 type_tucmd = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004331
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004332 if (skb->ip_summed != CHECKSUM_PARTIAL) {
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004333 if (!(first->tx_flags & IGB_TX_FLAGS_VLAN))
4334 return;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004335 } else {
4336 u8 l4_hdr = 0;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004337 switch (first->protocol) {
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004338 case __constant_htons(ETH_P_IP):
4339 vlan_macip_lens |= skb_network_header_len(skb);
4340 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
4341 l4_hdr = ip_hdr(skb)->protocol;
4342 break;
4343 case __constant_htons(ETH_P_IPV6):
4344 vlan_macip_lens |= skb_network_header_len(skb);
4345 l4_hdr = ipv6_hdr(skb)->nexthdr;
4346 break;
4347 default:
4348 if (unlikely(net_ratelimit())) {
4349 dev_warn(tx_ring->dev,
4350 "partial checksum but proto=%x!\n",
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004351 first->protocol);
Arthur Jonesfa4a7ef2009-03-21 16:55:07 -07004352 }
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004353 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08004354 }
4355
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004356 switch (l4_hdr) {
4357 case IPPROTO_TCP:
4358 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
4359 mss_l4len_idx = tcp_hdrlen(skb) <<
4360 E1000_ADVTXD_L4LEN_SHIFT;
4361 break;
4362 case IPPROTO_SCTP:
4363 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
4364 mss_l4len_idx = sizeof(struct sctphdr) <<
4365 E1000_ADVTXD_L4LEN_SHIFT;
4366 break;
4367 case IPPROTO_UDP:
4368 mss_l4len_idx = sizeof(struct udphdr) <<
4369 E1000_ADVTXD_L4LEN_SHIFT;
4370 break;
4371 default:
4372 if (unlikely(net_ratelimit())) {
4373 dev_warn(tx_ring->dev,
4374 "partial checksum but l4 proto=%x!\n",
4375 l4_hdr);
4376 }
4377 break;
4378 }
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004379
4380 /* update TX checksum flag */
4381 first->tx_flags |= IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08004382 }
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004383
4384 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004385 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004386
4387 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004388}
4389
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004390#define IGB_SET_FLAG(_input, _flag, _result) \
4391 ((_flag <= _result) ? \
4392 ((u32)(_input & _flag) * (_result / _flag)) : \
4393 ((u32)(_input & _flag) / (_flag / _result)))
4394
4395static u32 igb_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
Alexander Duycke032afc2011-08-26 07:44:48 +00004396{
4397 /* set type for advanced descriptor with frame checksum insertion */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004398 u32 cmd_type = E1000_ADVTXD_DTYP_DATA |
4399 E1000_ADVTXD_DCMD_DEXT |
4400 E1000_ADVTXD_DCMD_IFCS;
Alexander Duycke032afc2011-08-26 07:44:48 +00004401
4402 /* set HW vlan bit if vlan is present */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004403 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_VLAN,
4404 (E1000_ADVTXD_DCMD_VLE));
Alexander Duycke032afc2011-08-26 07:44:48 +00004405
4406 /* set segmentation bits for TSO */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004407 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSO,
4408 (E1000_ADVTXD_DCMD_TSE));
4409
4410 /* set timestamp bit if present */
4411 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSTAMP,
4412 (E1000_ADVTXD_MAC_TSTAMP));
4413
4414 /* insert frame checksum */
4415 cmd_type ^= IGB_SET_FLAG(skb->no_fcs, 1, E1000_ADVTXD_DCMD_IFCS);
Alexander Duycke032afc2011-08-26 07:44:48 +00004416
4417 return cmd_type;
4418}
4419
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004420static void igb_tx_olinfo_status(struct igb_ring *tx_ring,
4421 union e1000_adv_tx_desc *tx_desc,
4422 u32 tx_flags, unsigned int paylen)
Alexander Duycke032afc2011-08-26 07:44:48 +00004423{
4424 u32 olinfo_status = paylen << E1000_ADVTXD_PAYLEN_SHIFT;
4425
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004426 /* 82575 requires a unique index per ring */
4427 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
Alexander Duycke032afc2011-08-26 07:44:48 +00004428 olinfo_status |= tx_ring->reg_idx << 4;
4429
4430 /* insert L4 checksum */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004431 olinfo_status |= IGB_SET_FLAG(tx_flags,
4432 IGB_TX_FLAGS_CSUM,
4433 (E1000_TXD_POPTS_TXSM << 8));
Alexander Duycke032afc2011-08-26 07:44:48 +00004434
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004435 /* insert IPv4 checksum */
4436 olinfo_status |= IGB_SET_FLAG(tx_flags,
4437 IGB_TX_FLAGS_IPV4,
4438 (E1000_TXD_POPTS_IXSM << 8));
Alexander Duycke032afc2011-08-26 07:44:48 +00004439
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004440 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
Alexander Duycke032afc2011-08-26 07:44:48 +00004441}
4442
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004443static void igb_tx_map(struct igb_ring *tx_ring,
4444 struct igb_tx_buffer *first,
Alexander Duyckebe42d12011-08-26 07:45:09 +00004445 const u8 hdr_len)
Auke Kok9d5c8242008-01-24 02:22:38 -08004446{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004447 struct sk_buff *skb = first->skb;
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004448 struct igb_tx_buffer *tx_buffer;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004449 union e1000_adv_tx_desc *tx_desc;
Alexander Duyck80d07592012-11-13 04:03:24 +00004450 struct skb_frag_struct *frag;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004451 dma_addr_t dma;
Alexander Duyck80d07592012-11-13 04:03:24 +00004452 unsigned int data_len, size;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004453 u32 tx_flags = first->tx_flags;
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004454 u32 cmd_type = igb_tx_cmd_type(skb, tx_flags);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004455 u16 i = tx_ring->next_to_use;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004456
4457 tx_desc = IGB_TX_DESC(tx_ring, i);
4458
Alexander Duyck80d07592012-11-13 04:03:24 +00004459 igb_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
4460
4461 size = skb_headlen(skb);
4462 data_len = skb->data_len;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004463
4464 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08004465
Alexander Duyck80d07592012-11-13 04:03:24 +00004466 tx_buffer = first;
Alexander Duyck2bbfebe2011-08-26 07:44:59 +00004467
Alexander Duyck80d07592012-11-13 04:03:24 +00004468 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
4469 if (dma_mapping_error(tx_ring->dev, dma))
4470 goto dma_error;
4471
4472 /* record length, and DMA address */
4473 dma_unmap_len_set(tx_buffer, len, size);
4474 dma_unmap_addr_set(tx_buffer, dma, dma);
4475
4476 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4477
Alexander Duyckebe42d12011-08-26 07:45:09 +00004478 while (unlikely(size > IGB_MAX_DATA_PER_TXD)) {
4479 tx_desc->read.cmd_type_len =
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004480 cpu_to_le32(cmd_type ^ IGB_MAX_DATA_PER_TXD);
Auke Kok9d5c8242008-01-24 02:22:38 -08004481
Alexander Duyckebe42d12011-08-26 07:45:09 +00004482 i++;
4483 tx_desc++;
4484 if (i == tx_ring->count) {
4485 tx_desc = IGB_TX_DESC(tx_ring, 0);
4486 i = 0;
4487 }
Alexander Duyck80d07592012-11-13 04:03:24 +00004488 tx_desc->read.olinfo_status = 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004489
4490 dma += IGB_MAX_DATA_PER_TXD;
4491 size -= IGB_MAX_DATA_PER_TXD;
4492
Alexander Duyckebe42d12011-08-26 07:45:09 +00004493 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4494 }
4495
4496 if (likely(!data_len))
4497 break;
4498
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004499 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004500
Alexander Duyck65689fe2009-03-20 00:17:43 +00004501 i++;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004502 tx_desc++;
4503 if (i == tx_ring->count) {
4504 tx_desc = IGB_TX_DESC(tx_ring, 0);
Alexander Duyck65689fe2009-03-20 00:17:43 +00004505 i = 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004506 }
Alexander Duyck80d07592012-11-13 04:03:24 +00004507 tx_desc->read.olinfo_status = 0;
Alexander Duyck65689fe2009-03-20 00:17:43 +00004508
Eric Dumazet9e903e02011-10-18 21:00:24 +00004509 size = skb_frag_size(frag);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004510 data_len -= size;
4511
4512 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
Alexander Duyck80d07592012-11-13 04:03:24 +00004513 size, DMA_TO_DEVICE);
Alexander Duyck6366ad32009-12-02 16:47:18 +00004514
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004515 tx_buffer = &tx_ring->tx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08004516 }
4517
Alexander Duyckebe42d12011-08-26 07:45:09 +00004518 /* write last descriptor with RS and EOP bits */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004519 cmd_type |= size | IGB_TXD_DCMD;
4520 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
Alexander Duyck8542db02011-08-26 07:44:43 +00004521
Alexander Duyck80d07592012-11-13 04:03:24 +00004522 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
4523
Alexander Duyck8542db02011-08-26 07:44:43 +00004524 /* set the timestamp */
4525 first->time_stamp = jiffies;
4526
Alexander Duyckebe42d12011-08-26 07:45:09 +00004527 /*
4528 * Force memory writes to complete before letting h/w know there
4529 * are new descriptors to fetch. (Only applicable for weak-ordered
4530 * memory model archs, such as IA-64).
4531 *
4532 * We also need this memory barrier to make certain all of the
4533 * status bits have been updated before next_to_watch is written.
4534 */
Auke Kok9d5c8242008-01-24 02:22:38 -08004535 wmb();
4536
Alexander Duyckebe42d12011-08-26 07:45:09 +00004537 /* set next_to_watch value indicating a packet is present */
4538 first->next_to_watch = tx_desc;
4539
4540 i++;
4541 if (i == tx_ring->count)
4542 i = 0;
4543
Auke Kok9d5c8242008-01-24 02:22:38 -08004544 tx_ring->next_to_use = i;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004545
Alexander Duyckfce99e32009-10-27 15:51:27 +00004546 writel(i, tx_ring->tail);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004547
Auke Kok9d5c8242008-01-24 02:22:38 -08004548 /* we need this if more than one processor can write to our tail
4549 * at a time, it syncronizes IO on IA64/Altix systems */
4550 mmiowb();
Alexander Duyckebe42d12011-08-26 07:45:09 +00004551
4552 return;
4553
4554dma_error:
4555 dev_err(tx_ring->dev, "TX DMA map failed\n");
4556
4557 /* clear dma mappings for failed tx_buffer_info map */
4558 for (;;) {
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004559 tx_buffer = &tx_ring->tx_buffer_info[i];
4560 igb_unmap_and_free_tx_resource(tx_ring, tx_buffer);
4561 if (tx_buffer == first)
Alexander Duyckebe42d12011-08-26 07:45:09 +00004562 break;
4563 if (i == 0)
4564 i = tx_ring->count;
4565 i--;
4566 }
4567
4568 tx_ring->next_to_use = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08004569}
4570
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00004571static int __igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004572{
Alexander Duycke694e962009-10-27 15:53:06 +00004573 struct net_device *netdev = tx_ring->netdev;
4574
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004575 netif_stop_subqueue(netdev, tx_ring->queue_index);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004576
Auke Kok9d5c8242008-01-24 02:22:38 -08004577 /* Herbert's original patch had:
4578 * smp_mb__after_netif_stop_queue();
4579 * but since that doesn't exist yet, just open code it. */
4580 smp_mb();
4581
4582 /* We need to check again in a case another CPU has just
4583 * made room available. */
Alexander Duyckc493ea42009-03-20 00:16:50 +00004584 if (igb_desc_unused(tx_ring) < size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004585 return -EBUSY;
4586
4587 /* A reprieve! */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004588 netif_wake_subqueue(netdev, tx_ring->queue_index);
Eric Dumazet12dcd862010-10-15 17:27:10 +00004589
4590 u64_stats_update_begin(&tx_ring->tx_syncp2);
4591 tx_ring->tx_stats.restart_queue2++;
4592 u64_stats_update_end(&tx_ring->tx_syncp2);
4593
Auke Kok9d5c8242008-01-24 02:22:38 -08004594 return 0;
4595}
4596
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00004597static inline int igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004598{
Alexander Duyckc493ea42009-03-20 00:16:50 +00004599 if (igb_desc_unused(tx_ring) >= size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004600 return 0;
Alexander Duycke694e962009-10-27 15:53:06 +00004601 return __igb_maybe_stop_tx(tx_ring, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08004602}
4603
Alexander Duyckcd392f52011-08-26 07:43:59 +00004604netdev_tx_t igb_xmit_frame_ring(struct sk_buff *skb,
4605 struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08004606{
Alexander Duyck8542db02011-08-26 07:44:43 +00004607 struct igb_tx_buffer *first;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004608 int tso;
Nick Nunley91d4ee32010-02-17 01:04:56 +00004609 u32 tx_flags = 0;
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00004610 u16 count = TXD_USE_COUNT(skb_headlen(skb));
Alexander Duyck31f6adb2011-08-26 07:44:53 +00004611 __be16 protocol = vlan_get_protocol(skb);
Nick Nunley91d4ee32010-02-17 01:04:56 +00004612 u8 hdr_len = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004613
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00004614 /* need: 1 descriptor per page * PAGE_SIZE/IGB_MAX_DATA_PER_TXD,
4615 * + 1 desc for skb_headlen/IGB_MAX_DATA_PER_TXD,
Auke Kok9d5c8242008-01-24 02:22:38 -08004616 * + 2 desc gap to keep tail from touching head,
Auke Kok9d5c8242008-01-24 02:22:38 -08004617 * + 1 desc for context descriptor,
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00004618 * otherwise try next time
4619 */
4620 if (NETDEV_FRAG_PAGE_MAX_SIZE > IGB_MAX_DATA_PER_TXD) {
4621 unsigned short f;
4622 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
4623 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
4624 } else {
4625 count += skb_shinfo(skb)->nr_frags;
4626 }
4627
4628 if (igb_maybe_stop_tx(tx_ring, count + 3)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004629 /* this is a hard error */
Auke Kok9d5c8242008-01-24 02:22:38 -08004630 return NETDEV_TX_BUSY;
4631 }
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004632
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004633 /* record the location of the first descriptor for this packet */
4634 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
4635 first->skb = skb;
4636 first->bytecount = skb->len;
4637 first->gso_segs = 1;
4638
Matthew Vickb66e2392012-12-13 07:20:33 +00004639 skb_tx_timestamp(skb);
4640
Alexander Duyckb646c222013-02-07 08:55:46 +00004641 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
4642 struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
Matthew Vick1f6e8172012-08-18 07:26:33 +00004643
Alexander Duyckb646c222013-02-07 08:55:46 +00004644 if (!(adapter->ptp_tx_skb)) {
4645 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4646 tx_flags |= IGB_TX_FLAGS_TSTAMP;
4647
4648 adapter->ptp_tx_skb = skb_get(skb);
4649 adapter->ptp_tx_start = jiffies;
4650 if (adapter->hw.mac.type == e1000_82576)
4651 schedule_work(&adapter->ptp_tx_work);
4652 }
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004653 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004654
Jesse Grosseab6d182010-10-20 13:56:03 +00004655 if (vlan_tx_tag_present(skb)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004656 tx_flags |= IGB_TX_FLAGS_VLAN;
4657 tx_flags |= (vlan_tx_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
4658 }
4659
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004660 /* record initial flags and protocol */
4661 first->tx_flags = tx_flags;
4662 first->protocol = protocol;
Alexander Duyckcdfd01fc2009-10-27 23:50:57 +00004663
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004664 tso = igb_tso(tx_ring, first, &hdr_len);
4665 if (tso < 0)
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004666 goto out_drop;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004667 else if (!tso)
4668 igb_tx_csum(tx_ring, first);
Auke Kok9d5c8242008-01-24 02:22:38 -08004669
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004670 igb_tx_map(tx_ring, first, hdr_len);
Alexander Duyck85ad76b2009-10-27 15:52:46 +00004671
4672 /* Make sure there is space in the ring for the next send. */
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00004673 igb_maybe_stop_tx(tx_ring, DESC_NEEDED);
Alexander Duyck85ad76b2009-10-27 15:52:46 +00004674
Auke Kok9d5c8242008-01-24 02:22:38 -08004675 return NETDEV_TX_OK;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004676
4677out_drop:
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004678 igb_unmap_and_free_tx_resource(tx_ring, first);
4679
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004680 return NETDEV_TX_OK;
Auke Kok9d5c8242008-01-24 02:22:38 -08004681}
4682
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004683static inline struct igb_ring *igb_tx_queue_mapping(struct igb_adapter *adapter,
4684 struct sk_buff *skb)
4685{
4686 unsigned int r_idx = skb->queue_mapping;
4687
4688 if (r_idx >= adapter->num_tx_queues)
4689 r_idx = r_idx % adapter->num_tx_queues;
4690
4691 return adapter->tx_ring[r_idx];
4692}
4693
Alexander Duyckcd392f52011-08-26 07:43:59 +00004694static netdev_tx_t igb_xmit_frame(struct sk_buff *skb,
4695 struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08004696{
4697 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckb1a436c2009-10-27 15:54:43 +00004698
4699 if (test_bit(__IGB_DOWN, &adapter->state)) {
4700 dev_kfree_skb_any(skb);
4701 return NETDEV_TX_OK;
4702 }
4703
4704 if (skb->len <= 0) {
4705 dev_kfree_skb_any(skb);
4706 return NETDEV_TX_OK;
4707 }
4708
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004709 /*
4710 * The minimum packet size with TCTL.PSP set is 17 so pad the skb
4711 * in order to meet this minimum size requirement.
4712 */
Tushar Daveea5ceea2012-09-14 03:43:43 +00004713 if (unlikely(skb->len < 17)) {
4714 if (skb_pad(skb, 17 - skb->len))
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004715 return NETDEV_TX_OK;
4716 skb->len = 17;
Tushar Daveea5ceea2012-09-14 03:43:43 +00004717 skb_set_tail_pointer(skb, 17);
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004718 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004719
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004720 return igb_xmit_frame_ring(skb, igb_tx_queue_mapping(adapter, skb));
Auke Kok9d5c8242008-01-24 02:22:38 -08004721}
4722
4723/**
4724 * igb_tx_timeout - Respond to a Tx Hang
4725 * @netdev: network interface device structure
4726 **/
4727static void igb_tx_timeout(struct net_device *netdev)
4728{
4729 struct igb_adapter *adapter = netdev_priv(netdev);
4730 struct e1000_hw *hw = &adapter->hw;
4731
4732 /* Do the reset outside of interrupt context */
4733 adapter->tx_timeout_count++;
Alexander Duyckf7ba2052009-10-27 23:48:51 +00004734
Alexander Duyck06218a82011-08-26 07:46:55 +00004735 if (hw->mac.type >= e1000_82580)
Alexander Duyck55cac242009-11-19 12:42:21 +00004736 hw->dev_spec._82575.global_device_reset = true;
4737
Auke Kok9d5c8242008-01-24 02:22:38 -08004738 schedule_work(&adapter->reset_task);
Alexander Duyck265de402009-02-06 23:22:52 +00004739 wr32(E1000_EICS,
4740 (adapter->eims_enable_mask & ~adapter->eims_other));
Auke Kok9d5c8242008-01-24 02:22:38 -08004741}
4742
4743static void igb_reset_task(struct work_struct *work)
4744{
4745 struct igb_adapter *adapter;
4746 adapter = container_of(work, struct igb_adapter, reset_task);
4747
Taku Izumic97ec422010-04-27 14:39:30 +00004748 igb_dump(adapter);
4749 netdev_err(adapter->netdev, "Reset adapter\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004750 igb_reinit_locked(adapter);
4751}
4752
4753/**
Eric Dumazet12dcd862010-10-15 17:27:10 +00004754 * igb_get_stats64 - Get System Network Statistics
Auke Kok9d5c8242008-01-24 02:22:38 -08004755 * @netdev: network interface device structure
Eric Dumazet12dcd862010-10-15 17:27:10 +00004756 * @stats: rtnl_link_stats64 pointer
Auke Kok9d5c8242008-01-24 02:22:38 -08004757 *
Auke Kok9d5c8242008-01-24 02:22:38 -08004758 **/
Eric Dumazet12dcd862010-10-15 17:27:10 +00004759static struct rtnl_link_stats64 *igb_get_stats64(struct net_device *netdev,
4760 struct rtnl_link_stats64 *stats)
Auke Kok9d5c8242008-01-24 02:22:38 -08004761{
Eric Dumazet12dcd862010-10-15 17:27:10 +00004762 struct igb_adapter *adapter = netdev_priv(netdev);
4763
4764 spin_lock(&adapter->stats64_lock);
4765 igb_update_stats(adapter, &adapter->stats64);
4766 memcpy(stats, &adapter->stats64, sizeof(*stats));
4767 spin_unlock(&adapter->stats64_lock);
4768
4769 return stats;
Auke Kok9d5c8242008-01-24 02:22:38 -08004770}
4771
4772/**
4773 * igb_change_mtu - Change the Maximum Transfer Unit
4774 * @netdev: network interface device structure
4775 * @new_mtu: new value for maximum frame size
4776 *
4777 * Returns 0 on success, negative on failure
4778 **/
4779static int igb_change_mtu(struct net_device *netdev, int new_mtu)
4780{
4781 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck090b1792009-10-27 23:51:55 +00004782 struct pci_dev *pdev = adapter->pdev;
Alexander Duyck153285f2011-08-26 07:43:32 +00004783 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Auke Kok9d5c8242008-01-24 02:22:38 -08004784
Alexander Duyckc809d222009-10-27 23:52:13 +00004785 if ((new_mtu < 68) || (max_frame > MAX_JUMBO_FRAME_SIZE)) {
Alexander Duyck090b1792009-10-27 23:51:55 +00004786 dev_err(&pdev->dev, "Invalid MTU setting\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004787 return -EINVAL;
4788 }
4789
Alexander Duyck153285f2011-08-26 07:43:32 +00004790#define MAX_STD_JUMBO_FRAME_SIZE 9238
Auke Kok9d5c8242008-01-24 02:22:38 -08004791 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
Alexander Duyck090b1792009-10-27 23:51:55 +00004792 dev_err(&pdev->dev, "MTU > 9216 not supported.\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004793 return -EINVAL;
4794 }
4795
4796 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
4797 msleep(1);
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004798
Auke Kok9d5c8242008-01-24 02:22:38 -08004799 /* igb_down has a dependency on max_frame_size */
4800 adapter->max_frame_size = max_frame;
Alexander Duyck559e9c42009-10-27 23:52:50 +00004801
Alexander Duyck4c844852009-10-27 15:52:07 +00004802 if (netif_running(netdev))
4803 igb_down(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08004804
Alexander Duyck090b1792009-10-27 23:51:55 +00004805 dev_info(&pdev->dev, "changing MTU from %d to %d\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08004806 netdev->mtu, new_mtu);
4807 netdev->mtu = new_mtu;
4808
4809 if (netif_running(netdev))
4810 igb_up(adapter);
4811 else
4812 igb_reset(adapter);
4813
4814 clear_bit(__IGB_RESETTING, &adapter->state);
4815
4816 return 0;
4817}
4818
4819/**
4820 * igb_update_stats - Update the board statistics counters
4821 * @adapter: board private structure
4822 **/
4823
Eric Dumazet12dcd862010-10-15 17:27:10 +00004824void igb_update_stats(struct igb_adapter *adapter,
4825 struct rtnl_link_stats64 *net_stats)
Auke Kok9d5c8242008-01-24 02:22:38 -08004826{
4827 struct e1000_hw *hw = &adapter->hw;
4828 struct pci_dev *pdev = adapter->pdev;
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004829 u32 reg, mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004830 u16 phy_tmp;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004831 int i;
4832 u64 bytes, packets;
Eric Dumazet12dcd862010-10-15 17:27:10 +00004833 unsigned int start;
4834 u64 _bytes, _packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08004835
4836#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
4837
4838 /*
4839 * Prevent stats update while adapter is being reset, or if the pci
4840 * connection is down.
4841 */
4842 if (adapter->link_speed == 0)
4843 return;
4844 if (pci_channel_offline(pdev))
4845 return;
4846
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004847 bytes = 0;
4848 packets = 0;
4849 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyckae1c07a2012-08-08 05:23:22 +00004850 u32 rqdpc = rd32(E1000_RQDPC(i));
Alexander Duyck3025a442010-02-17 01:02:39 +00004851 struct igb_ring *ring = adapter->rx_ring[i];
Eric Dumazet12dcd862010-10-15 17:27:10 +00004852
Alexander Duyckae1c07a2012-08-08 05:23:22 +00004853 if (rqdpc) {
4854 ring->rx_stats.drops += rqdpc;
4855 net_stats->rx_fifo_errors += rqdpc;
4856 }
Eric Dumazet12dcd862010-10-15 17:27:10 +00004857
4858 do {
4859 start = u64_stats_fetch_begin_bh(&ring->rx_syncp);
4860 _bytes = ring->rx_stats.bytes;
4861 _packets = ring->rx_stats.packets;
4862 } while (u64_stats_fetch_retry_bh(&ring->rx_syncp, start));
4863 bytes += _bytes;
4864 packets += _packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004865 }
4866
Alexander Duyck128e45e2009-11-12 18:37:38 +00004867 net_stats->rx_bytes = bytes;
4868 net_stats->rx_packets = packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004869
4870 bytes = 0;
4871 packets = 0;
4872 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00004873 struct igb_ring *ring = adapter->tx_ring[i];
Eric Dumazet12dcd862010-10-15 17:27:10 +00004874 do {
4875 start = u64_stats_fetch_begin_bh(&ring->tx_syncp);
4876 _bytes = ring->tx_stats.bytes;
4877 _packets = ring->tx_stats.packets;
4878 } while (u64_stats_fetch_retry_bh(&ring->tx_syncp, start));
4879 bytes += _bytes;
4880 packets += _packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004881 }
Alexander Duyck128e45e2009-11-12 18:37:38 +00004882 net_stats->tx_bytes = bytes;
4883 net_stats->tx_packets = packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004884
4885 /* read stats registers */
Auke Kok9d5c8242008-01-24 02:22:38 -08004886 adapter->stats.crcerrs += rd32(E1000_CRCERRS);
4887 adapter->stats.gprc += rd32(E1000_GPRC);
4888 adapter->stats.gorc += rd32(E1000_GORCL);
4889 rd32(E1000_GORCH); /* clear GORCL */
4890 adapter->stats.bprc += rd32(E1000_BPRC);
4891 adapter->stats.mprc += rd32(E1000_MPRC);
4892 adapter->stats.roc += rd32(E1000_ROC);
4893
4894 adapter->stats.prc64 += rd32(E1000_PRC64);
4895 adapter->stats.prc127 += rd32(E1000_PRC127);
4896 adapter->stats.prc255 += rd32(E1000_PRC255);
4897 adapter->stats.prc511 += rd32(E1000_PRC511);
4898 adapter->stats.prc1023 += rd32(E1000_PRC1023);
4899 adapter->stats.prc1522 += rd32(E1000_PRC1522);
4900 adapter->stats.symerrs += rd32(E1000_SYMERRS);
4901 adapter->stats.sec += rd32(E1000_SEC);
4902
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004903 mpc = rd32(E1000_MPC);
4904 adapter->stats.mpc += mpc;
4905 net_stats->rx_fifo_errors += mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004906 adapter->stats.scc += rd32(E1000_SCC);
4907 adapter->stats.ecol += rd32(E1000_ECOL);
4908 adapter->stats.mcc += rd32(E1000_MCC);
4909 adapter->stats.latecol += rd32(E1000_LATECOL);
4910 adapter->stats.dc += rd32(E1000_DC);
4911 adapter->stats.rlec += rd32(E1000_RLEC);
4912 adapter->stats.xonrxc += rd32(E1000_XONRXC);
4913 adapter->stats.xontxc += rd32(E1000_XONTXC);
4914 adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
4915 adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
4916 adapter->stats.fcruc += rd32(E1000_FCRUC);
4917 adapter->stats.gptc += rd32(E1000_GPTC);
4918 adapter->stats.gotc += rd32(E1000_GOTCL);
4919 rd32(E1000_GOTCH); /* clear GOTCL */
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004920 adapter->stats.rnbc += rd32(E1000_RNBC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004921 adapter->stats.ruc += rd32(E1000_RUC);
4922 adapter->stats.rfc += rd32(E1000_RFC);
4923 adapter->stats.rjc += rd32(E1000_RJC);
4924 adapter->stats.tor += rd32(E1000_TORH);
4925 adapter->stats.tot += rd32(E1000_TOTH);
4926 adapter->stats.tpr += rd32(E1000_TPR);
4927
4928 adapter->stats.ptc64 += rd32(E1000_PTC64);
4929 adapter->stats.ptc127 += rd32(E1000_PTC127);
4930 adapter->stats.ptc255 += rd32(E1000_PTC255);
4931 adapter->stats.ptc511 += rd32(E1000_PTC511);
4932 adapter->stats.ptc1023 += rd32(E1000_PTC1023);
4933 adapter->stats.ptc1522 += rd32(E1000_PTC1522);
4934
4935 adapter->stats.mptc += rd32(E1000_MPTC);
4936 adapter->stats.bptc += rd32(E1000_BPTC);
4937
Nick Nunley2d0b0f62010-02-17 01:02:59 +00004938 adapter->stats.tpt += rd32(E1000_TPT);
4939 adapter->stats.colc += rd32(E1000_COLC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004940
4941 adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
Nick Nunley43915c7c2010-02-17 01:03:58 +00004942 /* read internal phy specific stats */
4943 reg = rd32(E1000_CTRL_EXT);
4944 if (!(reg & E1000_CTRL_EXT_LINK_MODE_MASK)) {
4945 adapter->stats.rxerrc += rd32(E1000_RXERRC);
Carolyn Wyborny3dbdf962012-09-12 04:36:24 +00004946
4947 /* this stat has invalid values on i210/i211 */
4948 if ((hw->mac.type != e1000_i210) &&
4949 (hw->mac.type != e1000_i211))
4950 adapter->stats.tncrs += rd32(E1000_TNCRS);
Nick Nunley43915c7c2010-02-17 01:03:58 +00004951 }
4952
Auke Kok9d5c8242008-01-24 02:22:38 -08004953 adapter->stats.tsctc += rd32(E1000_TSCTC);
4954 adapter->stats.tsctfc += rd32(E1000_TSCTFC);
4955
4956 adapter->stats.iac += rd32(E1000_IAC);
4957 adapter->stats.icrxoc += rd32(E1000_ICRXOC);
4958 adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
4959 adapter->stats.icrxatc += rd32(E1000_ICRXATC);
4960 adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
4961 adapter->stats.ictxatc += rd32(E1000_ICTXATC);
4962 adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
4963 adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
4964 adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
4965
4966 /* Fill out the OS statistics structure */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004967 net_stats->multicast = adapter->stats.mprc;
4968 net_stats->collisions = adapter->stats.colc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004969
4970 /* Rx Errors */
4971
4972 /* RLEC on some newer hardware can be incorrect so build
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00004973 * our own version based on RUC and ROC */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004974 net_stats->rx_errors = adapter->stats.rxerrc +
Auke Kok9d5c8242008-01-24 02:22:38 -08004975 adapter->stats.crcerrs + adapter->stats.algnerrc +
4976 adapter->stats.ruc + adapter->stats.roc +
4977 adapter->stats.cexterr;
Alexander Duyck128e45e2009-11-12 18:37:38 +00004978 net_stats->rx_length_errors = adapter->stats.ruc +
4979 adapter->stats.roc;
4980 net_stats->rx_crc_errors = adapter->stats.crcerrs;
4981 net_stats->rx_frame_errors = adapter->stats.algnerrc;
4982 net_stats->rx_missed_errors = adapter->stats.mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004983
4984 /* Tx Errors */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004985 net_stats->tx_errors = adapter->stats.ecol +
4986 adapter->stats.latecol;
4987 net_stats->tx_aborted_errors = adapter->stats.ecol;
4988 net_stats->tx_window_errors = adapter->stats.latecol;
4989 net_stats->tx_carrier_errors = adapter->stats.tncrs;
Auke Kok9d5c8242008-01-24 02:22:38 -08004990
4991 /* Tx Dropped needs to be maintained elsewhere */
4992
4993 /* Phy Stats */
4994 if (hw->phy.media_type == e1000_media_type_copper) {
4995 if ((adapter->link_speed == SPEED_1000) &&
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004996 (!igb_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004997 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
4998 adapter->phy_stats.idle_errors += phy_tmp;
4999 }
5000 }
5001
5002 /* Management Stats */
5003 adapter->stats.mgptc += rd32(E1000_MGTPTC);
5004 adapter->stats.mgprc += rd32(E1000_MGTPRC);
5005 adapter->stats.mgpdc += rd32(E1000_MGTPDC);
Carolyn Wyborny0a915b92011-02-26 07:42:37 +00005006
5007 /* OS2BMC Stats */
5008 reg = rd32(E1000_MANC);
5009 if (reg & E1000_MANC_EN_BMC2OS) {
5010 adapter->stats.o2bgptc += rd32(E1000_O2BGPTC);
5011 adapter->stats.o2bspc += rd32(E1000_O2BSPC);
5012 adapter->stats.b2ospc += rd32(E1000_B2OSPC);
5013 adapter->stats.b2ogprc += rd32(E1000_B2OGPRC);
5014 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005015}
5016
Auke Kok9d5c8242008-01-24 02:22:38 -08005017static irqreturn_t igb_msix_other(int irq, void *data)
5018{
Alexander Duyck047e0032009-10-27 15:49:27 +00005019 struct igb_adapter *adapter = data;
Auke Kok9d5c8242008-01-24 02:22:38 -08005020 struct e1000_hw *hw = &adapter->hw;
PJ Waskiewicz844290e2008-06-27 11:00:39 -07005021 u32 icr = rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07005022 /* reading ICR causes bit 31 of EICR to be cleared */
Alexander Duyckdda0e082009-02-06 23:19:08 +00005023
Alexander Duyck7f081d42010-01-07 17:41:00 +00005024 if (icr & E1000_ICR_DRSTA)
5025 schedule_work(&adapter->reset_task);
5026
Alexander Duyck047e0032009-10-27 15:49:27 +00005027 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00005028 /* HW is reporting DMA is out of sync */
5029 adapter->stats.doosync++;
Greg Rose13800462010-11-06 02:08:26 +00005030 /* The DMA Out of Sync is also indication of a spoof event
5031 * in IOV mode. Check the Wrong VM Behavior register to
5032 * see if it is really a spoof event. */
5033 igb_check_wvbr(adapter);
Alexander Duyckdda0e082009-02-06 23:19:08 +00005034 }
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00005035
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005036 /* Check for a mailbox event */
5037 if (icr & E1000_ICR_VMMB)
5038 igb_msg_task(adapter);
5039
5040 if (icr & E1000_ICR_LSC) {
5041 hw->mac.get_link_status = 1;
5042 /* guard against interrupt when we're going down */
5043 if (!test_bit(__IGB_DOWN, &adapter->state))
5044 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5045 }
5046
Matthew Vick1f6e8172012-08-18 07:26:33 +00005047 if (icr & E1000_ICR_TS) {
5048 u32 tsicr = rd32(E1000_TSICR);
5049
5050 if (tsicr & E1000_TSICR_TXTS) {
5051 /* acknowledge the interrupt */
5052 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5053 /* retrieve hardware timestamp */
5054 schedule_work(&adapter->ptp_tx_work);
5055 }
5056 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00005057
PJ Waskiewicz844290e2008-06-27 11:00:39 -07005058 wr32(E1000_EIMS, adapter->eims_other);
Auke Kok9d5c8242008-01-24 02:22:38 -08005059
5060 return IRQ_HANDLED;
5061}
5062
Alexander Duyck047e0032009-10-27 15:49:27 +00005063static void igb_write_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08005064{
Alexander Duyck26b39272010-02-17 01:00:41 +00005065 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck047e0032009-10-27 15:49:27 +00005066 u32 itr_val = q_vector->itr_val & 0x7FFC;
Auke Kok9d5c8242008-01-24 02:22:38 -08005067
Alexander Duyck047e0032009-10-27 15:49:27 +00005068 if (!q_vector->set_itr)
5069 return;
Alexander Duyck73cd78f2009-02-12 18:16:59 +00005070
Alexander Duyck047e0032009-10-27 15:49:27 +00005071 if (!itr_val)
5072 itr_val = 0x4;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005073
Alexander Duyck26b39272010-02-17 01:00:41 +00005074 if (adapter->hw.mac.type == e1000_82575)
5075 itr_val |= itr_val << 16;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005076 else
Alexander Duyck0ba82992011-08-26 07:45:47 +00005077 itr_val |= E1000_EITR_CNT_IGNR;
Alexander Duyck047e0032009-10-27 15:49:27 +00005078
5079 writel(itr_val, q_vector->itr_register);
5080 q_vector->set_itr = 0;
5081}
5082
5083static irqreturn_t igb_msix_ring(int irq, void *data)
5084{
5085 struct igb_q_vector *q_vector = data;
5086
5087 /* Write the ITR value calculated from the previous interrupt. */
5088 igb_write_itr(q_vector);
5089
5090 napi_schedule(&q_vector->napi);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005091
Auke Kok9d5c8242008-01-24 02:22:38 -08005092 return IRQ_HANDLED;
5093}
5094
Jeff Kirsher421e02f2008-10-17 11:08:31 -07005095#ifdef CONFIG_IGB_DCA
Alexander Duyck6a050042012-09-25 00:31:27 +00005096static void igb_update_tx_dca(struct igb_adapter *adapter,
5097 struct igb_ring *tx_ring,
5098 int cpu)
5099{
5100 struct e1000_hw *hw = &adapter->hw;
5101 u32 txctrl = dca3_get_tag(tx_ring->dev, cpu);
5102
5103 if (hw->mac.type != e1000_82575)
5104 txctrl <<= E1000_DCA_TXCTRL_CPUID_SHIFT;
5105
5106 /*
5107 * We can enable relaxed ordering for reads, but not writes when
5108 * DCA is enabled. This is due to a known issue in some chipsets
5109 * which will cause the DCA tag to be cleared.
5110 */
5111 txctrl |= E1000_DCA_TXCTRL_DESC_RRO_EN |
5112 E1000_DCA_TXCTRL_DATA_RRO_EN |
5113 E1000_DCA_TXCTRL_DESC_DCA_EN;
5114
5115 wr32(E1000_DCA_TXCTRL(tx_ring->reg_idx), txctrl);
5116}
5117
5118static void igb_update_rx_dca(struct igb_adapter *adapter,
5119 struct igb_ring *rx_ring,
5120 int cpu)
5121{
5122 struct e1000_hw *hw = &adapter->hw;
5123 u32 rxctrl = dca3_get_tag(&adapter->pdev->dev, cpu);
5124
5125 if (hw->mac.type != e1000_82575)
5126 rxctrl <<= E1000_DCA_RXCTRL_CPUID_SHIFT;
5127
5128 /*
5129 * We can enable relaxed ordering for reads, but not writes when
5130 * DCA is enabled. This is due to a known issue in some chipsets
5131 * which will cause the DCA tag to be cleared.
5132 */
5133 rxctrl |= E1000_DCA_RXCTRL_DESC_RRO_EN |
5134 E1000_DCA_RXCTRL_DESC_DCA_EN;
5135
5136 wr32(E1000_DCA_RXCTRL(rx_ring->reg_idx), rxctrl);
5137}
5138
Alexander Duyck047e0032009-10-27 15:49:27 +00005139static void igb_update_dca(struct igb_q_vector *q_vector)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005140{
Alexander Duyck047e0032009-10-27 15:49:27 +00005141 struct igb_adapter *adapter = q_vector->adapter;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005142 int cpu = get_cpu();
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005143
Alexander Duyck047e0032009-10-27 15:49:27 +00005144 if (q_vector->cpu == cpu)
5145 goto out_no_update;
5146
Alexander Duyck6a050042012-09-25 00:31:27 +00005147 if (q_vector->tx.ring)
5148 igb_update_tx_dca(adapter, q_vector->tx.ring, cpu);
5149
5150 if (q_vector->rx.ring)
5151 igb_update_rx_dca(adapter, q_vector->rx.ring, cpu);
5152
Alexander Duyck047e0032009-10-27 15:49:27 +00005153 q_vector->cpu = cpu;
5154out_no_update:
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005155 put_cpu();
5156}
5157
5158static void igb_setup_dca(struct igb_adapter *adapter)
5159{
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00005160 struct e1000_hw *hw = &adapter->hw;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005161 int i;
5162
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07005163 if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005164 return;
5165
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00005166 /* Always use CB2 mode, difference is masked in the CB driver. */
5167 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
5168
Alexander Duyck047e0032009-10-27 15:49:27 +00005169 for (i = 0; i < adapter->num_q_vectors; i++) {
Alexander Duyck26b39272010-02-17 01:00:41 +00005170 adapter->q_vector[i]->cpu = -1;
5171 igb_update_dca(adapter->q_vector[i]);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005172 }
5173}
5174
5175static int __igb_notify_dca(struct device *dev, void *data)
5176{
5177 struct net_device *netdev = dev_get_drvdata(dev);
5178 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck090b1792009-10-27 23:51:55 +00005179 struct pci_dev *pdev = adapter->pdev;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005180 struct e1000_hw *hw = &adapter->hw;
5181 unsigned long event = *(unsigned long *)data;
5182
5183 switch (event) {
5184 case DCA_PROVIDER_ADD:
5185 /* if already enabled, don't do it again */
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07005186 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005187 break;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005188 if (dca_add_requester(dev) == 0) {
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08005189 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Alexander Duyck090b1792009-10-27 23:51:55 +00005190 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005191 igb_setup_dca(adapter);
5192 break;
5193 }
5194 /* Fall Through since DCA is disabled. */
5195 case DCA_PROVIDER_REMOVE:
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07005196 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005197 /* without this a class_device is left
Alexander Duyck047e0032009-10-27 15:49:27 +00005198 * hanging around in the sysfs model */
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005199 dca_remove_requester(dev);
Alexander Duyck090b1792009-10-27 23:51:55 +00005200 dev_info(&pdev->dev, "DCA disabled\n");
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07005201 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08005202 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005203 }
5204 break;
5205 }
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08005206
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005207 return 0;
5208}
5209
5210static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
5211 void *p)
5212{
5213 int ret_val;
5214
5215 ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
5216 __igb_notify_dca);
5217
5218 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
5219}
Jeff Kirsher421e02f2008-10-17 11:08:31 -07005220#endif /* CONFIG_IGB_DCA */
Auke Kok9d5c8242008-01-24 02:22:38 -08005221
Greg Rose0224d662011-10-14 02:57:14 +00005222#ifdef CONFIG_PCI_IOV
5223static int igb_vf_configure(struct igb_adapter *adapter, int vf)
5224{
5225 unsigned char mac_addr[ETH_ALEN];
Greg Rose0224d662011-10-14 02:57:14 +00005226
Mitch A Williams5ac6f912013-01-18 08:57:20 +00005227 eth_zero_addr(mac_addr);
Greg Rose0224d662011-10-14 02:57:14 +00005228 igb_set_vf_mac(adapter, vf, mac_addr);
5229
Stefan Assmannf5571472012-08-18 04:06:11 +00005230 return 0;
Greg Rose0224d662011-10-14 02:57:14 +00005231}
5232
Stefan Assmannf5571472012-08-18 04:06:11 +00005233static bool igb_vfs_are_assigned(struct igb_adapter *adapter)
Greg Rose0224d662011-10-14 02:57:14 +00005234{
Greg Rose0224d662011-10-14 02:57:14 +00005235 struct pci_dev *pdev = adapter->pdev;
Stefan Assmannf5571472012-08-18 04:06:11 +00005236 struct pci_dev *vfdev;
5237 int dev_id;
Greg Rose0224d662011-10-14 02:57:14 +00005238
5239 switch (adapter->hw.mac.type) {
5240 case e1000_82576:
Stefan Assmannf5571472012-08-18 04:06:11 +00005241 dev_id = IGB_82576_VF_DEV_ID;
Greg Rose0224d662011-10-14 02:57:14 +00005242 break;
5243 case e1000_i350:
Stefan Assmannf5571472012-08-18 04:06:11 +00005244 dev_id = IGB_I350_VF_DEV_ID;
Greg Rose0224d662011-10-14 02:57:14 +00005245 break;
5246 default:
Stefan Assmannf5571472012-08-18 04:06:11 +00005247 return false;
Greg Rose0224d662011-10-14 02:57:14 +00005248 }
5249
Stefan Assmannf5571472012-08-18 04:06:11 +00005250 /* loop through all the VFs to see if we own any that are assigned */
5251 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
5252 while (vfdev) {
5253 /* if we don't own it we don't care */
5254 if (vfdev->is_virtfn && vfdev->physfn == pdev) {
5255 /* if it is assigned we cannot release it */
5256 if (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
Greg Rose0224d662011-10-14 02:57:14 +00005257 return true;
5258 }
Stefan Assmannf5571472012-08-18 04:06:11 +00005259
5260 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, vfdev);
Greg Rose0224d662011-10-14 02:57:14 +00005261 }
Stefan Assmannf5571472012-08-18 04:06:11 +00005262
Greg Rose0224d662011-10-14 02:57:14 +00005263 return false;
5264}
5265
5266#endif
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005267static void igb_ping_all_vfs(struct igb_adapter *adapter)
5268{
5269 struct e1000_hw *hw = &adapter->hw;
5270 u32 ping;
5271 int i;
5272
5273 for (i = 0 ; i < adapter->vfs_allocated_count; i++) {
5274 ping = E1000_PF_CONTROL_MSG;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005275 if (adapter->vf_data[i].flags & IGB_VF_FLAG_CTS)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005276 ping |= E1000_VT_MSGTYPE_CTS;
5277 igb_write_mbx(hw, &ping, 1, i);
5278 }
5279}
5280
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005281static int igb_set_vf_promisc(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5282{
5283 struct e1000_hw *hw = &adapter->hw;
5284 u32 vmolr = rd32(E1000_VMOLR(vf));
5285 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5286
Alexander Duyckd85b90042010-09-22 17:56:20 +00005287 vf_data->flags &= ~(IGB_VF_FLAG_UNI_PROMISC |
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005288 IGB_VF_FLAG_MULTI_PROMISC);
5289 vmolr &= ~(E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5290
5291 if (*msgbuf & E1000_VF_SET_PROMISC_MULTICAST) {
5292 vmolr |= E1000_VMOLR_MPME;
Alexander Duyckd85b90042010-09-22 17:56:20 +00005293 vf_data->flags |= IGB_VF_FLAG_MULTI_PROMISC;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005294 *msgbuf &= ~E1000_VF_SET_PROMISC_MULTICAST;
5295 } else {
5296 /*
5297 * if we have hashes and we are clearing a multicast promisc
5298 * flag we need to write the hashes to the MTA as this step
5299 * was previously skipped
5300 */
5301 if (vf_data->num_vf_mc_hashes > 30) {
5302 vmolr |= E1000_VMOLR_MPME;
5303 } else if (vf_data->num_vf_mc_hashes) {
5304 int j;
5305 vmolr |= E1000_VMOLR_ROMPE;
5306 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5307 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5308 }
5309 }
5310
5311 wr32(E1000_VMOLR(vf), vmolr);
5312
5313 /* there are flags left unprocessed, likely not supported */
5314 if (*msgbuf & E1000_VT_MSGINFO_MASK)
5315 return -EINVAL;
5316
5317 return 0;
5318
5319}
5320
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005321static int igb_set_vf_multicasts(struct igb_adapter *adapter,
5322 u32 *msgbuf, u32 vf)
5323{
5324 int n = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5325 u16 *hash_list = (u16 *)&msgbuf[1];
5326 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5327 int i;
5328
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005329 /* salt away the number of multicast addresses assigned
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005330 * to this VF for later use to restore when the PF multi cast
5331 * list changes
5332 */
5333 vf_data->num_vf_mc_hashes = n;
5334
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005335 /* only up to 30 hash values supported */
5336 if (n > 30)
5337 n = 30;
5338
5339 /* store the hashes for later use */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005340 for (i = 0; i < n; i++)
Joe Perchesa419aef2009-08-18 11:18:35 -07005341 vf_data->vf_mc_hashes[i] = hash_list[i];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005342
5343 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005344 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005345
5346 return 0;
5347}
5348
5349static void igb_restore_vf_multicasts(struct igb_adapter *adapter)
5350{
5351 struct e1000_hw *hw = &adapter->hw;
5352 struct vf_data_storage *vf_data;
5353 int i, j;
5354
5355 for (i = 0; i < adapter->vfs_allocated_count; i++) {
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005356 u32 vmolr = rd32(E1000_VMOLR(i));
5357 vmolr &= ~(E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5358
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005359 vf_data = &adapter->vf_data[i];
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005360
5361 if ((vf_data->num_vf_mc_hashes > 30) ||
5362 (vf_data->flags & IGB_VF_FLAG_MULTI_PROMISC)) {
5363 vmolr |= E1000_VMOLR_MPME;
5364 } else if (vf_data->num_vf_mc_hashes) {
5365 vmolr |= E1000_VMOLR_ROMPE;
5366 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5367 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5368 }
5369 wr32(E1000_VMOLR(i), vmolr);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005370 }
5371}
5372
5373static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf)
5374{
5375 struct e1000_hw *hw = &adapter->hw;
5376 u32 pool_mask, reg, vid;
5377 int i;
5378
5379 pool_mask = 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5380
5381 /* Find the vlan filter for this id */
5382 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5383 reg = rd32(E1000_VLVF(i));
5384
5385 /* remove the vf from the pool */
5386 reg &= ~pool_mask;
5387
5388 /* if pool is empty then remove entry from vfta */
5389 if (!(reg & E1000_VLVF_POOLSEL_MASK) &&
5390 (reg & E1000_VLVF_VLANID_ENABLE)) {
5391 reg = 0;
5392 vid = reg & E1000_VLVF_VLANID_MASK;
5393 igb_vfta_set(hw, vid, false);
5394 }
5395
5396 wr32(E1000_VLVF(i), reg);
5397 }
Alexander Duyckae641bd2009-09-03 14:49:33 +00005398
5399 adapter->vf_data[vf].vlans_enabled = 0;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005400}
5401
5402static s32 igb_vlvf_set(struct igb_adapter *adapter, u32 vid, bool add, u32 vf)
5403{
5404 struct e1000_hw *hw = &adapter->hw;
5405 u32 reg, i;
5406
Alexander Duyck51466232009-10-27 23:47:35 +00005407 /* The vlvf table only exists on 82576 hardware and newer */
5408 if (hw->mac.type < e1000_82576)
5409 return -1;
5410
5411 /* we only need to do this if VMDq is enabled */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005412 if (!adapter->vfs_allocated_count)
5413 return -1;
5414
5415 /* Find the vlan filter for this id */
5416 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5417 reg = rd32(E1000_VLVF(i));
5418 if ((reg & E1000_VLVF_VLANID_ENABLE) &&
5419 vid == (reg & E1000_VLVF_VLANID_MASK))
5420 break;
5421 }
5422
5423 if (add) {
5424 if (i == E1000_VLVF_ARRAY_SIZE) {
5425 /* Did not find a matching VLAN ID entry that was
5426 * enabled. Search for a free filter entry, i.e.
5427 * one without the enable bit set
5428 */
5429 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5430 reg = rd32(E1000_VLVF(i));
5431 if (!(reg & E1000_VLVF_VLANID_ENABLE))
5432 break;
5433 }
5434 }
5435 if (i < E1000_VLVF_ARRAY_SIZE) {
5436 /* Found an enabled/available entry */
5437 reg |= 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5438
5439 /* if !enabled we need to set this up in vfta */
5440 if (!(reg & E1000_VLVF_VLANID_ENABLE)) {
Alexander Duyck51466232009-10-27 23:47:35 +00005441 /* add VID to filter table */
5442 igb_vfta_set(hw, vid, true);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005443 reg |= E1000_VLVF_VLANID_ENABLE;
5444 }
Alexander Duyckcad6d052009-03-13 20:41:37 +00005445 reg &= ~E1000_VLVF_VLANID_MASK;
5446 reg |= vid;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005447 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00005448
5449 /* do not modify RLPML for PF devices */
5450 if (vf >= adapter->vfs_allocated_count)
5451 return 0;
5452
5453 if (!adapter->vf_data[vf].vlans_enabled) {
5454 u32 size;
5455 reg = rd32(E1000_VMOLR(vf));
5456 size = reg & E1000_VMOLR_RLPML_MASK;
5457 size += 4;
5458 reg &= ~E1000_VMOLR_RLPML_MASK;
5459 reg |= size;
5460 wr32(E1000_VMOLR(vf), reg);
5461 }
Alexander Duyckae641bd2009-09-03 14:49:33 +00005462
Alexander Duyck51466232009-10-27 23:47:35 +00005463 adapter->vf_data[vf].vlans_enabled++;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005464 }
5465 } else {
5466 if (i < E1000_VLVF_ARRAY_SIZE) {
5467 /* remove vf from the pool */
5468 reg &= ~(1 << (E1000_VLVF_POOLSEL_SHIFT + vf));
5469 /* if pool is empty then remove entry from vfta */
5470 if (!(reg & E1000_VLVF_POOLSEL_MASK)) {
5471 reg = 0;
5472 igb_vfta_set(hw, vid, false);
5473 }
5474 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00005475
5476 /* do not modify RLPML for PF devices */
5477 if (vf >= adapter->vfs_allocated_count)
5478 return 0;
5479
5480 adapter->vf_data[vf].vlans_enabled--;
5481 if (!adapter->vf_data[vf].vlans_enabled) {
5482 u32 size;
5483 reg = rd32(E1000_VMOLR(vf));
5484 size = reg & E1000_VMOLR_RLPML_MASK;
5485 size -= 4;
5486 reg &= ~E1000_VMOLR_RLPML_MASK;
5487 reg |= size;
5488 wr32(E1000_VMOLR(vf), reg);
5489 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005490 }
5491 }
Williams, Mitch A8151d292010-02-10 01:44:24 +00005492 return 0;
5493}
5494
5495static void igb_set_vmvir(struct igb_adapter *adapter, u32 vid, u32 vf)
5496{
5497 struct e1000_hw *hw = &adapter->hw;
5498
5499 if (vid)
5500 wr32(E1000_VMVIR(vf), (vid | E1000_VMVIR_VLANA_DEFAULT));
5501 else
5502 wr32(E1000_VMVIR(vf), 0);
5503}
5504
5505static int igb_ndo_set_vf_vlan(struct net_device *netdev,
5506 int vf, u16 vlan, u8 qos)
5507{
5508 int err = 0;
5509 struct igb_adapter *adapter = netdev_priv(netdev);
5510
5511 if ((vf >= adapter->vfs_allocated_count) || (vlan > 4095) || (qos > 7))
5512 return -EINVAL;
5513 if (vlan || qos) {
5514 err = igb_vlvf_set(adapter, vlan, !!vlan, vf);
5515 if (err)
5516 goto out;
5517 igb_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
5518 igb_set_vmolr(adapter, vf, !vlan);
5519 adapter->vf_data[vf].pf_vlan = vlan;
5520 adapter->vf_data[vf].pf_qos = qos;
5521 dev_info(&adapter->pdev->dev,
5522 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
5523 if (test_bit(__IGB_DOWN, &adapter->state)) {
5524 dev_warn(&adapter->pdev->dev,
5525 "The VF VLAN has been set,"
5526 " but the PF device is not up.\n");
5527 dev_warn(&adapter->pdev->dev,
5528 "Bring the PF device up before"
5529 " attempting to use the VF device.\n");
5530 }
5531 } else {
5532 igb_vlvf_set(adapter, adapter->vf_data[vf].pf_vlan,
5533 false, vf);
5534 igb_set_vmvir(adapter, vlan, vf);
5535 igb_set_vmolr(adapter, vf, true);
5536 adapter->vf_data[vf].pf_vlan = 0;
5537 adapter->vf_data[vf].pf_qos = 0;
5538 }
5539out:
5540 return err;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005541}
5542
5543static int igb_set_vf_vlan(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5544{
5545 int add = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5546 int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
5547
5548 return igb_vlvf_set(adapter, vid, add, vf);
5549}
5550
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005551static inline void igb_vf_reset(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005552{
Greg Rose8fa7e0f2010-11-06 05:43:21 +00005553 /* clear flags - except flag that indicates PF has set the MAC */
5554 adapter->vf_data[vf].flags &= IGB_VF_FLAG_PF_SET_MAC;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005555 adapter->vf_data[vf].last_nack = jiffies;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005556
5557 /* reset offloads to defaults */
Williams, Mitch A8151d292010-02-10 01:44:24 +00005558 igb_set_vmolr(adapter, vf, true);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005559
5560 /* reset vlans for device */
5561 igb_clear_vf_vfta(adapter, vf);
Williams, Mitch A8151d292010-02-10 01:44:24 +00005562 if (adapter->vf_data[vf].pf_vlan)
5563 igb_ndo_set_vf_vlan(adapter->netdev, vf,
5564 adapter->vf_data[vf].pf_vlan,
5565 adapter->vf_data[vf].pf_qos);
5566 else
5567 igb_clear_vf_vfta(adapter, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005568
5569 /* reset multicast table array for vf */
5570 adapter->vf_data[vf].num_vf_mc_hashes = 0;
5571
5572 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005573 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005574}
5575
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005576static void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf)
5577{
5578 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
5579
Mitch A Williams5ac6f912013-01-18 08:57:20 +00005580 /* clear mac address as we were hotplug removed/added */
Williams, Mitch A8151d292010-02-10 01:44:24 +00005581 if (!(adapter->vf_data[vf].flags & IGB_VF_FLAG_PF_SET_MAC))
Mitch A Williams5ac6f912013-01-18 08:57:20 +00005582 eth_zero_addr(vf_mac);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005583
5584 /* process remaining reset events */
5585 igb_vf_reset(adapter, vf);
5586}
5587
5588static void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005589{
5590 struct e1000_hw *hw = &adapter->hw;
5591 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005592 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005593 u32 reg, msgbuf[3];
5594 u8 *addr = (u8 *)(&msgbuf[1]);
5595
5596 /* process all the same items cleared in a function level reset */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005597 igb_vf_reset(adapter, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005598
5599 /* set vf mac address */
Alexander Duyck26ad9172009-10-05 06:32:49 +00005600 igb_rar_set_qsel(adapter, vf_mac, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005601
5602 /* enable transmit and receive for vf */
5603 reg = rd32(E1000_VFTE);
5604 wr32(E1000_VFTE, reg | (1 << vf));
5605 reg = rd32(E1000_VFRE);
5606 wr32(E1000_VFRE, reg | (1 << vf));
5607
Greg Rose8fa7e0f2010-11-06 05:43:21 +00005608 adapter->vf_data[vf].flags |= IGB_VF_FLAG_CTS;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005609
5610 /* reply to reset with ack and vf mac address */
5611 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
5612 memcpy(addr, vf_mac, 6);
5613 igb_write_mbx(hw, msgbuf, 3, vf);
5614}
5615
5616static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf)
5617{
Greg Rosede42edd2010-07-01 13:39:23 +00005618 /*
5619 * The VF MAC Address is stored in a packed array of bytes
5620 * starting at the second 32 bit word of the msg array
5621 */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005622 unsigned char *addr = (char *)&msg[1];
5623 int err = -1;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005624
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005625 if (is_valid_ether_addr(addr))
5626 err = igb_set_vf_mac(adapter, vf, addr);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005627
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005628 return err;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005629}
5630
5631static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf)
5632{
5633 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005634 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005635 u32 msg = E1000_VT_MSGTYPE_NACK;
5636
5637 /* if device isn't clear to send it shouldn't be reading either */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005638 if (!(vf_data->flags & IGB_VF_FLAG_CTS) &&
5639 time_after(jiffies, vf_data->last_nack + (2 * HZ))) {
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005640 igb_write_mbx(hw, &msg, 1, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005641 vf_data->last_nack = jiffies;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005642 }
5643}
5644
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005645static void igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005646{
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005647 struct pci_dev *pdev = adapter->pdev;
5648 u32 msgbuf[E1000_VFMAILBOX_SIZE];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005649 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005650 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005651 s32 retval;
5652
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005653 retval = igb_read_mbx(hw, msgbuf, E1000_VFMAILBOX_SIZE, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005654
Alexander Duyckfef45f42009-12-11 22:57:34 -08005655 if (retval) {
5656 /* if receive failed revoke VF CTS stats and restart init */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005657 dev_err(&pdev->dev, "Error receiving message from VF\n");
Alexander Duyckfef45f42009-12-11 22:57:34 -08005658 vf_data->flags &= ~IGB_VF_FLAG_CTS;
5659 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5660 return;
5661 goto out;
5662 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005663
5664 /* this is a message we already processed, do nothing */
5665 if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005666 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005667
5668 /*
5669 * until the vf completes a reset it should not be
5670 * allowed to start any configuration.
5671 */
5672
5673 if (msgbuf[0] == E1000_VF_RESET) {
5674 igb_vf_reset_msg(adapter, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005675 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005676 }
5677
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005678 if (!(vf_data->flags & IGB_VF_FLAG_CTS)) {
Alexander Duyckfef45f42009-12-11 22:57:34 -08005679 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5680 return;
5681 retval = -1;
5682 goto out;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005683 }
5684
5685 switch ((msgbuf[0] & 0xFFFF)) {
5686 case E1000_VF_SET_MAC_ADDR:
Greg Rosea6b5ea32010-11-06 05:42:59 +00005687 retval = -EINVAL;
5688 if (!(vf_data->flags & IGB_VF_FLAG_PF_SET_MAC))
5689 retval = igb_set_vf_mac_addr(adapter, msgbuf, vf);
5690 else
5691 dev_warn(&pdev->dev,
5692 "VF %d attempted to override administratively "
5693 "set MAC address\nReload the VF driver to "
5694 "resume operations\n", vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005695 break;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005696 case E1000_VF_SET_PROMISC:
5697 retval = igb_set_vf_promisc(adapter, msgbuf, vf);
5698 break;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005699 case E1000_VF_SET_MULTICAST:
5700 retval = igb_set_vf_multicasts(adapter, msgbuf, vf);
5701 break;
5702 case E1000_VF_SET_LPE:
5703 retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf);
5704 break;
5705 case E1000_VF_SET_VLAN:
Greg Rosea6b5ea32010-11-06 05:42:59 +00005706 retval = -1;
5707 if (vf_data->pf_vlan)
5708 dev_warn(&pdev->dev,
5709 "VF %d attempted to override administratively "
5710 "set VLAN tag\nReload the VF driver to "
5711 "resume operations\n", vf);
Williams, Mitch A8151d292010-02-10 01:44:24 +00005712 else
5713 retval = igb_set_vf_vlan(adapter, msgbuf, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005714 break;
5715 default:
Alexander Duyck090b1792009-10-27 23:51:55 +00005716 dev_err(&pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005717 retval = -1;
5718 break;
5719 }
5720
Alexander Duyckfef45f42009-12-11 22:57:34 -08005721 msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
5722out:
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005723 /* notify the VF of the results of what it sent us */
5724 if (retval)
5725 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
5726 else
5727 msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
5728
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005729 igb_write_mbx(hw, msgbuf, 1, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005730}
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005731
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005732static void igb_msg_task(struct igb_adapter *adapter)
5733{
5734 struct e1000_hw *hw = &adapter->hw;
5735 u32 vf;
5736
5737 for (vf = 0; vf < adapter->vfs_allocated_count; vf++) {
5738 /* process any reset requests */
5739 if (!igb_check_for_rst(hw, vf))
5740 igb_vf_reset_event(adapter, vf);
5741
5742 /* process any messages pending */
5743 if (!igb_check_for_msg(hw, vf))
5744 igb_rcv_msg_from_vf(adapter, vf);
5745
5746 /* process any acks */
5747 if (!igb_check_for_ack(hw, vf))
5748 igb_rcv_ack_from_vf(adapter, vf);
5749 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005750}
5751
Auke Kok9d5c8242008-01-24 02:22:38 -08005752/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00005753 * igb_set_uta - Set unicast filter table address
5754 * @adapter: board private structure
5755 *
5756 * The unicast table address is a register array of 32-bit registers.
5757 * The table is meant to be used in a way similar to how the MTA is used
5758 * however due to certain limitations in the hardware it is necessary to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005759 * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscuous
5760 * enable bit to allow vlan tag stripping when promiscuous mode is enabled
Alexander Duyck68d480c2009-10-05 06:33:08 +00005761 **/
5762static void igb_set_uta(struct igb_adapter *adapter)
5763{
5764 struct e1000_hw *hw = &adapter->hw;
5765 int i;
5766
5767 /* The UTA table only exists on 82576 hardware and newer */
5768 if (hw->mac.type < e1000_82576)
5769 return;
5770
5771 /* we only need to do this if VMDq is enabled */
5772 if (!adapter->vfs_allocated_count)
5773 return;
5774
5775 for (i = 0; i < hw->mac.uta_reg_count; i++)
5776 array_wr32(E1000_UTA, i, ~0);
5777}
5778
5779/**
Auke Kok9d5c8242008-01-24 02:22:38 -08005780 * igb_intr_msi - Interrupt Handler
5781 * @irq: interrupt number
5782 * @data: pointer to a network interface device structure
5783 **/
5784static irqreturn_t igb_intr_msi(int irq, void *data)
5785{
Alexander Duyck047e0032009-10-27 15:49:27 +00005786 struct igb_adapter *adapter = data;
5787 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08005788 struct e1000_hw *hw = &adapter->hw;
5789 /* read ICR disables interrupts using IAM */
5790 u32 icr = rd32(E1000_ICR);
5791
Alexander Duyck047e0032009-10-27 15:49:27 +00005792 igb_write_itr(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08005793
Alexander Duyck7f081d42010-01-07 17:41:00 +00005794 if (icr & E1000_ICR_DRSTA)
5795 schedule_work(&adapter->reset_task);
5796
Alexander Duyck047e0032009-10-27 15:49:27 +00005797 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00005798 /* HW is reporting DMA is out of sync */
5799 adapter->stats.doosync++;
5800 }
5801
Auke Kok9d5c8242008-01-24 02:22:38 -08005802 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5803 hw->mac.get_link_status = 1;
5804 if (!test_bit(__IGB_DOWN, &adapter->state))
5805 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5806 }
5807
Matthew Vick1f6e8172012-08-18 07:26:33 +00005808 if (icr & E1000_ICR_TS) {
5809 u32 tsicr = rd32(E1000_TSICR);
5810
5811 if (tsicr & E1000_TSICR_TXTS) {
5812 /* acknowledge the interrupt */
5813 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5814 /* retrieve hardware timestamp */
5815 schedule_work(&adapter->ptp_tx_work);
5816 }
5817 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00005818
Alexander Duyck047e0032009-10-27 15:49:27 +00005819 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08005820
5821 return IRQ_HANDLED;
5822}
5823
5824/**
Alexander Duyck4a3c6432009-02-06 23:20:49 +00005825 * igb_intr - Legacy Interrupt Handler
Auke Kok9d5c8242008-01-24 02:22:38 -08005826 * @irq: interrupt number
5827 * @data: pointer to a network interface device structure
5828 **/
5829static irqreturn_t igb_intr(int irq, void *data)
5830{
Alexander Duyck047e0032009-10-27 15:49:27 +00005831 struct igb_adapter *adapter = data;
5832 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08005833 struct e1000_hw *hw = &adapter->hw;
5834 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
5835 * need for the IMC write */
5836 u32 icr = rd32(E1000_ICR);
Auke Kok9d5c8242008-01-24 02:22:38 -08005837
5838 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
5839 * not set, then the adapter didn't send an interrupt */
5840 if (!(icr & E1000_ICR_INT_ASSERTED))
5841 return IRQ_NONE;
5842
Alexander Duyck0ba82992011-08-26 07:45:47 +00005843 igb_write_itr(q_vector);
5844
Alexander Duyck7f081d42010-01-07 17:41:00 +00005845 if (icr & E1000_ICR_DRSTA)
5846 schedule_work(&adapter->reset_task);
5847
Alexander Duyck047e0032009-10-27 15:49:27 +00005848 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00005849 /* HW is reporting DMA is out of sync */
5850 adapter->stats.doosync++;
5851 }
5852
Auke Kok9d5c8242008-01-24 02:22:38 -08005853 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5854 hw->mac.get_link_status = 1;
5855 /* guard against interrupt when we're going down */
5856 if (!test_bit(__IGB_DOWN, &adapter->state))
5857 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5858 }
5859
Matthew Vick1f6e8172012-08-18 07:26:33 +00005860 if (icr & E1000_ICR_TS) {
5861 u32 tsicr = rd32(E1000_TSICR);
5862
5863 if (tsicr & E1000_TSICR_TXTS) {
5864 /* acknowledge the interrupt */
5865 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5866 /* retrieve hardware timestamp */
5867 schedule_work(&adapter->ptp_tx_work);
5868 }
5869 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00005870
Alexander Duyck047e0032009-10-27 15:49:27 +00005871 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08005872
5873 return IRQ_HANDLED;
5874}
5875
Stephen Hemmingerc50b52a2012-01-18 22:13:26 +00005876static void igb_ring_irq_enable(struct igb_q_vector *q_vector)
Alexander Duyck46544252009-02-19 20:39:04 -08005877{
Alexander Duyck047e0032009-10-27 15:49:27 +00005878 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck46544252009-02-19 20:39:04 -08005879 struct e1000_hw *hw = &adapter->hw;
5880
Alexander Duyck0ba82992011-08-26 07:45:47 +00005881 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
5882 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
5883 if ((adapter->num_q_vectors == 1) && !adapter->vf_data)
5884 igb_set_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005885 else
Alexander Duyck047e0032009-10-27 15:49:27 +00005886 igb_update_ring_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005887 }
5888
5889 if (!test_bit(__IGB_DOWN, &adapter->state)) {
5890 if (adapter->msix_entries)
Alexander Duyck047e0032009-10-27 15:49:27 +00005891 wr32(E1000_EIMS, q_vector->eims_value);
Alexander Duyck46544252009-02-19 20:39:04 -08005892 else
5893 igb_irq_enable(adapter);
5894 }
5895}
5896
Auke Kok9d5c8242008-01-24 02:22:38 -08005897/**
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005898 * igb_poll - NAPI Rx polling callback
5899 * @napi: napi polling structure
5900 * @budget: count of how many packets we should handle
Auke Kok9d5c8242008-01-24 02:22:38 -08005901 **/
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005902static int igb_poll(struct napi_struct *napi, int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08005903{
Alexander Duyck047e0032009-10-27 15:49:27 +00005904 struct igb_q_vector *q_vector = container_of(napi,
5905 struct igb_q_vector,
5906 napi);
Alexander Duyck16eb8812011-08-26 07:43:54 +00005907 bool clean_complete = true;
Auke Kok9d5c8242008-01-24 02:22:38 -08005908
Jeff Kirsher421e02f2008-10-17 11:08:31 -07005909#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +00005910 if (q_vector->adapter->flags & IGB_FLAG_DCA_ENABLED)
5911 igb_update_dca(q_vector);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005912#endif
Alexander Duyck0ba82992011-08-26 07:45:47 +00005913 if (q_vector->tx.ring)
Alexander Duyck13fde972011-10-05 13:35:24 +00005914 clean_complete = igb_clean_tx_irq(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08005915
Alexander Duyck0ba82992011-08-26 07:45:47 +00005916 if (q_vector->rx.ring)
Alexander Duyckcd392f52011-08-26 07:43:59 +00005917 clean_complete &= igb_clean_rx_irq(q_vector, budget);
Alexander Duyck047e0032009-10-27 15:49:27 +00005918
Alexander Duyck16eb8812011-08-26 07:43:54 +00005919 /* If all work not completed, return budget and keep polling */
5920 if (!clean_complete)
5921 return budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08005922
Alexander Duyck46544252009-02-19 20:39:04 -08005923 /* If not enough Rx work done, exit the polling mode */
Alexander Duyck16eb8812011-08-26 07:43:54 +00005924 napi_complete(napi);
5925 igb_ring_irq_enable(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005926
Alexander Duyck16eb8812011-08-26 07:43:54 +00005927 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08005928}
Al Viro6d8126f2008-03-16 22:23:24 +00005929
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005930/**
Auke Kok9d5c8242008-01-24 02:22:38 -08005931 * igb_clean_tx_irq - Reclaim resources after transmit completes
Alexander Duyck047e0032009-10-27 15:49:27 +00005932 * @q_vector: pointer to q_vector containing needed info
Ben Hutchings49ce9c22012-07-10 10:56:00 +00005933 *
Auke Kok9d5c8242008-01-24 02:22:38 -08005934 * returns true if ring is completely cleaned
5935 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00005936static bool igb_clean_tx_irq(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08005937{
Alexander Duyck047e0032009-10-27 15:49:27 +00005938 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck0ba82992011-08-26 07:45:47 +00005939 struct igb_ring *tx_ring = q_vector->tx.ring;
Alexander Duyck06034642011-08-26 07:44:22 +00005940 struct igb_tx_buffer *tx_buffer;
Alexander Duyckf4128782012-09-13 06:28:01 +00005941 union e1000_adv_tx_desc *tx_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08005942 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck0ba82992011-08-26 07:45:47 +00005943 unsigned int budget = q_vector->tx.work_limit;
Alexander Duyck8542db02011-08-26 07:44:43 +00005944 unsigned int i = tx_ring->next_to_clean;
Auke Kok9d5c8242008-01-24 02:22:38 -08005945
Alexander Duyck13fde972011-10-05 13:35:24 +00005946 if (test_bit(__IGB_DOWN, &adapter->state))
5947 return true;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08005948
Alexander Duyck06034642011-08-26 07:44:22 +00005949 tx_buffer = &tx_ring->tx_buffer_info[i];
Alexander Duyck13fde972011-10-05 13:35:24 +00005950 tx_desc = IGB_TX_DESC(tx_ring, i);
Alexander Duyck8542db02011-08-26 07:44:43 +00005951 i -= tx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08005952
Alexander Duyckf4128782012-09-13 06:28:01 +00005953 do {
5954 union e1000_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
Alexander Duyck8542db02011-08-26 07:44:43 +00005955
5956 /* if next_to_watch is not set then there is no work pending */
5957 if (!eop_desc)
5958 break;
Alexander Duyck13fde972011-10-05 13:35:24 +00005959
Alexander Duyckf4128782012-09-13 06:28:01 +00005960 /* prevent any other reads prior to eop_desc */
Alexander Duyck70d289b2013-01-08 07:01:03 +00005961 read_barrier_depends();
Alexander Duyckf4128782012-09-13 06:28:01 +00005962
Alexander Duyck13fde972011-10-05 13:35:24 +00005963 /* if DD is not set pending work has not been completed */
5964 if (!(eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)))
5965 break;
5966
Alexander Duyck8542db02011-08-26 07:44:43 +00005967 /* clear next_to_watch to prevent false hangs */
5968 tx_buffer->next_to_watch = NULL;
Alexander Duyck13fde972011-10-05 13:35:24 +00005969
Alexander Duyckebe42d12011-08-26 07:45:09 +00005970 /* update the statistics for this packet */
5971 total_bytes += tx_buffer->bytecount;
5972 total_packets += tx_buffer->gso_segs;
Alexander Duyck13fde972011-10-05 13:35:24 +00005973
Alexander Duyckebe42d12011-08-26 07:45:09 +00005974 /* free the skb */
5975 dev_kfree_skb_any(tx_buffer->skb);
Alexander Duyckebe42d12011-08-26 07:45:09 +00005976
5977 /* unmap skb header data */
5978 dma_unmap_single(tx_ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005979 dma_unmap_addr(tx_buffer, dma),
5980 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00005981 DMA_TO_DEVICE);
5982
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005983 /* clear tx_buffer data */
5984 tx_buffer->skb = NULL;
5985 dma_unmap_len_set(tx_buffer, len, 0);
5986
Alexander Duyckebe42d12011-08-26 07:45:09 +00005987 /* clear last DMA location and unmap remaining buffers */
5988 while (tx_desc != eop_desc) {
Alexander Duyck13fde972011-10-05 13:35:24 +00005989 tx_buffer++;
5990 tx_desc++;
Auke Kok9d5c8242008-01-24 02:22:38 -08005991 i++;
Alexander Duyck8542db02011-08-26 07:44:43 +00005992 if (unlikely(!i)) {
5993 i -= tx_ring->count;
Alexander Duyck06034642011-08-26 07:44:22 +00005994 tx_buffer = tx_ring->tx_buffer_info;
Alexander Duyck13fde972011-10-05 13:35:24 +00005995 tx_desc = IGB_TX_DESC(tx_ring, 0);
5996 }
Alexander Duyckebe42d12011-08-26 07:45:09 +00005997
5998 /* unmap any remaining paged data */
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005999 if (dma_unmap_len(tx_buffer, len)) {
Alexander Duyckebe42d12011-08-26 07:45:09 +00006000 dma_unmap_page(tx_ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00006001 dma_unmap_addr(tx_buffer, dma),
6002 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00006003 DMA_TO_DEVICE);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00006004 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duyckebe42d12011-08-26 07:45:09 +00006005 }
6006 }
6007
Alexander Duyckebe42d12011-08-26 07:45:09 +00006008 /* move us one more past the eop_desc for start of next pkt */
6009 tx_buffer++;
6010 tx_desc++;
6011 i++;
6012 if (unlikely(!i)) {
6013 i -= tx_ring->count;
6014 tx_buffer = tx_ring->tx_buffer_info;
6015 tx_desc = IGB_TX_DESC(tx_ring, 0);
6016 }
Alexander Duyckf4128782012-09-13 06:28:01 +00006017
6018 /* issue prefetch for next Tx descriptor */
6019 prefetch(tx_desc);
6020
6021 /* update budget accounting */
6022 budget--;
6023 } while (likely(budget));
Alexander Duyck0e014cb2008-12-26 01:33:18 -08006024
Eric Dumazetbdbc0632012-01-04 20:23:36 +00006025 netdev_tx_completed_queue(txring_txq(tx_ring),
6026 total_packets, total_bytes);
Alexander Duyck8542db02011-08-26 07:44:43 +00006027 i += tx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08006028 tx_ring->next_to_clean = i;
Alexander Duyck13fde972011-10-05 13:35:24 +00006029 u64_stats_update_begin(&tx_ring->tx_syncp);
6030 tx_ring->tx_stats.bytes += total_bytes;
6031 tx_ring->tx_stats.packets += total_packets;
6032 u64_stats_update_end(&tx_ring->tx_syncp);
Alexander Duyck0ba82992011-08-26 07:45:47 +00006033 q_vector->tx.total_bytes += total_bytes;
6034 q_vector->tx.total_packets += total_packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08006035
Alexander Duyck6d095fa2011-08-26 07:46:19 +00006036 if (test_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
Alexander Duyck13fde972011-10-05 13:35:24 +00006037 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck13fde972011-10-05 13:35:24 +00006038
Auke Kok9d5c8242008-01-24 02:22:38 -08006039 /* Detect a transmit hang in hardware, this serializes the
6040 * check with the clearing of time_stamp and movement of i */
Alexander Duyck6d095fa2011-08-26 07:46:19 +00006041 clear_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
Alexander Duyckf4128782012-09-13 06:28:01 +00006042 if (tx_buffer->next_to_watch &&
Alexander Duyck8542db02011-08-26 07:44:43 +00006043 time_after(jiffies, tx_buffer->time_stamp +
Joe Perches8e95a202009-12-03 07:58:21 +00006044 (adapter->tx_timeout_factor * HZ)) &&
6045 !(rd32(E1000_STATUS) & E1000_STATUS_TXOFF)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006046
Auke Kok9d5c8242008-01-24 02:22:38 -08006047 /* detected Tx unit hang */
Alexander Duyck59d71982010-04-27 13:09:25 +00006048 dev_err(tx_ring->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08006049 "Detected Tx Unit Hang\n"
Alexander Duyck2d064c02008-07-08 15:10:12 -07006050 " Tx Queue <%d>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08006051 " TDH <%x>\n"
6052 " TDT <%x>\n"
6053 " next_to_use <%x>\n"
6054 " next_to_clean <%x>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08006055 "buffer_info[next_to_clean]\n"
6056 " time_stamp <%lx>\n"
Alexander Duyck8542db02011-08-26 07:44:43 +00006057 " next_to_watch <%p>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08006058 " jiffies <%lx>\n"
6059 " desc.status <%x>\n",
Alexander Duyck2d064c02008-07-08 15:10:12 -07006060 tx_ring->queue_index,
Alexander Duyck238ac812011-08-26 07:43:48 +00006061 rd32(E1000_TDH(tx_ring->reg_idx)),
Alexander Duyckfce99e32009-10-27 15:51:27 +00006062 readl(tx_ring->tail),
Auke Kok9d5c8242008-01-24 02:22:38 -08006063 tx_ring->next_to_use,
6064 tx_ring->next_to_clean,
Alexander Duyck8542db02011-08-26 07:44:43 +00006065 tx_buffer->time_stamp,
Alexander Duyckf4128782012-09-13 06:28:01 +00006066 tx_buffer->next_to_watch,
Auke Kok9d5c8242008-01-24 02:22:38 -08006067 jiffies,
Alexander Duyckf4128782012-09-13 06:28:01 +00006068 tx_buffer->next_to_watch->wb.status);
Alexander Duyck13fde972011-10-05 13:35:24 +00006069 netif_stop_subqueue(tx_ring->netdev,
6070 tx_ring->queue_index);
6071
6072 /* we are about to reset, no point in enabling stuff */
6073 return true;
Auke Kok9d5c8242008-01-24 02:22:38 -08006074 }
6075 }
Alexander Duyck13fde972011-10-05 13:35:24 +00006076
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00006077#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
Alexander Duyck13fde972011-10-05 13:35:24 +00006078 if (unlikely(total_packets &&
6079 netif_carrier_ok(tx_ring->netdev) &&
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00006080 igb_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD)) {
Alexander Duyck13fde972011-10-05 13:35:24 +00006081 /* Make sure that anybody stopping the queue after this
6082 * sees the new next_to_clean.
6083 */
6084 smp_mb();
6085 if (__netif_subqueue_stopped(tx_ring->netdev,
6086 tx_ring->queue_index) &&
6087 !(test_bit(__IGB_DOWN, &adapter->state))) {
6088 netif_wake_subqueue(tx_ring->netdev,
6089 tx_ring->queue_index);
6090
6091 u64_stats_update_begin(&tx_ring->tx_syncp);
6092 tx_ring->tx_stats.restart_queue++;
6093 u64_stats_update_end(&tx_ring->tx_syncp);
6094 }
6095 }
6096
6097 return !!budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08006098}
6099
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006100/**
6101 * igb_reuse_rx_page - page flip buffer and store it back on the ring
6102 * @rx_ring: rx descriptor ring to store buffers on
6103 * @old_buff: donor buffer to have page reused
6104 *
6105 * Synchronizes page for reuse by the adapter
6106 **/
6107static void igb_reuse_rx_page(struct igb_ring *rx_ring,
6108 struct igb_rx_buffer *old_buff)
6109{
6110 struct igb_rx_buffer *new_buff;
6111 u16 nta = rx_ring->next_to_alloc;
6112
6113 new_buff = &rx_ring->rx_buffer_info[nta];
6114
6115 /* update, and store next to alloc */
6116 nta++;
6117 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
6118
6119 /* transfer page from old buffer to new buffer */
6120 memcpy(new_buff, old_buff, sizeof(struct igb_rx_buffer));
6121
6122 /* sync the buffer for use by the device */
6123 dma_sync_single_range_for_device(rx_ring->dev, old_buff->dma,
6124 old_buff->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006125 IGB_RX_BUFSZ,
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006126 DMA_FROM_DEVICE);
6127}
6128
Alexander Duyck74e238e2013-02-02 05:07:11 +00006129static bool igb_can_reuse_rx_page(struct igb_rx_buffer *rx_buffer,
6130 struct page *page,
6131 unsigned int truesize)
6132{
6133 /* avoid re-using remote pages */
6134 if (unlikely(page_to_nid(page) != numa_node_id()))
6135 return false;
6136
6137#if (PAGE_SIZE < 8192)
6138 /* if we are only owner of page we can reuse it */
6139 if (unlikely(page_count(page) != 1))
6140 return false;
6141
6142 /* flip page offset to other buffer */
6143 rx_buffer->page_offset ^= IGB_RX_BUFSZ;
6144
6145 /* since we are the only owner of the page and we need to
6146 * increment it, just set the value to 2 in order to avoid
6147 * an unnecessary locked operation
6148 */
6149 atomic_set(&page->_count, 2);
6150#else
6151 /* move offset up to the next cache line */
6152 rx_buffer->page_offset += truesize;
6153
6154 if (rx_buffer->page_offset > (PAGE_SIZE - IGB_RX_BUFSZ))
6155 return false;
6156
6157 /* bump ref count on page before it is given to the stack */
6158 get_page(page);
6159#endif
6160
6161 return true;
6162}
6163
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006164/**
6165 * igb_add_rx_frag - Add contents of Rx buffer to sk_buff
6166 * @rx_ring: rx descriptor ring to transact packets on
6167 * @rx_buffer: buffer containing page to add
6168 * @rx_desc: descriptor containing length of buffer written by hardware
6169 * @skb: sk_buff to place the data into
6170 *
6171 * This function will add the data contained in rx_buffer->page to the skb.
6172 * This is done either through a direct copy if the data in the buffer is
6173 * less than the skb header size, otherwise it will just attach the page as
6174 * a frag to the skb.
6175 *
6176 * The function will then update the page offset if necessary and return
6177 * true if the buffer can be reused by the adapter.
6178 **/
6179static bool igb_add_rx_frag(struct igb_ring *rx_ring,
6180 struct igb_rx_buffer *rx_buffer,
6181 union e1000_adv_rx_desc *rx_desc,
6182 struct sk_buff *skb)
6183{
6184 struct page *page = rx_buffer->page;
6185 unsigned int size = le16_to_cpu(rx_desc->wb.upper.length);
Alexander Duyck74e238e2013-02-02 05:07:11 +00006186#if (PAGE_SIZE < 8192)
6187 unsigned int truesize = IGB_RX_BUFSZ;
6188#else
6189 unsigned int truesize = ALIGN(size, L1_CACHE_BYTES);
6190#endif
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006191
6192 if ((size <= IGB_RX_HDR_LEN) && !skb_is_nonlinear(skb)) {
6193 unsigned char *va = page_address(page) + rx_buffer->page_offset;
6194
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006195 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
6196 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
6197 va += IGB_TS_HDR_LEN;
6198 size -= IGB_TS_HDR_LEN;
6199 }
6200
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006201 memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
6202
6203 /* we can reuse buffer as-is, just make sure it is local */
6204 if (likely(page_to_nid(page) == numa_node_id()))
6205 return true;
6206
6207 /* this page cannot be reused so discard it */
6208 put_page(page);
6209 return false;
6210 }
6211
6212 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
Alexander Duyck74e238e2013-02-02 05:07:11 +00006213 rx_buffer->page_offset, size, truesize);
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006214
Alexander Duyck74e238e2013-02-02 05:07:11 +00006215 return igb_can_reuse_rx_page(rx_buffer, page, truesize);
6216}
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006217
Alexander Duyck74e238e2013-02-02 05:07:11 +00006218static struct sk_buff *igb_build_rx_buffer(struct igb_ring *rx_ring,
6219 union e1000_adv_rx_desc *rx_desc)
6220{
6221 struct igb_rx_buffer *rx_buffer;
6222 struct sk_buff *skb;
6223 struct page *page;
6224 void *page_addr;
6225 unsigned int size = le16_to_cpu(rx_desc->wb.upper.length);
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006226#if (PAGE_SIZE < 8192)
Alexander Duyck74e238e2013-02-02 05:07:11 +00006227 unsigned int truesize = IGB_RX_BUFSZ;
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006228#else
Alexander Duyck74e238e2013-02-02 05:07:11 +00006229 unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
6230 SKB_DATA_ALIGN(NET_SKB_PAD +
6231 NET_IP_ALIGN +
6232 size);
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006233#endif
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006234
Alexander Duyck74e238e2013-02-02 05:07:11 +00006235 /* If we spanned a buffer we have a huge mess so test for it */
6236 BUG_ON(unlikely(!igb_test_staterr(rx_desc, E1000_RXD_STAT_EOP)));
6237
Alexander Duyck74e238e2013-02-02 05:07:11 +00006238 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
6239 page = rx_buffer->page;
6240 prefetchw(page);
6241
6242 page_addr = page_address(page) + rx_buffer->page_offset;
6243
6244 /* prefetch first cache line of first page */
6245 prefetch(page_addr + NET_SKB_PAD + NET_IP_ALIGN);
6246#if L1_CACHE_BYTES < 128
6247 prefetch(page_addr + L1_CACHE_BYTES + NET_SKB_PAD + NET_IP_ALIGN);
6248#endif
6249
6250 /* build an skb to around the page buffer */
6251 skb = build_skb(page_addr, truesize);
6252 if (unlikely(!skb)) {
6253 rx_ring->rx_stats.alloc_failed++;
6254 return NULL;
6255 }
6256
6257 /* we are reusing so sync this buffer for CPU use */
6258 dma_sync_single_range_for_cpu(rx_ring->dev,
6259 rx_buffer->dma,
6260 rx_buffer->page_offset,
6261 IGB_RX_BUFSZ,
6262 DMA_FROM_DEVICE);
6263
6264 /* update pointers within the skb to store the data */
6265 skb_reserve(skb, NET_IP_ALIGN + NET_SKB_PAD);
6266 __skb_put(skb, size);
6267
6268 /* pull timestamp out of packet data */
6269 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
6270 igb_ptp_rx_pktstamp(rx_ring->q_vector, skb->data, skb);
6271 __skb_pull(skb, IGB_TS_HDR_LEN);
6272 }
6273
6274 if (igb_can_reuse_rx_page(rx_buffer, page, truesize)) {
6275 /* hand second half of page back to the ring */
6276 igb_reuse_rx_page(rx_ring, rx_buffer);
6277 } else {
6278 /* we are not reusing the buffer so unmap it */
6279 dma_unmap_page(rx_ring->dev, rx_buffer->dma,
6280 PAGE_SIZE, DMA_FROM_DEVICE);
6281 }
6282
6283 /* clear contents of buffer_info */
6284 rx_buffer->dma = 0;
6285 rx_buffer->page = NULL;
6286
6287 return skb;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006288}
6289
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006290static struct sk_buff *igb_fetch_rx_buffer(struct igb_ring *rx_ring,
6291 union e1000_adv_rx_desc *rx_desc,
6292 struct sk_buff *skb)
6293{
6294 struct igb_rx_buffer *rx_buffer;
6295 struct page *page;
6296
6297 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
6298
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006299 page = rx_buffer->page;
6300 prefetchw(page);
6301
6302 if (likely(!skb)) {
6303 void *page_addr = page_address(page) +
6304 rx_buffer->page_offset;
6305
6306 /* prefetch first cache line of first page */
6307 prefetch(page_addr);
6308#if L1_CACHE_BYTES < 128
6309 prefetch(page_addr + L1_CACHE_BYTES);
6310#endif
6311
6312 /* allocate a skb to store the frags */
6313 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
6314 IGB_RX_HDR_LEN);
6315 if (unlikely(!skb)) {
6316 rx_ring->rx_stats.alloc_failed++;
6317 return NULL;
6318 }
6319
6320 /*
6321 * we will be copying header into skb->data in
6322 * pskb_may_pull so it is in our interest to prefetch
6323 * it now to avoid a possible cache miss
6324 */
6325 prefetchw(skb->data);
6326 }
6327
6328 /* we are reusing so sync this buffer for CPU use */
6329 dma_sync_single_range_for_cpu(rx_ring->dev,
6330 rx_buffer->dma,
6331 rx_buffer->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006332 IGB_RX_BUFSZ,
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006333 DMA_FROM_DEVICE);
6334
6335 /* pull page into skb */
6336 if (igb_add_rx_frag(rx_ring, rx_buffer, rx_desc, skb)) {
6337 /* hand second half of page back to the ring */
6338 igb_reuse_rx_page(rx_ring, rx_buffer);
6339 } else {
6340 /* we are not reusing the buffer so unmap it */
6341 dma_unmap_page(rx_ring->dev, rx_buffer->dma,
6342 PAGE_SIZE, DMA_FROM_DEVICE);
6343 }
6344
6345 /* clear contents of rx_buffer */
6346 rx_buffer->page = NULL;
6347
6348 return skb;
6349}
6350
Alexander Duyckcd392f52011-08-26 07:43:59 +00006351static inline void igb_rx_checksum(struct igb_ring *ring,
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006352 union e1000_adv_rx_desc *rx_desc,
6353 struct sk_buff *skb)
Auke Kok9d5c8242008-01-24 02:22:38 -08006354{
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006355 skb_checksum_none_assert(skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08006356
Alexander Duyck294e7d72011-08-26 07:45:57 +00006357 /* Ignore Checksum bit is set */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006358 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_IXSM))
Alexander Duyck294e7d72011-08-26 07:45:57 +00006359 return;
6360
6361 /* Rx checksum disabled via ethtool */
6362 if (!(ring->netdev->features & NETIF_F_RXCSUM))
Auke Kok9d5c8242008-01-24 02:22:38 -08006363 return;
Alexander Duyck85ad76b2009-10-27 15:52:46 +00006364
Auke Kok9d5c8242008-01-24 02:22:38 -08006365 /* TCP/UDP checksum error bit is set */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006366 if (igb_test_staterr(rx_desc,
6367 E1000_RXDEXT_STATERR_TCPE |
6368 E1000_RXDEXT_STATERR_IPE)) {
Jesse Brandeburgb9473562009-04-27 22:36:13 +00006369 /*
6370 * work around errata with sctp packets where the TCPE aka
6371 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
6372 * packets, (aka let the stack check the crc32c)
6373 */
Alexander Duyck866cff02011-08-26 07:45:36 +00006374 if (!((skb->len == 60) &&
6375 test_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
Eric Dumazet12dcd862010-10-15 17:27:10 +00006376 u64_stats_update_begin(&ring->rx_syncp);
Alexander Duyck04a5fcaa2009-10-27 15:52:27 +00006377 ring->rx_stats.csum_err++;
Eric Dumazet12dcd862010-10-15 17:27:10 +00006378 u64_stats_update_end(&ring->rx_syncp);
6379 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006380 /* let the stack verify checksum errors */
Auke Kok9d5c8242008-01-24 02:22:38 -08006381 return;
6382 }
6383 /* It must be a TCP or UDP packet with a valid checksum */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006384 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_TCPCS |
6385 E1000_RXD_STAT_UDPCS))
Auke Kok9d5c8242008-01-24 02:22:38 -08006386 skb->ip_summed = CHECKSUM_UNNECESSARY;
6387
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006388 dev_dbg(ring->dev, "cksum success: bits %08X\n",
6389 le32_to_cpu(rx_desc->wb.upper.status_error));
Auke Kok9d5c8242008-01-24 02:22:38 -08006390}
6391
Alexander Duyck077887c2011-08-26 07:46:29 +00006392static inline void igb_rx_hash(struct igb_ring *ring,
6393 union e1000_adv_rx_desc *rx_desc,
6394 struct sk_buff *skb)
6395{
6396 if (ring->netdev->features & NETIF_F_RXHASH)
6397 skb->rxhash = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
6398}
6399
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006400/**
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006401 * igb_is_non_eop - process handling of non-EOP buffers
6402 * @rx_ring: Rx ring being processed
6403 * @rx_desc: Rx descriptor for current buffer
6404 * @skb: current socket buffer containing buffer in progress
6405 *
6406 * This function updates next to clean. If the buffer is an EOP buffer
6407 * this function exits returning false, otherwise it will place the
6408 * sk_buff in the next buffer to be chained and return true indicating
6409 * that this is in fact a non-EOP buffer.
6410 **/
6411static bool igb_is_non_eop(struct igb_ring *rx_ring,
6412 union e1000_adv_rx_desc *rx_desc)
6413{
6414 u32 ntc = rx_ring->next_to_clean + 1;
6415
6416 /* fetch, update, and store next to clean */
6417 ntc = (ntc < rx_ring->count) ? ntc : 0;
6418 rx_ring->next_to_clean = ntc;
6419
6420 prefetch(IGB_RX_DESC(rx_ring, ntc));
6421
6422 if (likely(igb_test_staterr(rx_desc, E1000_RXD_STAT_EOP)))
6423 return false;
6424
6425 return true;
6426}
6427
6428/**
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006429 * igb_get_headlen - determine size of header for LRO/GRO
6430 * @data: pointer to the start of the headers
6431 * @max_len: total length of section to find headers in
6432 *
6433 * This function is meant to determine the length of headers that will
6434 * be recognized by hardware for LRO, and GRO offloads. The main
6435 * motivation of doing this is to only perform one pull for IPv4 TCP
6436 * packets so that we can do basic things like calculating the gso_size
6437 * based on the average data per packet.
6438 **/
6439static unsigned int igb_get_headlen(unsigned char *data,
6440 unsigned int max_len)
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006441{
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006442 union {
6443 unsigned char *network;
6444 /* l2 headers */
6445 struct ethhdr *eth;
6446 struct vlan_hdr *vlan;
6447 /* l3 headers */
6448 struct iphdr *ipv4;
6449 struct ipv6hdr *ipv6;
6450 } hdr;
6451 __be16 protocol;
6452 u8 nexthdr = 0; /* default to not TCP */
6453 u8 hlen;
6454
6455 /* this should never happen, but better safe than sorry */
6456 if (max_len < ETH_HLEN)
6457 return max_len;
6458
6459 /* initialize network frame pointer */
6460 hdr.network = data;
6461
6462 /* set first protocol and move network header forward */
6463 protocol = hdr.eth->h_proto;
6464 hdr.network += ETH_HLEN;
6465
6466 /* handle any vlan tag if present */
6467 if (protocol == __constant_htons(ETH_P_8021Q)) {
6468 if ((hdr.network - data) > (max_len - VLAN_HLEN))
6469 return max_len;
6470
6471 protocol = hdr.vlan->h_vlan_encapsulated_proto;
6472 hdr.network += VLAN_HLEN;
6473 }
6474
6475 /* handle L3 protocols */
6476 if (protocol == __constant_htons(ETH_P_IP)) {
6477 if ((hdr.network - data) > (max_len - sizeof(struct iphdr)))
6478 return max_len;
6479
6480 /* access ihl as a u8 to avoid unaligned access on ia64 */
6481 hlen = (hdr.network[0] & 0x0F) << 2;
6482
6483 /* verify hlen meets minimum size requirements */
6484 if (hlen < sizeof(struct iphdr))
6485 return hdr.network - data;
6486
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006487 /* record next protocol if header is present */
Alexander Duyckb9555f62013-02-01 08:56:47 +00006488 if (!(hdr.ipv4->frag_off & htons(IP_OFFSET)))
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006489 nexthdr = hdr.ipv4->protocol;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006490 } else if (protocol == __constant_htons(ETH_P_IPV6)) {
6491 if ((hdr.network - data) > (max_len - sizeof(struct ipv6hdr)))
6492 return max_len;
6493
6494 /* record next protocol */
6495 nexthdr = hdr.ipv6->nexthdr;
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006496 hlen = sizeof(struct ipv6hdr);
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006497 } else {
6498 return hdr.network - data;
6499 }
6500
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006501 /* relocate pointer to start of L4 header */
6502 hdr.network += hlen;
6503
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006504 /* finally sort out TCP */
6505 if (nexthdr == IPPROTO_TCP) {
6506 if ((hdr.network - data) > (max_len - sizeof(struct tcphdr)))
6507 return max_len;
6508
6509 /* access doff as a u8 to avoid unaligned access on ia64 */
6510 hlen = (hdr.network[12] & 0xF0) >> 2;
6511
6512 /* verify hlen meets minimum size requirements */
6513 if (hlen < sizeof(struct tcphdr))
6514 return hdr.network - data;
6515
6516 hdr.network += hlen;
6517 } else if (nexthdr == IPPROTO_UDP) {
6518 if ((hdr.network - data) > (max_len - sizeof(struct udphdr)))
6519 return max_len;
6520
6521 hdr.network += sizeof(struct udphdr);
6522 }
6523
6524 /*
6525 * If everything has gone correctly hdr.network should be the
6526 * data section of the packet and will be the end of the header.
6527 * If not then it probably represents the end of the last recognized
6528 * header.
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006529 */
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006530 if ((hdr.network - data) < max_len)
6531 return hdr.network - data;
6532 else
6533 return max_len;
6534}
6535
6536/**
6537 * igb_pull_tail - igb specific version of skb_pull_tail
6538 * @rx_ring: rx descriptor ring packet is being transacted on
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006539 * @rx_desc: pointer to the EOP Rx descriptor
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006540 * @skb: pointer to current skb being adjusted
6541 *
6542 * This function is an igb specific version of __pskb_pull_tail. The
6543 * main difference between this version and the original function is that
6544 * this function can make several assumptions about the state of things
6545 * that allow for significant optimizations versus the standard function.
6546 * As a result we can do things like drop a frag and maintain an accurate
6547 * truesize for the skb.
6548 */
6549static void igb_pull_tail(struct igb_ring *rx_ring,
6550 union e1000_adv_rx_desc *rx_desc,
6551 struct sk_buff *skb)
6552{
6553 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
6554 unsigned char *va;
6555 unsigned int pull_len;
6556
6557 /*
6558 * it is valid to use page_address instead of kmap since we are
6559 * working with pages allocated out of the lomem pool per
6560 * alloc_page(GFP_ATOMIC)
6561 */
6562 va = skb_frag_address(frag);
6563
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006564 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
6565 /* retrieve timestamp from buffer */
6566 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
6567
6568 /* update pointers to remove timestamp header */
6569 skb_frag_size_sub(frag, IGB_TS_HDR_LEN);
6570 frag->page_offset += IGB_TS_HDR_LEN;
6571 skb->data_len -= IGB_TS_HDR_LEN;
6572 skb->len -= IGB_TS_HDR_LEN;
6573
6574 /* move va to start of packet data */
6575 va += IGB_TS_HDR_LEN;
6576 }
6577
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006578 /*
6579 * we need the header to contain the greater of either ETH_HLEN or
6580 * 60 bytes if the skb->len is less than 60 for skb_pad.
6581 */
6582 pull_len = igb_get_headlen(va, IGB_RX_HDR_LEN);
6583
6584 /* align pull length to size of long to optimize memcpy performance */
6585 skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
6586
6587 /* update all of the pointers */
6588 skb_frag_size_sub(frag, pull_len);
6589 frag->page_offset += pull_len;
6590 skb->data_len -= pull_len;
6591 skb->tail += pull_len;
6592}
6593
6594/**
6595 * igb_cleanup_headers - Correct corrupted or empty headers
6596 * @rx_ring: rx descriptor ring packet is being transacted on
6597 * @rx_desc: pointer to the EOP Rx descriptor
6598 * @skb: pointer to current skb being fixed
6599 *
6600 * Address the case where we are pulling data in on pages only
6601 * and as such no data is present in the skb header.
6602 *
6603 * In addition if skb is not at least 60 bytes we need to pad it so that
6604 * it is large enough to qualify as a valid Ethernet frame.
6605 *
6606 * Returns true if an error was encountered and skb was freed.
6607 **/
6608static bool igb_cleanup_headers(struct igb_ring *rx_ring,
6609 union e1000_adv_rx_desc *rx_desc,
6610 struct sk_buff *skb)
6611{
6612
6613 if (unlikely((igb_test_staterr(rx_desc,
6614 E1000_RXDEXT_ERR_FRAME_ERR_MASK)))) {
6615 struct net_device *netdev = rx_ring->netdev;
6616 if (!(netdev->features & NETIF_F_RXALL)) {
6617 dev_kfree_skb_any(skb);
6618 return true;
6619 }
6620 }
6621
6622 /* place header in linear portion of buffer */
6623 if (skb_is_nonlinear(skb))
6624 igb_pull_tail(rx_ring, rx_desc, skb);
6625
6626 /* if skb_pad returns an error the skb was freed */
6627 if (unlikely(skb->len < 60)) {
6628 int pad_len = 60 - skb->len;
6629
6630 if (skb_pad(skb, pad_len))
6631 return true;
6632 __skb_put(skb, pad_len);
6633 }
6634
6635 return false;
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006636}
6637
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006638/**
6639 * igb_process_skb_fields - Populate skb header fields from Rx descriptor
6640 * @rx_ring: rx descriptor ring packet is being transacted on
6641 * @rx_desc: pointer to the EOP Rx descriptor
6642 * @skb: pointer to current skb being populated
6643 *
6644 * This function checks the ring, descriptor, and packet information in
6645 * order to populate the hash, checksum, VLAN, timestamp, protocol, and
6646 * other fields within the skb.
6647 **/
6648static void igb_process_skb_fields(struct igb_ring *rx_ring,
6649 union e1000_adv_rx_desc *rx_desc,
6650 struct sk_buff *skb)
6651{
6652 struct net_device *dev = rx_ring->netdev;
6653
6654 igb_rx_hash(rx_ring, rx_desc, skb);
6655
6656 igb_rx_checksum(rx_ring, rx_desc, skb);
6657
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006658 igb_ptp_rx_hwtstamp(rx_ring->q_vector, rx_desc, skb);
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006659
6660 if ((dev->features & NETIF_F_HW_VLAN_RX) &&
6661 igb_test_staterr(rx_desc, E1000_RXD_STAT_VP)) {
6662 u16 vid;
6663 if (igb_test_staterr(rx_desc, E1000_RXDEXT_STATERR_LB) &&
6664 test_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags))
6665 vid = be16_to_cpu(rx_desc->wb.upper.vlan);
6666 else
6667 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
6668
6669 __vlan_hwaccel_put_tag(skb, vid);
6670 }
6671
6672 skb_record_rx_queue(skb, rx_ring->queue_index);
6673
6674 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
6675}
6676
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006677static bool igb_clean_rx_irq(struct igb_q_vector *q_vector, const int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08006678{
Alexander Duyck0ba82992011-08-26 07:45:47 +00006679 struct igb_ring *rx_ring = q_vector->rx.ring;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006680 struct sk_buff *skb = rx_ring->skb;
Auke Kok9d5c8242008-01-24 02:22:38 -08006681 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck16eb8812011-08-26 07:43:54 +00006682 u16 cleaned_count = igb_desc_unused(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08006683
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006684 do {
6685 union e1000_adv_rx_desc *rx_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08006686
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006687 /* return some buffers to hardware, one at a time is too slow */
6688 if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
6689 igb_alloc_rx_buffers(rx_ring, cleaned_count);
6690 cleaned_count = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006691 }
6692
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006693 rx_desc = IGB_RX_DESC(rx_ring, rx_ring->next_to_clean);
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006694
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006695 if (!igb_test_staterr(rx_desc, E1000_RXD_STAT_DD))
6696 break;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006697
Alexander Duyck74e238e2013-02-02 05:07:11 +00006698 /* This memory barrier is needed to keep us from reading
6699 * any other fields out of the rx_desc until we know the
6700 * RXD_STAT_DD bit is set
6701 */
6702 rmb();
6703
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006704 /* retrieve a buffer from the ring */
Alexander Duyck74e238e2013-02-02 05:07:11 +00006705 if (ring_uses_build_skb(rx_ring))
6706 skb = igb_build_rx_buffer(rx_ring, rx_desc);
6707 else
6708 skb = igb_fetch_rx_buffer(rx_ring, rx_desc, skb);
Alexander Duyck16eb8812011-08-26 07:43:54 +00006709
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006710 /* exit if we failed to retrieve a buffer */
6711 if (!skb)
6712 break;
6713
6714 cleaned_count++;
6715
6716 /* fetch next buffer in frame if non-eop */
6717 if (igb_is_non_eop(rx_ring, rx_desc))
6718 continue;
Alexander Duyck44390ca2011-08-26 07:43:38 +00006719
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006720 /* verify the packet layout is correct */
6721 if (igb_cleanup_headers(rx_ring, rx_desc, skb)) {
6722 skb = NULL;
6723 continue;
Auke Kok9d5c8242008-01-24 02:22:38 -08006724 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006725
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006726 /* probably a little skewed due to removing CRC */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006727 total_bytes += skb->len;
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006728
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006729 /* populate checksum, timestamp, VLAN, and protocol */
6730 igb_process_skb_fields(rx_ring, rx_desc, skb);
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006731
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006732 napi_gro_receive(&q_vector->napi, skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08006733
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006734 /* reset skb pointer */
6735 skb = NULL;
6736
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006737 /* update budget accounting */
6738 total_packets++;
6739 } while (likely(total_packets < budget));
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006740
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006741 /* place incomplete frames back on ring for completion */
6742 rx_ring->skb = skb;
6743
Eric Dumazet12dcd862010-10-15 17:27:10 +00006744 u64_stats_update_begin(&rx_ring->rx_syncp);
Auke Kok9d5c8242008-01-24 02:22:38 -08006745 rx_ring->rx_stats.packets += total_packets;
6746 rx_ring->rx_stats.bytes += total_bytes;
Eric Dumazet12dcd862010-10-15 17:27:10 +00006747 u64_stats_update_end(&rx_ring->rx_syncp);
Alexander Duyck0ba82992011-08-26 07:45:47 +00006748 q_vector->rx.total_packets += total_packets;
6749 q_vector->rx.total_bytes += total_bytes;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006750
6751 if (cleaned_count)
Alexander Duyckcd392f52011-08-26 07:43:59 +00006752 igb_alloc_rx_buffers(rx_ring, cleaned_count);
Alexander Duyckc023cd82011-08-26 07:43:43 +00006753
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006754 return (total_packets < budget);
Auke Kok9d5c8242008-01-24 02:22:38 -08006755}
6756
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006757static bool igb_alloc_mapped_page(struct igb_ring *rx_ring,
6758 struct igb_rx_buffer *bi)
Alexander Duyckc023cd82011-08-26 07:43:43 +00006759{
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006760 struct page *page = bi->page;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006761 dma_addr_t dma;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006762
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006763 /* since we are recycling buffers we should seldom need to alloc */
6764 if (likely(page))
Alexander Duyckc023cd82011-08-26 07:43:43 +00006765 return true;
6766
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006767 /* alloc new page for storage */
6768 page = __skb_alloc_page(GFP_ATOMIC | __GFP_COLD, NULL);
6769 if (unlikely(!page)) {
6770 rx_ring->rx_stats.alloc_failed++;
6771 return false;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006772 }
6773
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006774 /* map page for use */
6775 dma = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
Alexander Duyckc023cd82011-08-26 07:43:43 +00006776
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006777 /*
6778 * if mapping failed free memory back to system since
6779 * there isn't much point in holding memory we can't use
6780 */
Alexander Duyckc023cd82011-08-26 07:43:43 +00006781 if (dma_mapping_error(rx_ring->dev, dma)) {
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006782 __free_page(page);
6783
Alexander Duyckc023cd82011-08-26 07:43:43 +00006784 rx_ring->rx_stats.alloc_failed++;
6785 return false;
6786 }
6787
6788 bi->dma = dma;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006789 bi->page = page;
6790 bi->page_offset = 0;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006791
Alexander Duyckc023cd82011-08-26 07:43:43 +00006792 return true;
6793}
6794
Alexander Duyck74e238e2013-02-02 05:07:11 +00006795static inline unsigned int igb_rx_offset(struct igb_ring *rx_ring)
6796{
6797 if (ring_uses_build_skb(rx_ring))
6798 return NET_SKB_PAD + NET_IP_ALIGN;
6799 else
6800 return 0;
6801}
6802
Auke Kok9d5c8242008-01-24 02:22:38 -08006803/**
Alexander Duyckcd392f52011-08-26 07:43:59 +00006804 * igb_alloc_rx_buffers - Replace used receive buffers; packet split
Auke Kok9d5c8242008-01-24 02:22:38 -08006805 * @adapter: address of board private structure
6806 **/
Alexander Duyckcd392f52011-08-26 07:43:59 +00006807void igb_alloc_rx_buffers(struct igb_ring *rx_ring, u16 cleaned_count)
Auke Kok9d5c8242008-01-24 02:22:38 -08006808{
Auke Kok9d5c8242008-01-24 02:22:38 -08006809 union e1000_adv_rx_desc *rx_desc;
Alexander Duyck06034642011-08-26 07:44:22 +00006810 struct igb_rx_buffer *bi;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006811 u16 i = rx_ring->next_to_use;
Auke Kok9d5c8242008-01-24 02:22:38 -08006812
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006813 /* nothing to do */
6814 if (!cleaned_count)
6815 return;
6816
Alexander Duyck601369062011-08-26 07:44:05 +00006817 rx_desc = IGB_RX_DESC(rx_ring, i);
Alexander Duyck06034642011-08-26 07:44:22 +00006818 bi = &rx_ring->rx_buffer_info[i];
Alexander Duyckc023cd82011-08-26 07:43:43 +00006819 i -= rx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08006820
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006821 do {
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006822 if (!igb_alloc_mapped_page(rx_ring, bi))
Alexander Duyckc023cd82011-08-26 07:43:43 +00006823 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08006824
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006825 /*
6826 * Refresh the desc even if buffer_addrs didn't change
6827 * because each write-back erases this info.
6828 */
Alexander Duyck74e238e2013-02-02 05:07:11 +00006829 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma +
6830 bi->page_offset +
6831 igb_rx_offset(rx_ring));
Auke Kok9d5c8242008-01-24 02:22:38 -08006832
Alexander Duyckc023cd82011-08-26 07:43:43 +00006833 rx_desc++;
6834 bi++;
Auke Kok9d5c8242008-01-24 02:22:38 -08006835 i++;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006836 if (unlikely(!i)) {
Alexander Duyck601369062011-08-26 07:44:05 +00006837 rx_desc = IGB_RX_DESC(rx_ring, 0);
Alexander Duyck06034642011-08-26 07:44:22 +00006838 bi = rx_ring->rx_buffer_info;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006839 i -= rx_ring->count;
6840 }
6841
6842 /* clear the hdr_addr for the next_to_use descriptor */
6843 rx_desc->read.hdr_addr = 0;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006844
6845 cleaned_count--;
6846 } while (cleaned_count);
Auke Kok9d5c8242008-01-24 02:22:38 -08006847
Alexander Duyckc023cd82011-08-26 07:43:43 +00006848 i += rx_ring->count;
6849
Auke Kok9d5c8242008-01-24 02:22:38 -08006850 if (rx_ring->next_to_use != i) {
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006851 /* record the next descriptor to use */
Auke Kok9d5c8242008-01-24 02:22:38 -08006852 rx_ring->next_to_use = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08006853
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006854 /* update next to alloc since we have filled the ring */
6855 rx_ring->next_to_alloc = i;
6856
6857 /*
6858 * Force memory writes to complete before letting h/w
Auke Kok9d5c8242008-01-24 02:22:38 -08006859 * know there are new descriptors to fetch. (Only
6860 * applicable for weak-ordered memory model archs,
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006861 * such as IA-64).
6862 */
Auke Kok9d5c8242008-01-24 02:22:38 -08006863 wmb();
Alexander Duyckfce99e32009-10-27 15:51:27 +00006864 writel(i, rx_ring->tail);
Auke Kok9d5c8242008-01-24 02:22:38 -08006865 }
6866}
6867
6868/**
6869 * igb_mii_ioctl -
6870 * @netdev:
6871 * @ifreq:
6872 * @cmd:
6873 **/
6874static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6875{
6876 struct igb_adapter *adapter = netdev_priv(netdev);
6877 struct mii_ioctl_data *data = if_mii(ifr);
6878
6879 if (adapter->hw.phy.media_type != e1000_media_type_copper)
6880 return -EOPNOTSUPP;
6881
6882 switch (cmd) {
6883 case SIOCGMIIPHY:
6884 data->phy_id = adapter->hw.phy.addr;
6885 break;
6886 case SIOCGMIIREG:
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08006887 if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
6888 &data->val_out))
Auke Kok9d5c8242008-01-24 02:22:38 -08006889 return -EIO;
6890 break;
6891 case SIOCSMIIREG:
6892 default:
6893 return -EOPNOTSUPP;
6894 }
6895 return 0;
6896}
6897
6898/**
6899 * igb_ioctl -
6900 * @netdev:
6901 * @ifreq:
6902 * @cmd:
6903 **/
6904static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6905{
6906 switch (cmd) {
6907 case SIOCGMIIPHY:
6908 case SIOCGMIIREG:
6909 case SIOCSMIIREG:
6910 return igb_mii_ioctl(netdev, ifr, cmd);
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00006911 case SIOCSHWTSTAMP:
Matthew Vicka79f4f82012-08-10 05:40:44 +00006912 return igb_ptp_hwtstamp_ioctl(netdev, ifr, cmd);
Auke Kok9d5c8242008-01-24 02:22:38 -08006913 default:
6914 return -EOPNOTSUPP;
6915 }
6916}
6917
Alexander Duyck009bc062009-07-23 18:08:35 +00006918s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6919{
6920 struct igb_adapter *adapter = hw->back;
Alexander Duyck009bc062009-07-23 18:08:35 +00006921
Jiang Liu23d028c2012-08-20 13:32:20 -06006922 if (pcie_capability_read_word(adapter->pdev, reg, value))
Alexander Duyck009bc062009-07-23 18:08:35 +00006923 return -E1000_ERR_CONFIG;
6924
Alexander Duyck009bc062009-07-23 18:08:35 +00006925 return 0;
6926}
6927
6928s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6929{
6930 struct igb_adapter *adapter = hw->back;
Alexander Duyck009bc062009-07-23 18:08:35 +00006931
Jiang Liu23d028c2012-08-20 13:32:20 -06006932 if (pcie_capability_write_word(adapter->pdev, reg, *value))
Alexander Duyck009bc062009-07-23 18:08:35 +00006933 return -E1000_ERR_CONFIG;
6934
Alexander Duyck009bc062009-07-23 18:08:35 +00006935 return 0;
6936}
6937
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006938static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features)
Auke Kok9d5c8242008-01-24 02:22:38 -08006939{
6940 struct igb_adapter *adapter = netdev_priv(netdev);
6941 struct e1000_hw *hw = &adapter->hw;
6942 u32 ctrl, rctl;
Alexander Duyck5faf0302011-08-26 07:46:08 +00006943 bool enable = !!(features & NETIF_F_HW_VLAN_RX);
Auke Kok9d5c8242008-01-24 02:22:38 -08006944
Alexander Duyck5faf0302011-08-26 07:46:08 +00006945 if (enable) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006946 /* enable VLAN tag insert/strip */
6947 ctrl = rd32(E1000_CTRL);
6948 ctrl |= E1000_CTRL_VME;
6949 wr32(E1000_CTRL, ctrl);
6950
Alexander Duyck51466232009-10-27 23:47:35 +00006951 /* Disable CFI check */
Auke Kok9d5c8242008-01-24 02:22:38 -08006952 rctl = rd32(E1000_RCTL);
Auke Kok9d5c8242008-01-24 02:22:38 -08006953 rctl &= ~E1000_RCTL_CFIEN;
6954 wr32(E1000_RCTL, rctl);
Auke Kok9d5c8242008-01-24 02:22:38 -08006955 } else {
6956 /* disable VLAN tag insert/strip */
6957 ctrl = rd32(E1000_CTRL);
6958 ctrl &= ~E1000_CTRL_VME;
6959 wr32(E1000_CTRL, ctrl);
Auke Kok9d5c8242008-01-24 02:22:38 -08006960 }
6961
Alexander Duycke1739522009-02-19 20:39:44 -08006962 igb_rlpml_set(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006963}
6964
Jiri Pirko8e586132011-12-08 19:52:37 -05006965static int igb_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
Auke Kok9d5c8242008-01-24 02:22:38 -08006966{
6967 struct igb_adapter *adapter = netdev_priv(netdev);
6968 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006969 int pf_id = adapter->vfs_allocated_count;
Auke Kok9d5c8242008-01-24 02:22:38 -08006970
Alexander Duyck51466232009-10-27 23:47:35 +00006971 /* attempt to add filter to vlvf array */
6972 igb_vlvf_set(adapter, vid, true, pf_id);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006973
Alexander Duyck51466232009-10-27 23:47:35 +00006974 /* add the filter since PF can receive vlans w/o entry in vlvf */
6975 igb_vfta_set(hw, vid, true);
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006976
6977 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05006978
6979 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08006980}
6981
Jiri Pirko8e586132011-12-08 19:52:37 -05006982static int igb_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
Auke Kok9d5c8242008-01-24 02:22:38 -08006983{
6984 struct igb_adapter *adapter = netdev_priv(netdev);
6985 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006986 int pf_id = adapter->vfs_allocated_count;
Alexander Duyck51466232009-10-27 23:47:35 +00006987 s32 err;
Auke Kok9d5c8242008-01-24 02:22:38 -08006988
Alexander Duyck51466232009-10-27 23:47:35 +00006989 /* remove vlan from VLVF table array */
6990 err = igb_vlvf_set(adapter, vid, false, pf_id);
Auke Kok9d5c8242008-01-24 02:22:38 -08006991
Alexander Duyck51466232009-10-27 23:47:35 +00006992 /* if vid was not present in VLVF just remove it from table */
6993 if (err)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006994 igb_vfta_set(hw, vid, false);
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006995
6996 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05006997
6998 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08006999}
7000
7001static void igb_restore_vlan(struct igb_adapter *adapter)
7002{
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00007003 u16 vid;
Auke Kok9d5c8242008-01-24 02:22:38 -08007004
Alexander Duyck5faf0302011-08-26 07:46:08 +00007005 igb_vlan_mode(adapter->netdev, adapter->netdev->features);
7006
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00007007 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
7008 igb_vlan_rx_add_vid(adapter->netdev, vid);
Auke Kok9d5c8242008-01-24 02:22:38 -08007009}
7010
David Decotigny14ad2512011-04-27 18:32:43 +00007011int igb_set_spd_dplx(struct igb_adapter *adapter, u32 spd, u8 dplx)
Auke Kok9d5c8242008-01-24 02:22:38 -08007012{
Alexander Duyck090b1792009-10-27 23:51:55 +00007013 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08007014 struct e1000_mac_info *mac = &adapter->hw.mac;
7015
7016 mac->autoneg = 0;
7017
David Decotigny14ad2512011-04-27 18:32:43 +00007018 /* Make sure dplx is at most 1 bit and lsb of speed is not set
7019 * for the switch() below to work */
7020 if ((spd & 1) || (dplx & ~1))
7021 goto err_inval;
7022
Akeem G. Abodunrinf502ef72013-04-05 16:49:06 +00007023 /* Fiber NIC's only allow 1000 gbps Full duplex
7024 * and 100Mbps Full duplex for 100baseFx sfp
7025 */
7026 if (adapter->hw.phy.media_type == e1000_media_type_internal_serdes) {
7027 switch (spd + dplx) {
7028 case SPEED_10 + DUPLEX_HALF:
7029 case SPEED_10 + DUPLEX_FULL:
7030 case SPEED_100 + DUPLEX_HALF:
7031 goto err_inval;
7032 default:
7033 break;
7034 }
7035 }
Carolyn Wybornycd2638a2010-10-12 22:27:02 +00007036
David Decotigny14ad2512011-04-27 18:32:43 +00007037 switch (spd + dplx) {
Auke Kok9d5c8242008-01-24 02:22:38 -08007038 case SPEED_10 + DUPLEX_HALF:
7039 mac->forced_speed_duplex = ADVERTISE_10_HALF;
7040 break;
7041 case SPEED_10 + DUPLEX_FULL:
7042 mac->forced_speed_duplex = ADVERTISE_10_FULL;
7043 break;
7044 case SPEED_100 + DUPLEX_HALF:
7045 mac->forced_speed_duplex = ADVERTISE_100_HALF;
7046 break;
7047 case SPEED_100 + DUPLEX_FULL:
7048 mac->forced_speed_duplex = ADVERTISE_100_FULL;
7049 break;
7050 case SPEED_1000 + DUPLEX_FULL:
7051 mac->autoneg = 1;
7052 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
7053 break;
7054 case SPEED_1000 + DUPLEX_HALF: /* not supported */
7055 default:
David Decotigny14ad2512011-04-27 18:32:43 +00007056 goto err_inval;
Auke Kok9d5c8242008-01-24 02:22:38 -08007057 }
Jesse Brandeburg8376dad2012-07-26 02:31:19 +00007058
7059 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
7060 adapter->hw.phy.mdix = AUTO_ALL_MODES;
7061
Auke Kok9d5c8242008-01-24 02:22:38 -08007062 return 0;
David Decotigny14ad2512011-04-27 18:32:43 +00007063
7064err_inval:
7065 dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n");
7066 return -EINVAL;
Auke Kok9d5c8242008-01-24 02:22:38 -08007067}
7068
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007069static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake,
7070 bool runtime)
Auke Kok9d5c8242008-01-24 02:22:38 -08007071{
7072 struct net_device *netdev = pci_get_drvdata(pdev);
7073 struct igb_adapter *adapter = netdev_priv(netdev);
7074 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07007075 u32 ctrl, rctl, status;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007076 u32 wufc = runtime ? E1000_WUFC_LNKC : adapter->wol;
Auke Kok9d5c8242008-01-24 02:22:38 -08007077#ifdef CONFIG_PM
7078 int retval = 0;
7079#endif
7080
7081 netif_device_detach(netdev);
7082
Alexander Duycka88f10e2008-07-08 15:13:38 -07007083 if (netif_running(netdev))
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007084 __igb_close(netdev, true);
Alexander Duycka88f10e2008-07-08 15:13:38 -07007085
Alexander Duyck047e0032009-10-27 15:49:27 +00007086 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08007087
7088#ifdef CONFIG_PM
7089 retval = pci_save_state(pdev);
7090 if (retval)
7091 return retval;
7092#endif
7093
7094 status = rd32(E1000_STATUS);
7095 if (status & E1000_STATUS_LU)
7096 wufc &= ~E1000_WUFC_LNKC;
7097
7098 if (wufc) {
7099 igb_setup_rctl(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00007100 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08007101
7102 /* turn on all-multi mode if wake on multicast is enabled */
7103 if (wufc & E1000_WUFC_MC) {
7104 rctl = rd32(E1000_RCTL);
7105 rctl |= E1000_RCTL_MPE;
7106 wr32(E1000_RCTL, rctl);
7107 }
7108
7109 ctrl = rd32(E1000_CTRL);
7110 /* advertise wake from D3Cold */
7111 #define E1000_CTRL_ADVD3WUC 0x00100000
7112 /* phy power management enable */
7113 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
7114 ctrl |= E1000_CTRL_ADVD3WUC;
7115 wr32(E1000_CTRL, ctrl);
7116
Auke Kok9d5c8242008-01-24 02:22:38 -08007117 /* Allow time for pending master requests to run */
Alexander Duyck330a6d62009-10-27 23:51:35 +00007118 igb_disable_pcie_master(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08007119
7120 wr32(E1000_WUC, E1000_WUC_PME_EN);
7121 wr32(E1000_WUFC, wufc);
Auke Kok9d5c8242008-01-24 02:22:38 -08007122 } else {
7123 wr32(E1000_WUC, 0);
7124 wr32(E1000_WUFC, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08007125 }
7126
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00007127 *enable_wake = wufc || adapter->en_mng_pt;
7128 if (!*enable_wake)
Nick Nunley88a268c2010-02-17 01:01:59 +00007129 igb_power_down_link(adapter);
7130 else
7131 igb_power_up_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08007132
7133 /* Release control of h/w to f/w. If f/w is AMT enabled, this
7134 * would have already happened in close and is redundant. */
7135 igb_release_hw_control(adapter);
7136
7137 pci_disable_device(pdev);
7138
Auke Kok9d5c8242008-01-24 02:22:38 -08007139 return 0;
7140}
7141
7142#ifdef CONFIG_PM
Emil Tantilovd9dd9662012-01-28 08:10:35 +00007143#ifdef CONFIG_PM_SLEEP
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007144static int igb_suspend(struct device *dev)
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00007145{
7146 int retval;
7147 bool wake;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007148 struct pci_dev *pdev = to_pci_dev(dev);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00007149
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007150 retval = __igb_shutdown(pdev, &wake, 0);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00007151 if (retval)
7152 return retval;
7153
7154 if (wake) {
7155 pci_prepare_to_sleep(pdev);
7156 } else {
7157 pci_wake_from_d3(pdev, false);
7158 pci_set_power_state(pdev, PCI_D3hot);
7159 }
7160
7161 return 0;
7162}
Emil Tantilovd9dd9662012-01-28 08:10:35 +00007163#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00007164
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007165static int igb_resume(struct device *dev)
Auke Kok9d5c8242008-01-24 02:22:38 -08007166{
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007167 struct pci_dev *pdev = to_pci_dev(dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08007168 struct net_device *netdev = pci_get_drvdata(pdev);
7169 struct igb_adapter *adapter = netdev_priv(netdev);
7170 struct e1000_hw *hw = &adapter->hw;
7171 u32 err;
7172
7173 pci_set_power_state(pdev, PCI_D0);
7174 pci_restore_state(pdev);
Nick Nunleyb94f2d72010-02-17 01:02:19 +00007175 pci_save_state(pdev);
Taku Izumi42bfd33a2008-06-20 12:10:30 +09007176
Alexander Duyckaed5dec2009-02-06 23:16:04 +00007177 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08007178 if (err) {
7179 dev_err(&pdev->dev,
7180 "igb: Cannot enable PCI device from suspend\n");
7181 return err;
7182 }
7183 pci_set_master(pdev);
7184
7185 pci_enable_wake(pdev, PCI_D3hot, 0);
7186 pci_enable_wake(pdev, PCI_D3cold, 0);
7187
Stefan Assmann53c7d062012-12-04 06:00:12 +00007188 if (igb_init_interrupt_scheme(adapter, true)) {
Alexander Duycka88f10e2008-07-08 15:13:38 -07007189 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
7190 return -ENOMEM;
Auke Kok9d5c8242008-01-24 02:22:38 -08007191 }
7192
Auke Kok9d5c8242008-01-24 02:22:38 -08007193 igb_reset(adapter);
Alexander Duycka8564f02009-02-06 23:21:10 +00007194
7195 /* let the f/w know that the h/w is now under the control of the
7196 * driver. */
7197 igb_get_hw_control(adapter);
7198
Auke Kok9d5c8242008-01-24 02:22:38 -08007199 wr32(E1000_WUS, ~0);
7200
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007201 if (netdev->flags & IFF_UP) {
Alexander Duyck0c2cc022012-09-25 00:31:22 +00007202 rtnl_lock();
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007203 err = __igb_open(netdev, true);
Alexander Duyck0c2cc022012-09-25 00:31:22 +00007204 rtnl_unlock();
Alexander Duycka88f10e2008-07-08 15:13:38 -07007205 if (err)
7206 return err;
7207 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007208
7209 netif_device_attach(netdev);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007210 return 0;
7211}
7212
7213#ifdef CONFIG_PM_RUNTIME
7214static int igb_runtime_idle(struct device *dev)
7215{
7216 struct pci_dev *pdev = to_pci_dev(dev);
7217 struct net_device *netdev = pci_get_drvdata(pdev);
7218 struct igb_adapter *adapter = netdev_priv(netdev);
7219
7220 if (!igb_has_link(adapter))
7221 pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
7222
7223 return -EBUSY;
7224}
7225
7226static int igb_runtime_suspend(struct device *dev)
7227{
7228 struct pci_dev *pdev = to_pci_dev(dev);
7229 int retval;
7230 bool wake;
7231
7232 retval = __igb_shutdown(pdev, &wake, 1);
7233 if (retval)
7234 return retval;
7235
7236 if (wake) {
7237 pci_prepare_to_sleep(pdev);
7238 } else {
7239 pci_wake_from_d3(pdev, false);
7240 pci_set_power_state(pdev, PCI_D3hot);
7241 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007242
Auke Kok9d5c8242008-01-24 02:22:38 -08007243 return 0;
7244}
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007245
7246static int igb_runtime_resume(struct device *dev)
7247{
7248 return igb_resume(dev);
7249}
7250#endif /* CONFIG_PM_RUNTIME */
Auke Kok9d5c8242008-01-24 02:22:38 -08007251#endif
7252
7253static void igb_shutdown(struct pci_dev *pdev)
7254{
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00007255 bool wake;
7256
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007257 __igb_shutdown(pdev, &wake, 0);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00007258
7259 if (system_state == SYSTEM_POWER_OFF) {
7260 pci_wake_from_d3(pdev, wake);
7261 pci_set_power_state(pdev, PCI_D3hot);
7262 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007263}
7264
Greg Rosefa44f2f2013-01-17 01:03:06 -08007265#ifdef CONFIG_PCI_IOV
7266static int igb_sriov_reinit(struct pci_dev *dev)
7267{
7268 struct net_device *netdev = pci_get_drvdata(dev);
7269 struct igb_adapter *adapter = netdev_priv(netdev);
7270 struct pci_dev *pdev = adapter->pdev;
7271
7272 rtnl_lock();
7273
7274 if (netif_running(netdev))
7275 igb_close(netdev);
7276
7277 igb_clear_interrupt_scheme(adapter);
7278
7279 igb_init_queue_configuration(adapter);
7280
7281 if (igb_init_interrupt_scheme(adapter, true)) {
7282 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
7283 return -ENOMEM;
7284 }
7285
7286 if (netif_running(netdev))
7287 igb_open(netdev);
7288
7289 rtnl_unlock();
7290
7291 return 0;
7292}
7293
7294static int igb_pci_disable_sriov(struct pci_dev *dev)
7295{
7296 int err = igb_disable_sriov(dev);
7297
7298 if (!err)
7299 err = igb_sriov_reinit(dev);
7300
7301 return err;
7302}
7303
7304static int igb_pci_enable_sriov(struct pci_dev *dev, int num_vfs)
7305{
7306 int err = igb_enable_sriov(dev, num_vfs);
7307
7308 if (err)
7309 goto out;
7310
7311 err = igb_sriov_reinit(dev);
7312 if (!err)
7313 return num_vfs;
7314
7315out:
7316 return err;
7317}
7318
7319#endif
7320static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
7321{
7322#ifdef CONFIG_PCI_IOV
7323 if (num_vfs == 0)
7324 return igb_pci_disable_sriov(dev);
7325 else
7326 return igb_pci_enable_sriov(dev, num_vfs);
7327#endif
7328 return 0;
7329}
7330
Auke Kok9d5c8242008-01-24 02:22:38 -08007331#ifdef CONFIG_NET_POLL_CONTROLLER
7332/*
7333 * Polling 'interrupt' - used by things like netconsole to send skbs
7334 * without having to re-enable interrupts. It's not called while
7335 * the interrupt routine is executing.
7336 */
7337static void igb_netpoll(struct net_device *netdev)
7338{
7339 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00007340 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00007341 struct igb_q_vector *q_vector;
Auke Kok9d5c8242008-01-24 02:22:38 -08007342 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08007343
Alexander Duyck047e0032009-10-27 15:49:27 +00007344 for (i = 0; i < adapter->num_q_vectors; i++) {
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00007345 q_vector = adapter->q_vector[i];
7346 if (adapter->msix_entries)
7347 wr32(E1000_EIMC, q_vector->eims_value);
7348 else
7349 igb_irq_disable(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00007350 napi_schedule(&q_vector->napi);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00007351 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007352}
7353#endif /* CONFIG_NET_POLL_CONTROLLER */
7354
7355/**
7356 * igb_io_error_detected - called when PCI error is detected
7357 * @pdev: Pointer to PCI device
7358 * @state: The current pci connection state
7359 *
7360 * This function is called after a PCI bus error affecting
7361 * this device has been detected.
7362 */
7363static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
7364 pci_channel_state_t state)
7365{
7366 struct net_device *netdev = pci_get_drvdata(pdev);
7367 struct igb_adapter *adapter = netdev_priv(netdev);
7368
7369 netif_device_detach(netdev);
7370
Alexander Duyck59ed6ee2009-06-30 12:46:34 +00007371 if (state == pci_channel_io_perm_failure)
7372 return PCI_ERS_RESULT_DISCONNECT;
7373
Auke Kok9d5c8242008-01-24 02:22:38 -08007374 if (netif_running(netdev))
7375 igb_down(adapter);
7376 pci_disable_device(pdev);
7377
7378 /* Request a slot slot reset. */
7379 return PCI_ERS_RESULT_NEED_RESET;
7380}
7381
7382/**
7383 * igb_io_slot_reset - called after the pci bus has been reset.
7384 * @pdev: Pointer to PCI device
7385 *
7386 * Restart the card from scratch, as if from a cold-boot. Implementation
7387 * resembles the first-half of the igb_resume routine.
7388 */
7389static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
7390{
7391 struct net_device *netdev = pci_get_drvdata(pdev);
7392 struct igb_adapter *adapter = netdev_priv(netdev);
7393 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck40a914f2008-11-27 00:24:37 -08007394 pci_ers_result_t result;
Taku Izumi42bfd33a2008-06-20 12:10:30 +09007395 int err;
Auke Kok9d5c8242008-01-24 02:22:38 -08007396
Alexander Duyckaed5dec2009-02-06 23:16:04 +00007397 if (pci_enable_device_mem(pdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08007398 dev_err(&pdev->dev,
7399 "Cannot re-enable PCI device after reset.\n");
Alexander Duyck40a914f2008-11-27 00:24:37 -08007400 result = PCI_ERS_RESULT_DISCONNECT;
7401 } else {
7402 pci_set_master(pdev);
7403 pci_restore_state(pdev);
Nick Nunleyb94f2d72010-02-17 01:02:19 +00007404 pci_save_state(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08007405
7406 pci_enable_wake(pdev, PCI_D3hot, 0);
7407 pci_enable_wake(pdev, PCI_D3cold, 0);
7408
7409 igb_reset(adapter);
7410 wr32(E1000_WUS, ~0);
7411 result = PCI_ERS_RESULT_RECOVERED;
Auke Kok9d5c8242008-01-24 02:22:38 -08007412 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007413
Jeff Kirsherea943d42008-12-11 20:34:19 -08007414 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7415 if (err) {
7416 dev_err(&pdev->dev, "pci_cleanup_aer_uncorrect_error_status "
7417 "failed 0x%0x\n", err);
7418 /* non-fatal, continue */
7419 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007420
Alexander Duyck40a914f2008-11-27 00:24:37 -08007421 return result;
Auke Kok9d5c8242008-01-24 02:22:38 -08007422}
7423
7424/**
7425 * igb_io_resume - called when traffic can start flowing again.
7426 * @pdev: Pointer to PCI device
7427 *
7428 * This callback is called when the error recovery driver tells us that
7429 * its OK to resume normal operation. Implementation resembles the
7430 * second-half of the igb_resume routine.
7431 */
7432static void igb_io_resume(struct pci_dev *pdev)
7433{
7434 struct net_device *netdev = pci_get_drvdata(pdev);
7435 struct igb_adapter *adapter = netdev_priv(netdev);
7436
Auke Kok9d5c8242008-01-24 02:22:38 -08007437 if (netif_running(netdev)) {
7438 if (igb_up(adapter)) {
7439 dev_err(&pdev->dev, "igb_up failed after reset\n");
7440 return;
7441 }
7442 }
7443
7444 netif_device_attach(netdev);
7445
7446 /* let the f/w know that the h/w is now under the control of the
7447 * driver. */
7448 igb_get_hw_control(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08007449}
7450
Alexander Duyck26ad9172009-10-05 06:32:49 +00007451static void igb_rar_set_qsel(struct igb_adapter *adapter, u8 *addr, u32 index,
7452 u8 qsel)
7453{
7454 u32 rar_low, rar_high;
7455 struct e1000_hw *hw = &adapter->hw;
7456
7457 /* HW expects these in little endian so we reverse the byte order
7458 * from network order (big endian) to little endian
7459 */
7460 rar_low = ((u32) addr[0] | ((u32) addr[1] << 8) |
7461 ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
7462 rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
7463
7464 /* Indicate to hardware the Address is Valid. */
7465 rar_high |= E1000_RAH_AV;
7466
7467 if (hw->mac.type == e1000_82575)
7468 rar_high |= E1000_RAH_POOL_1 * qsel;
7469 else
7470 rar_high |= E1000_RAH_POOL_1 << qsel;
7471
7472 wr32(E1000_RAL(index), rar_low);
7473 wrfl();
7474 wr32(E1000_RAH(index), rar_high);
7475 wrfl();
7476}
7477
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007478static int igb_set_vf_mac(struct igb_adapter *adapter,
7479 int vf, unsigned char *mac_addr)
7480{
7481 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00007482 /* VF MAC addresses start at end of receive addresses and moves
7483 * torwards the first, as a result a collision should not be possible */
7484 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007485
Alexander Duyck37680112009-02-19 20:40:30 -08007486 memcpy(adapter->vf_data[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007487
Alexander Duyck26ad9172009-10-05 06:32:49 +00007488 igb_rar_set_qsel(adapter, mac_addr, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007489
7490 return 0;
7491}
7492
Williams, Mitch A8151d292010-02-10 01:44:24 +00007493static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
7494{
7495 struct igb_adapter *adapter = netdev_priv(netdev);
7496 if (!is_valid_ether_addr(mac) || (vf >= adapter->vfs_allocated_count))
7497 return -EINVAL;
7498 adapter->vf_data[vf].flags |= IGB_VF_FLAG_PF_SET_MAC;
7499 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
7500 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
7501 " change effective.");
7502 if (test_bit(__IGB_DOWN, &adapter->state)) {
7503 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
7504 " but the PF device is not up.\n");
7505 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
7506 " attempting to use the VF device.\n");
7507 }
7508 return igb_set_vf_mac(adapter, vf, mac);
7509}
7510
Lior Levy17dc5662011-02-08 02:28:46 +00007511static int igb_link_mbps(int internal_link_speed)
7512{
7513 switch (internal_link_speed) {
7514 case SPEED_100:
7515 return 100;
7516 case SPEED_1000:
7517 return 1000;
7518 default:
7519 return 0;
7520 }
7521}
7522
7523static void igb_set_vf_rate_limit(struct e1000_hw *hw, int vf, int tx_rate,
7524 int link_speed)
7525{
7526 int rf_dec, rf_int;
7527 u32 bcnrc_val;
7528
7529 if (tx_rate != 0) {
7530 /* Calculate the rate factor values to set */
7531 rf_int = link_speed / tx_rate;
7532 rf_dec = (link_speed - (rf_int * tx_rate));
7533 rf_dec = (rf_dec * (1<<E1000_RTTBCNRC_RF_INT_SHIFT)) / tx_rate;
7534
7535 bcnrc_val = E1000_RTTBCNRC_RS_ENA;
7536 bcnrc_val |= ((rf_int<<E1000_RTTBCNRC_RF_INT_SHIFT) &
7537 E1000_RTTBCNRC_RF_INT_MASK);
7538 bcnrc_val |= (rf_dec & E1000_RTTBCNRC_RF_DEC_MASK);
7539 } else {
7540 bcnrc_val = 0;
7541 }
7542
7543 wr32(E1000_RTTDQSEL, vf); /* vf X uses queue X */
Lior Levyf00b0da2011-06-04 06:05:03 +00007544 /*
7545 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
7546 * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported.
7547 */
7548 wr32(E1000_RTTBCNRM, 0x14);
Lior Levy17dc5662011-02-08 02:28:46 +00007549 wr32(E1000_RTTBCNRC, bcnrc_val);
7550}
7551
7552static void igb_check_vf_rate_limit(struct igb_adapter *adapter)
7553{
7554 int actual_link_speed, i;
7555 bool reset_rate = false;
7556
7557 /* VF TX rate limit was not set or not supported */
7558 if ((adapter->vf_rate_link_speed == 0) ||
7559 (adapter->hw.mac.type != e1000_82576))
7560 return;
7561
7562 actual_link_speed = igb_link_mbps(adapter->link_speed);
7563 if (actual_link_speed != adapter->vf_rate_link_speed) {
7564 reset_rate = true;
7565 adapter->vf_rate_link_speed = 0;
7566 dev_info(&adapter->pdev->dev,
7567 "Link speed has been changed. VF Transmit "
7568 "rate is disabled\n");
7569 }
7570
7571 for (i = 0; i < adapter->vfs_allocated_count; i++) {
7572 if (reset_rate)
7573 adapter->vf_data[i].tx_rate = 0;
7574
7575 igb_set_vf_rate_limit(&adapter->hw, i,
7576 adapter->vf_data[i].tx_rate,
7577 actual_link_speed);
7578 }
7579}
7580
Williams, Mitch A8151d292010-02-10 01:44:24 +00007581static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
7582{
Lior Levy17dc5662011-02-08 02:28:46 +00007583 struct igb_adapter *adapter = netdev_priv(netdev);
7584 struct e1000_hw *hw = &adapter->hw;
7585 int actual_link_speed;
7586
7587 if (hw->mac.type != e1000_82576)
7588 return -EOPNOTSUPP;
7589
7590 actual_link_speed = igb_link_mbps(adapter->link_speed);
7591 if ((vf >= adapter->vfs_allocated_count) ||
7592 (!(rd32(E1000_STATUS) & E1000_STATUS_LU)) ||
7593 (tx_rate < 0) || (tx_rate > actual_link_speed))
7594 return -EINVAL;
7595
7596 adapter->vf_rate_link_speed = actual_link_speed;
7597 adapter->vf_data[vf].tx_rate = (u16)tx_rate;
7598 igb_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
7599
7600 return 0;
Williams, Mitch A8151d292010-02-10 01:44:24 +00007601}
7602
7603static int igb_ndo_get_vf_config(struct net_device *netdev,
7604 int vf, struct ifla_vf_info *ivi)
7605{
7606 struct igb_adapter *adapter = netdev_priv(netdev);
7607 if (vf >= adapter->vfs_allocated_count)
7608 return -EINVAL;
7609 ivi->vf = vf;
7610 memcpy(&ivi->mac, adapter->vf_data[vf].vf_mac_addresses, ETH_ALEN);
Lior Levy17dc5662011-02-08 02:28:46 +00007611 ivi->tx_rate = adapter->vf_data[vf].tx_rate;
Williams, Mitch A8151d292010-02-10 01:44:24 +00007612 ivi->vlan = adapter->vf_data[vf].pf_vlan;
7613 ivi->qos = adapter->vf_data[vf].pf_qos;
7614 return 0;
7615}
7616
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007617static void igb_vmm_control(struct igb_adapter *adapter)
7618{
7619 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck10d8e902009-10-27 15:54:04 +00007620 u32 reg;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007621
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007622 switch (hw->mac.type) {
7623 case e1000_82575:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00007624 case e1000_i210:
7625 case e1000_i211:
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007626 default:
7627 /* replication is not supported for 82575 */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007628 return;
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007629 case e1000_82576:
7630 /* notify HW that the MAC is adding vlan tags */
7631 reg = rd32(E1000_DTXCTL);
7632 reg |= E1000_DTXCTL_VLAN_ADDED;
7633 wr32(E1000_DTXCTL, reg);
7634 case e1000_82580:
7635 /* enable replication vlan tag stripping */
7636 reg = rd32(E1000_RPLOLR);
7637 reg |= E1000_RPLOLR_STRVLAN;
7638 wr32(E1000_RPLOLR, reg);
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +00007639 case e1000_i350:
7640 /* none of the above registers are supported by i350 */
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007641 break;
7642 }
Alexander Duyck10d8e902009-10-27 15:54:04 +00007643
Alexander Duyckd4960302009-10-27 15:53:45 +00007644 if (adapter->vfs_allocated_count) {
7645 igb_vmdq_set_loopback_pf(hw, true);
7646 igb_vmdq_set_replication_pf(hw, true);
Greg Rose13800462010-11-06 02:08:26 +00007647 igb_vmdq_set_anti_spoofing_pf(hw, true,
7648 adapter->vfs_allocated_count);
Alexander Duyckd4960302009-10-27 15:53:45 +00007649 } else {
7650 igb_vmdq_set_loopback_pf(hw, false);
7651 igb_vmdq_set_replication_pf(hw, false);
7652 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007653}
7654
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007655static void igb_init_dmac(struct igb_adapter *adapter, u32 pba)
7656{
7657 struct e1000_hw *hw = &adapter->hw;
7658 u32 dmac_thr;
7659 u16 hwm;
7660
7661 if (hw->mac.type > e1000_82580) {
7662 if (adapter->flags & IGB_FLAG_DMAC) {
7663 u32 reg;
7664
7665 /* force threshold to 0. */
7666 wr32(E1000_DMCTXTH, 0);
7667
7668 /*
Matthew Vicke8c626e2011-11-17 08:33:12 +00007669 * DMA Coalescing high water mark needs to be greater
7670 * than the Rx threshold. Set hwm to PBA - max frame
7671 * size in 16B units, capping it at PBA - 6KB.
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007672 */
Matthew Vicke8c626e2011-11-17 08:33:12 +00007673 hwm = 64 * pba - adapter->max_frame_size / 16;
7674 if (hwm < 64 * (pba - 6))
7675 hwm = 64 * (pba - 6);
7676 reg = rd32(E1000_FCRTC);
7677 reg &= ~E1000_FCRTC_RTH_COAL_MASK;
7678 reg |= ((hwm << E1000_FCRTC_RTH_COAL_SHIFT)
7679 & E1000_FCRTC_RTH_COAL_MASK);
7680 wr32(E1000_FCRTC, reg);
7681
7682 /*
7683 * Set the DMA Coalescing Rx threshold to PBA - 2 * max
7684 * frame size, capping it at PBA - 10KB.
7685 */
7686 dmac_thr = pba - adapter->max_frame_size / 512;
7687 if (dmac_thr < pba - 10)
7688 dmac_thr = pba - 10;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007689 reg = rd32(E1000_DMACR);
7690 reg &= ~E1000_DMACR_DMACTHR_MASK;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007691 reg |= ((dmac_thr << E1000_DMACR_DMACTHR_SHIFT)
7692 & E1000_DMACR_DMACTHR_MASK);
7693
7694 /* transition to L0x or L1 if available..*/
7695 reg |= (E1000_DMACR_DMAC_EN | E1000_DMACR_DMAC_LX_MASK);
7696
7697 /* watchdog timer= +-1000 usec in 32usec intervals */
7698 reg |= (1000 >> 5);
Matthew Vick0c02dd92012-04-14 05:20:32 +00007699
7700 /* Disable BMC-to-OS Watchdog Enable */
7701 reg &= ~E1000_DMACR_DC_BMC2OSW_EN;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007702 wr32(E1000_DMACR, reg);
7703
7704 /*
7705 * no lower threshold to disable
7706 * coalescing(smart fifb)-UTRESH=0
7707 */
7708 wr32(E1000_DMCRTRH, 0);
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007709
7710 reg = (IGB_DMCTLX_DCFLUSH_DIS | 0x4);
7711
7712 wr32(E1000_DMCTLX, reg);
7713
7714 /*
7715 * free space in tx packet buffer to wake from
7716 * DMA coal
7717 */
7718 wr32(E1000_DMCTXTH, (IGB_MIN_TXPBSIZE -
7719 (IGB_TX_BUF_4096 + adapter->max_frame_size)) >> 6);
7720
7721 /*
7722 * make low power state decision controlled
7723 * by DMA coal
7724 */
7725 reg = rd32(E1000_PCIEMISC);
7726 reg &= ~E1000_PCIEMISC_LX_DECISION;
7727 wr32(E1000_PCIEMISC, reg);
7728 } /* endif adapter->dmac is not disabled */
7729 } else if (hw->mac.type == e1000_82580) {
7730 u32 reg = rd32(E1000_PCIEMISC);
7731 wr32(E1000_PCIEMISC, reg & ~E1000_PCIEMISC_LX_DECISION);
7732 wr32(E1000_DMACR, 0);
7733 }
7734}
7735
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00007736/* igb_read_i2c_byte - Reads 8 bit word over I2C
7737 * @hw: pointer to hardware structure
7738 * @byte_offset: byte offset to read
7739 * @dev_addr: device address
7740 * @data: value read
7741 *
7742 * Performs byte read operation over I2C interface at
7743 * a specified device address.
7744 */
7745s32 igb_read_i2c_byte(struct e1000_hw *hw, u8 byte_offset,
7746 u8 dev_addr, u8 *data)
7747{
7748 struct igb_adapter *adapter = container_of(hw, struct igb_adapter, hw);
Carolyn Wyborny603e86f2013-02-20 07:40:55 +00007749 struct i2c_client *this_client = adapter->i2c_client;
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00007750 s32 status;
7751 u16 swfw_mask = 0;
7752
7753 if (!this_client)
7754 return E1000_ERR_I2C;
7755
7756 swfw_mask = E1000_SWFW_PHY0_SM;
7757
7758 if (hw->mac.ops.acquire_swfw_sync(hw, swfw_mask)
7759 != E1000_SUCCESS)
7760 return E1000_ERR_SWFW_SYNC;
7761
7762 status = i2c_smbus_read_byte_data(this_client, byte_offset);
7763 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
7764
7765 if (status < 0)
7766 return E1000_ERR_I2C;
7767 else {
7768 *data = status;
7769 return E1000_SUCCESS;
7770 }
7771}
7772
7773/* igb_write_i2c_byte - Writes 8 bit word over I2C
7774 * @hw: pointer to hardware structure
7775 * @byte_offset: byte offset to write
7776 * @dev_addr: device address
7777 * @data: value to write
7778 *
7779 * Performs byte write operation over I2C interface at
7780 * a specified device address.
7781 */
7782s32 igb_write_i2c_byte(struct e1000_hw *hw, u8 byte_offset,
7783 u8 dev_addr, u8 data)
7784{
7785 struct igb_adapter *adapter = container_of(hw, struct igb_adapter, hw);
Carolyn Wyborny603e86f2013-02-20 07:40:55 +00007786 struct i2c_client *this_client = adapter->i2c_client;
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00007787 s32 status;
7788 u16 swfw_mask = E1000_SWFW_PHY0_SM;
7789
7790 if (!this_client)
7791 return E1000_ERR_I2C;
7792
7793 if (hw->mac.ops.acquire_swfw_sync(hw, swfw_mask) != E1000_SUCCESS)
7794 return E1000_ERR_SWFW_SYNC;
7795 status = i2c_smbus_write_byte_data(this_client, byte_offset, data);
7796 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
7797
7798 if (status)
7799 return E1000_ERR_I2C;
7800 else
7801 return E1000_SUCCESS;
7802
7803}
Auke Kok9d5c8242008-01-24 02:22:38 -08007804/* igb_main.c */