]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/ixgbe/ixgbe_main.c
ixgbe: change vector numbering so that queues end up on correct CPUs
[net-next-2.6.git] / drivers / net / ixgbe / ixgbe_main.c
CommitLineData
9a799d71
AK
1/*******************************************************************************
2
3 Intel 10 Gigabit PCI Express Linux driver
8c47eaa7 4 Copyright(c) 1999 - 2010 Intel Corporation.
9a799d71
AK
5
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:
9a799d71
AK
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
28#include <linux/types.h>
29#include <linux/module.h>
30#include <linux/pci.h>
31#include <linux/netdevice.h>
32#include <linux/vmalloc.h>
33#include <linux/string.h>
34#include <linux/in.h>
35#include <linux/ip.h>
36#include <linux/tcp.h>
60127865 37#include <linux/pkt_sched.h>
9a799d71 38#include <linux/ipv6.h>
5a0e3ad6 39#include <linux/slab.h>
9a799d71
AK
40#include <net/checksum.h>
41#include <net/ip6_checksum.h>
42#include <linux/ethtool.h>
43#include <linux/if_vlan.h>
eacd73f7 44#include <scsi/fc/fc_fcoe.h>
9a799d71
AK
45
46#include "ixgbe.h"
47#include "ixgbe_common.h"
ee5f784a 48#include "ixgbe_dcb_82599.h"
1cdd1ec8 49#include "ixgbe_sriov.h"
9a799d71
AK
50
51char ixgbe_driver_name[] = "ixgbe";
9c8eb720 52static const char ixgbe_driver_string[] =
e8e9f696 53 "Intel(R) 10 Gigabit PCI Express Network Driver";
9a799d71 54
99faf68e 55#define DRV_VERSION "2.0.84-k2"
9c8eb720 56const char ixgbe_driver_version[] = DRV_VERSION;
8c47eaa7 57static char ixgbe_copyright[] = "Copyright (c) 1999-2010 Intel Corporation.";
9a799d71
AK
58
59static const struct ixgbe_info *ixgbe_info_tbl[] = {
b4617240 60 [board_82598] = &ixgbe_82598_info,
e8e26350 61 [board_82599] = &ixgbe_82599_info,
9a799d71
AK
62};
63
64/* ixgbe_pci_tbl - PCI Device ID Table
65 *
66 * Wildcard entries (PCI_ANY_ID) should come last
67 * Last entry must be all 0s
68 *
69 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
70 * Class, Class Mask, private data (not used) }
71 */
a3aa1884 72static DEFINE_PCI_DEVICE_TABLE(ixgbe_pci_tbl) = {
1e336d0f
DS
73 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598),
74 board_82598 },
9a799d71 75 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_DUAL_PORT),
3957d63d 76 board_82598 },
9a799d71 77 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_SINGLE_PORT),
3957d63d 78 board_82598 },
0befdb3e
JB
79 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AT),
80 board_82598 },
3845bec0
PWJ
81 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AT2),
82 board_82598 },
9a799d71 83 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_CX4),
3957d63d 84 board_82598 },
8d792cd9
JB
85 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_CX4_DUAL_PORT),
86 board_82598 },
c4900be0
DS
87 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_DA_DUAL_PORT),
88 board_82598 },
89 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM),
90 board_82598 },
b95f5fcb
JB
91 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_XF_LR),
92 board_82598 },
c4900be0
DS
93 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_SFP_LOM),
94 board_82598 },
2f21bdd3
DS
95 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_BX),
96 board_82598 },
e8e26350
PW
97 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_KX4),
98 board_82599 },
1fcf03e6
PWJ
99 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_XAUI_LOM),
100 board_82599 },
74757d49
DS
101 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_KR),
102 board_82599 },
e8e26350
PW
103 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP),
104 board_82599 },
38ad1c8e
DS
105 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_SFP_EM),
106 board_82599 },
dbfec662
DS
107 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_KX4_MEZZ),
108 board_82599 },
8911184f
PWJ
109 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_CX4),
110 board_82599 },
119fc60a
MC
111 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_T3_LOM),
112 board_82599 },
312eb931
DS
113 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_COMBO_BACKPLANE),
114 board_82599 },
9a799d71
AK
115
116 /* required last entry */
117 {0, }
118};
119MODULE_DEVICE_TABLE(pci, ixgbe_pci_tbl);
120
5dd2d332 121#ifdef CONFIG_IXGBE_DCA
bd0362dd 122static int ixgbe_notify_dca(struct notifier_block *, unsigned long event,
e8e9f696 123 void *p);
bd0362dd
JC
124static struct notifier_block dca_notifier = {
125 .notifier_call = ixgbe_notify_dca,
126 .next = NULL,
127 .priority = 0
128};
129#endif
130
1cdd1ec8
GR
131#ifdef CONFIG_PCI_IOV
132static unsigned int max_vfs;
133module_param(max_vfs, uint, 0);
e8e9f696
JP
134MODULE_PARM_DESC(max_vfs,
135 "Maximum number of virtual functions to allocate per physical function");
1cdd1ec8
GR
136#endif /* CONFIG_PCI_IOV */
137
9a799d71
AK
138MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
139MODULE_DESCRIPTION("Intel(R) 10 Gigabit PCI Express Network Driver");
140MODULE_LICENSE("GPL");
141MODULE_VERSION(DRV_VERSION);
142
143#define DEFAULT_DEBUG_LEVEL_SHIFT 3
144
1cdd1ec8
GR
145static inline void ixgbe_disable_sriov(struct ixgbe_adapter *adapter)
146{
147 struct ixgbe_hw *hw = &adapter->hw;
148 u32 gcr;
149 u32 gpie;
150 u32 vmdctl;
151
152#ifdef CONFIG_PCI_IOV
153 /* disable iov and allow time for transactions to clear */
154 pci_disable_sriov(adapter->pdev);
155#endif
156
157 /* turn off device IOV mode */
158 gcr = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
159 gcr &= ~(IXGBE_GCR_EXT_SRIOV);
160 IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr);
161 gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
162 gpie &= ~IXGBE_GPIE_VTMODE_MASK;
163 IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
164
165 /* set default pool back to 0 */
166 vmdctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
167 vmdctl &= ~IXGBE_VT_CTL_POOL_MASK;
168 IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vmdctl);
169
170 /* take a breather then clean up driver data */
171 msleep(100);
e8e9f696
JP
172
173 kfree(adapter->vfinfo);
1cdd1ec8
GR
174 adapter->vfinfo = NULL;
175
176 adapter->num_vfs = 0;
177 adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
178}
179
dcd79aeb
TI
180struct ixgbe_reg_info {
181 u32 ofs;
182 char *name;
183};
184
185static const struct ixgbe_reg_info ixgbe_reg_info_tbl[] = {
186
187 /* General Registers */
188 {IXGBE_CTRL, "CTRL"},
189 {IXGBE_STATUS, "STATUS"},
190 {IXGBE_CTRL_EXT, "CTRL_EXT"},
191
192 /* Interrupt Registers */
193 {IXGBE_EICR, "EICR"},
194
195 /* RX Registers */
196 {IXGBE_SRRCTL(0), "SRRCTL"},
197 {IXGBE_DCA_RXCTRL(0), "DRXCTL"},
198 {IXGBE_RDLEN(0), "RDLEN"},
199 {IXGBE_RDH(0), "RDH"},
200 {IXGBE_RDT(0), "RDT"},
201 {IXGBE_RXDCTL(0), "RXDCTL"},
202 {IXGBE_RDBAL(0), "RDBAL"},
203 {IXGBE_RDBAH(0), "RDBAH"},
204
205 /* TX Registers */
206 {IXGBE_TDBAL(0), "TDBAL"},
207 {IXGBE_TDBAH(0), "TDBAH"},
208 {IXGBE_TDLEN(0), "TDLEN"},
209 {IXGBE_TDH(0), "TDH"},
210 {IXGBE_TDT(0), "TDT"},
211 {IXGBE_TXDCTL(0), "TXDCTL"},
212
213 /* List Terminator */
214 {}
215};
216
217
218/*
219 * ixgbe_regdump - register printout routine
220 */
221static void ixgbe_regdump(struct ixgbe_hw *hw, struct ixgbe_reg_info *reginfo)
222{
223 int i = 0, j = 0;
224 char rname[16];
225 u32 regs[64];
226
227 switch (reginfo->ofs) {
228 case IXGBE_SRRCTL(0):
229 for (i = 0; i < 64; i++)
230 regs[i] = IXGBE_READ_REG(hw, IXGBE_SRRCTL(i));
231 break;
232 case IXGBE_DCA_RXCTRL(0):
233 for (i = 0; i < 64; i++)
234 regs[i] = IXGBE_READ_REG(hw, IXGBE_DCA_RXCTRL(i));
235 break;
236 case IXGBE_RDLEN(0):
237 for (i = 0; i < 64; i++)
238 regs[i] = IXGBE_READ_REG(hw, IXGBE_RDLEN(i));
239 break;
240 case IXGBE_RDH(0):
241 for (i = 0; i < 64; i++)
242 regs[i] = IXGBE_READ_REG(hw, IXGBE_RDH(i));
243 break;
244 case IXGBE_RDT(0):
245 for (i = 0; i < 64; i++)
246 regs[i] = IXGBE_READ_REG(hw, IXGBE_RDT(i));
247 break;
248 case IXGBE_RXDCTL(0):
249 for (i = 0; i < 64; i++)
250 regs[i] = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
251 break;
252 case IXGBE_RDBAL(0):
253 for (i = 0; i < 64; i++)
254 regs[i] = IXGBE_READ_REG(hw, IXGBE_RDBAL(i));
255 break;
256 case IXGBE_RDBAH(0):
257 for (i = 0; i < 64; i++)
258 regs[i] = IXGBE_READ_REG(hw, IXGBE_RDBAH(i));
259 break;
260 case IXGBE_TDBAL(0):
261 for (i = 0; i < 64; i++)
262 regs[i] = IXGBE_READ_REG(hw, IXGBE_TDBAL(i));
263 break;
264 case IXGBE_TDBAH(0):
265 for (i = 0; i < 64; i++)
266 regs[i] = IXGBE_READ_REG(hw, IXGBE_TDBAH(i));
267 break;
268 case IXGBE_TDLEN(0):
269 for (i = 0; i < 64; i++)
270 regs[i] = IXGBE_READ_REG(hw, IXGBE_TDLEN(i));
271 break;
272 case IXGBE_TDH(0):
273 for (i = 0; i < 64; i++)
274 regs[i] = IXGBE_READ_REG(hw, IXGBE_TDH(i));
275 break;
276 case IXGBE_TDT(0):
277 for (i = 0; i < 64; i++)
278 regs[i] = IXGBE_READ_REG(hw, IXGBE_TDT(i));
279 break;
280 case IXGBE_TXDCTL(0):
281 for (i = 0; i < 64; i++)
282 regs[i] = IXGBE_READ_REG(hw, IXGBE_TXDCTL(i));
283 break;
284 default:
c7689578 285 pr_info("%-15s %08x\n", reginfo->name,
dcd79aeb
TI
286 IXGBE_READ_REG(hw, reginfo->ofs));
287 return;
288 }
289
290 for (i = 0; i < 8; i++) {
291 snprintf(rname, 16, "%s[%d-%d]", reginfo->name, i*8, i*8+7);
c7689578 292 pr_err("%-15s", rname);
dcd79aeb 293 for (j = 0; j < 8; j++)
c7689578
JP
294 pr_cont(" %08x", regs[i*8+j]);
295 pr_cont("\n");
dcd79aeb
TI
296 }
297
298}
299
300/*
301 * ixgbe_dump - Print registers, tx-rings and rx-rings
302 */
303static void ixgbe_dump(struct ixgbe_adapter *adapter)
304{
305 struct net_device *netdev = adapter->netdev;
306 struct ixgbe_hw *hw = &adapter->hw;
307 struct ixgbe_reg_info *reginfo;
308 int n = 0;
309 struct ixgbe_ring *tx_ring;
310 struct ixgbe_tx_buffer *tx_buffer_info;
311 union ixgbe_adv_tx_desc *tx_desc;
312 struct my_u0 { u64 a; u64 b; } *u0;
313 struct ixgbe_ring *rx_ring;
314 union ixgbe_adv_rx_desc *rx_desc;
315 struct ixgbe_rx_buffer *rx_buffer_info;
316 u32 staterr;
317 int i = 0;
318
319 if (!netif_msg_hw(adapter))
320 return;
321
322 /* Print netdevice Info */
323 if (netdev) {
324 dev_info(&adapter->pdev->dev, "Net device Info\n");
c7689578 325 pr_info("Device Name state "
dcd79aeb 326 "trans_start last_rx\n");
c7689578
JP
327 pr_info("%-15s %016lX %016lX %016lX\n",
328 netdev->name,
329 netdev->state,
330 netdev->trans_start,
331 netdev->last_rx);
dcd79aeb
TI
332 }
333
334 /* Print Registers */
335 dev_info(&adapter->pdev->dev, "Register Dump\n");
c7689578 336 pr_info(" Register Name Value\n");
dcd79aeb
TI
337 for (reginfo = (struct ixgbe_reg_info *)ixgbe_reg_info_tbl;
338 reginfo->name; reginfo++) {
339 ixgbe_regdump(hw, reginfo);
340 }
341
342 /* Print TX Ring Summary */
343 if (!netdev || !netif_running(netdev))
344 goto exit;
345
346 dev_info(&adapter->pdev->dev, "TX Rings Summary\n");
c7689578 347 pr_info("Queue [NTU] [NTC] [bi(ntc)->dma ] leng ntw timestamp\n");
dcd79aeb
TI
348 for (n = 0; n < adapter->num_tx_queues; n++) {
349 tx_ring = adapter->tx_ring[n];
350 tx_buffer_info =
351 &tx_ring->tx_buffer_info[tx_ring->next_to_clean];
c7689578 352 pr_info(" %5d %5X %5X %016llX %04X %3X %016llX\n",
dcd79aeb
TI
353 n, tx_ring->next_to_use, tx_ring->next_to_clean,
354 (u64)tx_buffer_info->dma,
355 tx_buffer_info->length,
356 tx_buffer_info->next_to_watch,
357 (u64)tx_buffer_info->time_stamp);
358 }
359
360 /* Print TX Rings */
361 if (!netif_msg_tx_done(adapter))
362 goto rx_ring_summary;
363
364 dev_info(&adapter->pdev->dev, "TX Rings Dump\n");
365
366 /* Transmit Descriptor Formats
367 *
368 * Advanced Transmit Descriptor
369 * +--------------------------------------------------------------+
370 * 0 | Buffer Address [63:0] |
371 * +--------------------------------------------------------------+
372 * 8 | PAYLEN | PORTS | IDX | STA | DCMD |DTYP | RSV | DTALEN |
373 * +--------------------------------------------------------------+
374 * 63 46 45 40 39 36 35 32 31 24 23 20 19 0
375 */
376
377 for (n = 0; n < adapter->num_tx_queues; n++) {
378 tx_ring = adapter->tx_ring[n];
c7689578
JP
379 pr_info("------------------------------------\n");
380 pr_info("TX QUEUE INDEX = %d\n", tx_ring->queue_index);
381 pr_info("------------------------------------\n");
382 pr_info("T [desc] [address 63:0 ] "
dcd79aeb
TI
383 "[PlPOIdStDDt Ln] [bi->dma ] "
384 "leng ntw timestamp bi->skb\n");
385
386 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
31f05a2d 387 tx_desc = IXGBE_TX_DESC_ADV(tx_ring, i);
dcd79aeb
TI
388 tx_buffer_info = &tx_ring->tx_buffer_info[i];
389 u0 = (struct my_u0 *)tx_desc;
c7689578 390 pr_info("T [0x%03X] %016llX %016llX %016llX"
dcd79aeb
TI
391 " %04X %3X %016llX %p", i,
392 le64_to_cpu(u0->a),
393 le64_to_cpu(u0->b),
394 (u64)tx_buffer_info->dma,
395 tx_buffer_info->length,
396 tx_buffer_info->next_to_watch,
397 (u64)tx_buffer_info->time_stamp,
398 tx_buffer_info->skb);
399 if (i == tx_ring->next_to_use &&
400 i == tx_ring->next_to_clean)
c7689578 401 pr_cont(" NTC/U\n");
dcd79aeb 402 else if (i == tx_ring->next_to_use)
c7689578 403 pr_cont(" NTU\n");
dcd79aeb 404 else if (i == tx_ring->next_to_clean)
c7689578 405 pr_cont(" NTC\n");
dcd79aeb 406 else
c7689578 407 pr_cont("\n");
dcd79aeb
TI
408
409 if (netif_msg_pktdata(adapter) &&
410 tx_buffer_info->dma != 0)
411 print_hex_dump(KERN_INFO, "",
412 DUMP_PREFIX_ADDRESS, 16, 1,
413 phys_to_virt(tx_buffer_info->dma),
414 tx_buffer_info->length, true);
415 }
416 }
417
418 /* Print RX Rings Summary */
419rx_ring_summary:
420 dev_info(&adapter->pdev->dev, "RX Rings Summary\n");
c7689578 421 pr_info("Queue [NTU] [NTC]\n");
dcd79aeb
TI
422 for (n = 0; n < adapter->num_rx_queues; n++) {
423 rx_ring = adapter->rx_ring[n];
c7689578
JP
424 pr_info("%5d %5X %5X\n",
425 n, rx_ring->next_to_use, rx_ring->next_to_clean);
dcd79aeb
TI
426 }
427
428 /* Print RX Rings */
429 if (!netif_msg_rx_status(adapter))
430 goto exit;
431
432 dev_info(&adapter->pdev->dev, "RX Rings Dump\n");
433
434 /* Advanced Receive Descriptor (Read) Format
435 * 63 1 0
436 * +-----------------------------------------------------+
437 * 0 | Packet Buffer Address [63:1] |A0/NSE|
438 * +----------------------------------------------+------+
439 * 8 | Header Buffer Address [63:1] | DD |
440 * +-----------------------------------------------------+
441 *
442 *
443 * Advanced Receive Descriptor (Write-Back) Format
444 *
445 * 63 48 47 32 31 30 21 20 16 15 4 3 0
446 * +------------------------------------------------------+
447 * 0 | Packet IP |SPH| HDR_LEN | RSV|Packet| RSS |
448 * | Checksum Ident | | | | Type | Type |
449 * +------------------------------------------------------+
450 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
451 * +------------------------------------------------------+
452 * 63 48 47 32 31 20 19 0
453 */
454 for (n = 0; n < adapter->num_rx_queues; n++) {
455 rx_ring = adapter->rx_ring[n];
c7689578
JP
456 pr_info("------------------------------------\n");
457 pr_info("RX QUEUE INDEX = %d\n", rx_ring->queue_index);
458 pr_info("------------------------------------\n");
459 pr_info("R [desc] [ PktBuf A0] "
dcd79aeb
TI
460 "[ HeadBuf DD] [bi->dma ] [bi->skb] "
461 "<-- Adv Rx Read format\n");
c7689578 462 pr_info("RWB[desc] [PcsmIpSHl PtRs] "
dcd79aeb
TI
463 "[vl er S cks ln] ---------------- [bi->skb] "
464 "<-- Adv Rx Write-Back format\n");
465
466 for (i = 0; i < rx_ring->count; i++) {
467 rx_buffer_info = &rx_ring->rx_buffer_info[i];
31f05a2d 468 rx_desc = IXGBE_RX_DESC_ADV(rx_ring, i);
dcd79aeb
TI
469 u0 = (struct my_u0 *)rx_desc;
470 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
471 if (staterr & IXGBE_RXD_STAT_DD) {
472 /* Descriptor Done */
c7689578 473 pr_info("RWB[0x%03X] %016llX "
dcd79aeb
TI
474 "%016llX ---------------- %p", i,
475 le64_to_cpu(u0->a),
476 le64_to_cpu(u0->b),
477 rx_buffer_info->skb);
478 } else {
c7689578 479 pr_info("R [0x%03X] %016llX "
dcd79aeb
TI
480 "%016llX %016llX %p", i,
481 le64_to_cpu(u0->a),
482 le64_to_cpu(u0->b),
483 (u64)rx_buffer_info->dma,
484 rx_buffer_info->skb);
485
486 if (netif_msg_pktdata(adapter)) {
487 print_hex_dump(KERN_INFO, "",
488 DUMP_PREFIX_ADDRESS, 16, 1,
489 phys_to_virt(rx_buffer_info->dma),
490 rx_ring->rx_buf_len, true);
491
492 if (rx_ring->rx_buf_len
493 < IXGBE_RXBUFFER_2048)
494 print_hex_dump(KERN_INFO, "",
495 DUMP_PREFIX_ADDRESS, 16, 1,
496 phys_to_virt(
497 rx_buffer_info->page_dma +
498 rx_buffer_info->page_offset
499 ),
500 PAGE_SIZE/2, true);
501 }
502 }
503
504 if (i == rx_ring->next_to_use)
c7689578 505 pr_cont(" NTU\n");
dcd79aeb 506 else if (i == rx_ring->next_to_clean)
c7689578 507 pr_cont(" NTC\n");
dcd79aeb 508 else
c7689578 509 pr_cont("\n");
dcd79aeb
TI
510
511 }
512 }
513
514exit:
515 return;
516}
517
5eba3699
AV
518static void ixgbe_release_hw_control(struct ixgbe_adapter *adapter)
519{
520 u32 ctrl_ext;
521
522 /* Let firmware take over control of h/w */
523 ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
524 IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
e8e9f696 525 ctrl_ext & ~IXGBE_CTRL_EXT_DRV_LOAD);
5eba3699
AV
526}
527
528static void ixgbe_get_hw_control(struct ixgbe_adapter *adapter)
529{
530 u32 ctrl_ext;
531
532 /* Let firmware know the driver has taken over */
533 ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
534 IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
e8e9f696 535 ctrl_ext | IXGBE_CTRL_EXT_DRV_LOAD);
5eba3699 536}
9a799d71 537
e8e26350
PW
538/*
539 * ixgbe_set_ivar - set the IVAR registers, mapping interrupt causes to vectors
540 * @adapter: pointer to adapter struct
541 * @direction: 0 for Rx, 1 for Tx, -1 for other causes
542 * @queue: queue to map the corresponding interrupt to
543 * @msix_vector: the vector to map to the corresponding queue
544 *
545 */
546static void ixgbe_set_ivar(struct ixgbe_adapter *adapter, s8 direction,
e8e9f696 547 u8 queue, u8 msix_vector)
9a799d71
AK
548{
549 u32 ivar, index;
e8e26350
PW
550 struct ixgbe_hw *hw = &adapter->hw;
551 switch (hw->mac.type) {
552 case ixgbe_mac_82598EB:
553 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
554 if (direction == -1)
555 direction = 0;
556 index = (((direction * 64) + queue) >> 2) & 0x1F;
557 ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(index));
558 ivar &= ~(0xFF << (8 * (queue & 0x3)));
559 ivar |= (msix_vector << (8 * (queue & 0x3)));
560 IXGBE_WRITE_REG(hw, IXGBE_IVAR(index), ivar);
561 break;
562 case ixgbe_mac_82599EB:
563 if (direction == -1) {
564 /* other causes */
565 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
566 index = ((queue & 1) * 8);
567 ivar = IXGBE_READ_REG(&adapter->hw, IXGBE_IVAR_MISC);
568 ivar &= ~(0xFF << index);
569 ivar |= (msix_vector << index);
570 IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR_MISC, ivar);
571 break;
572 } else {
573 /* tx or rx causes */
574 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
575 index = ((16 * (queue & 1)) + (8 * direction));
576 ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(queue >> 1));
577 ivar &= ~(0xFF << index);
578 ivar |= (msix_vector << index);
579 IXGBE_WRITE_REG(hw, IXGBE_IVAR(queue >> 1), ivar);
580 break;
581 }
582 default:
583 break;
584 }
9a799d71
AK
585}
586
fe49f04a 587static inline void ixgbe_irq_rearm_queues(struct ixgbe_adapter *adapter,
e8e9f696 588 u64 qmask)
fe49f04a
AD
589{
590 u32 mask;
591
592 if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
593 mask = (IXGBE_EIMS_RTX_QUEUE & qmask);
594 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS, mask);
595 } else {
596 mask = (qmask & 0xFFFFFFFF);
597 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS_EX(0), mask);
598 mask = (qmask >> 32);
599 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS_EX(1), mask);
600 }
601}
602
b6ec895e
AD
603void ixgbe_unmap_and_free_tx_resource(struct ixgbe_ring *tx_ring,
604 struct ixgbe_tx_buffer *tx_buffer_info)
9a799d71 605{
e5a43549
AD
606 if (tx_buffer_info->dma) {
607 if (tx_buffer_info->mapped_as_page)
b6ec895e 608 dma_unmap_page(tx_ring->dev,
e5a43549
AD
609 tx_buffer_info->dma,
610 tx_buffer_info->length,
1b507730 611 DMA_TO_DEVICE);
e5a43549 612 else
b6ec895e 613 dma_unmap_single(tx_ring->dev,
e5a43549
AD
614 tx_buffer_info->dma,
615 tx_buffer_info->length,
1b507730 616 DMA_TO_DEVICE);
e5a43549
AD
617 tx_buffer_info->dma = 0;
618 }
9a799d71
AK
619 if (tx_buffer_info->skb) {
620 dev_kfree_skb_any(tx_buffer_info->skb);
621 tx_buffer_info->skb = NULL;
622 }
44df32c5 623 tx_buffer_info->time_stamp = 0;
9a799d71
AK
624 /* tx_buffer_info must be completely set up in the transmit path */
625}
626
26f23d82 627/**
7483d9dd 628 * ixgbe_tx_xon_state - check the tx ring xon state
26f23d82
YZ
629 * @adapter: the ixgbe adapter
630 * @tx_ring: the corresponding tx_ring
631 *
632 * If not in DCB mode, checks TFCS.TXOFF, otherwise, find out the
633 * corresponding TC of this tx_ring when checking TFCS.
634 *
7483d9dd 635 * Returns : true if in xon state (currently not paused)
26f23d82 636 */
7483d9dd 637static inline bool ixgbe_tx_xon_state(struct ixgbe_adapter *adapter,
e8e9f696 638 struct ixgbe_ring *tx_ring)
26f23d82 639{
26f23d82
YZ
640 u32 txoff = IXGBE_TFCS_TXOFF;
641
642#ifdef CONFIG_IXGBE_DCB
ca739481 643 if (adapter->dcb_cfg.pfc_mode_enable) {
30b76832 644 int tc;
26f23d82
YZ
645 int reg_idx = tx_ring->reg_idx;
646 int dcb_i = adapter->ring_feature[RING_F_DCB].indices;
647
6837e895
PW
648 switch (adapter->hw.mac.type) {
649 case ixgbe_mac_82598EB:
26f23d82
YZ
650 tc = reg_idx >> 2;
651 txoff = IXGBE_TFCS_TXOFF0;
6837e895
PW
652 break;
653 case ixgbe_mac_82599EB:
26f23d82
YZ
654 tc = 0;
655 txoff = IXGBE_TFCS_TXOFF;
656 if (dcb_i == 8) {
657 /* TC0, TC1 */
658 tc = reg_idx >> 5;
659 if (tc == 2) /* TC2, TC3 */
660 tc += (reg_idx - 64) >> 4;
661 else if (tc == 3) /* TC4, TC5, TC6, TC7 */
662 tc += 1 + ((reg_idx - 96) >> 3);
663 } else if (dcb_i == 4) {
664 /* TC0, TC1 */
665 tc = reg_idx >> 6;
666 if (tc == 1) {
667 tc += (reg_idx - 64) >> 5;
668 if (tc == 2) /* TC2, TC3 */
669 tc += (reg_idx - 96) >> 4;
670 }
671 }
6837e895
PW
672 break;
673 default:
674 tc = 0;
26f23d82
YZ
675 }
676 txoff <<= tc;
677 }
678#endif
679 return IXGBE_READ_REG(&adapter->hw, IXGBE_TFCS) & txoff;
680}
681
9a799d71 682static inline bool ixgbe_check_tx_hang(struct ixgbe_adapter *adapter,
e8e9f696
JP
683 struct ixgbe_ring *tx_ring,
684 unsigned int eop)
9a799d71 685{
e01c31a5 686 struct ixgbe_hw *hw = &adapter->hw;
e01c31a5 687
9a799d71 688 /* Detect a transmit hang in hardware, this serializes the
e01c31a5 689 * check with the clearing of time_stamp and movement of eop */
7d637bcc 690 clear_check_for_tx_hang(tx_ring);
44df32c5 691 if (tx_ring->tx_buffer_info[eop].time_stamp &&
9a799d71 692 time_after(jiffies, tx_ring->tx_buffer_info[eop].time_stamp + HZ) &&
7483d9dd 693 ixgbe_tx_xon_state(adapter, tx_ring)) {
9a799d71 694 /* detected Tx unit hang */
e01c31a5 695 union ixgbe_adv_tx_desc *tx_desc;
31f05a2d 696 tx_desc = IXGBE_TX_DESC_ADV(tx_ring, eop);
396e799c 697 e_err(drv, "Detected Tx Unit Hang\n"
849c4542
ET
698 " Tx Queue <%d>\n"
699 " TDH, TDT <%x>, <%x>\n"
700 " next_to_use <%x>\n"
701 " next_to_clean <%x>\n"
702 "tx_buffer_info[next_to_clean]\n"
703 " time_stamp <%lx>\n"
704 " jiffies <%lx>\n",
705 tx_ring->queue_index,
84ea2591
AD
706 IXGBE_READ_REG(hw, IXGBE_TDH(tx_ring->reg_idx)),
707 IXGBE_READ_REG(hw, IXGBE_TDT(tx_ring->reg_idx)),
849c4542
ET
708 tx_ring->next_to_use, eop,
709 tx_ring->tx_buffer_info[eop].time_stamp, jiffies);
9a799d71
AK
710 return true;
711 }
712
713 return false;
714}
715
b4617240
PW
716#define IXGBE_MAX_TXD_PWR 14
717#define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
e092be60
AV
718
719/* Tx Descriptors needed, worst case */
720#define TXD_USE_COUNT(S) (((S) >> IXGBE_MAX_TXD_PWR) + \
721 (((S) & (IXGBE_MAX_DATA_PER_TXD - 1)) ? 1 : 0))
722#define DESC_NEEDED (TXD_USE_COUNT(IXGBE_MAX_DATA_PER_TXD) /* skb->data */ + \
b4617240 723 MAX_SKB_FRAGS * TXD_USE_COUNT(PAGE_SIZE) + 1) /* for context */
e092be60 724
e01c31a5
JB
725static void ixgbe_tx_timeout(struct net_device *netdev);
726
9a799d71
AK
727/**
728 * ixgbe_clean_tx_irq - Reclaim resources after transmit completes
fe49f04a 729 * @q_vector: structure containing interrupt and ring information
e01c31a5 730 * @tx_ring: tx ring to clean
9a799d71 731 **/
fe49f04a 732static bool ixgbe_clean_tx_irq(struct ixgbe_q_vector *q_vector,
e8e9f696 733 struct ixgbe_ring *tx_ring)
9a799d71 734{
fe49f04a 735 struct ixgbe_adapter *adapter = q_vector->adapter;
12207e49
PWJ
736 union ixgbe_adv_tx_desc *tx_desc, *eop_desc;
737 struct ixgbe_tx_buffer *tx_buffer_info;
e01c31a5 738 unsigned int total_bytes = 0, total_packets = 0;
b953799e 739 u16 i, eop, count = 0;
9a799d71
AK
740
741 i = tx_ring->next_to_clean;
12207e49 742 eop = tx_ring->tx_buffer_info[i].next_to_watch;
31f05a2d 743 eop_desc = IXGBE_TX_DESC_ADV(tx_ring, eop);
12207e49
PWJ
744
745 while ((eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)) &&
9a1a69ad 746 (count < tx_ring->work_limit)) {
12207e49 747 bool cleaned = false;
2d0bb1c1 748 rmb(); /* read buffer_info after eop_desc */
12207e49 749 for ( ; !cleaned; count++) {
31f05a2d 750 tx_desc = IXGBE_TX_DESC_ADV(tx_ring, i);
9a799d71 751 tx_buffer_info = &tx_ring->tx_buffer_info[i];
8ad494b0
AD
752
753 tx_desc->wb.status = 0;
12207e49 754 cleaned = (i == eop);
9a799d71 755
8ad494b0
AD
756 i++;
757 if (i == tx_ring->count)
758 i = 0;
e01c31a5 759
8ad494b0
AD
760 if (cleaned && tx_buffer_info->skb) {
761 total_bytes += tx_buffer_info->bytecount;
762 total_packets += tx_buffer_info->gso_segs;
e092be60 763 }
e01c31a5 764
b6ec895e 765 ixgbe_unmap_and_free_tx_resource(tx_ring,
e8e9f696 766 tx_buffer_info);
e01c31a5 767 }
12207e49
PWJ
768
769 eop = tx_ring->tx_buffer_info[i].next_to_watch;
31f05a2d 770 eop_desc = IXGBE_TX_DESC_ADV(tx_ring, eop);
12207e49
PWJ
771 }
772
9a799d71 773 tx_ring->next_to_clean = i;
b953799e
AD
774 tx_ring->total_bytes += total_bytes;
775 tx_ring->total_packets += total_packets;
776 u64_stats_update_begin(&tx_ring->syncp);
777 tx_ring->stats.packets += total_packets;
778 tx_ring->stats.bytes += total_bytes;
779 u64_stats_update_end(&tx_ring->syncp);
780
781 if (check_for_tx_hang(tx_ring) &&
782 ixgbe_check_tx_hang(adapter, tx_ring, i)) {
783 /* schedule immediate reset if we believe we hung */
784 e_info(probe, "tx hang %d detected, resetting "
785 "adapter\n", adapter->tx_timeout_count + 1);
786 ixgbe_tx_timeout(adapter->netdev);
787
788 /* the adapter is about to reset, no point in enabling stuff */
789 return true;
790 }
9a799d71 791
e092be60 792#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
fc77dc3c 793 if (unlikely(count && netif_carrier_ok(tx_ring->netdev) &&
e8e9f696 794 (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
e092be60
AV
795 /* Make sure that anybody stopping the queue after this
796 * sees the new next_to_clean.
797 */
798 smp_mb();
fc77dc3c 799 if (__netif_subqueue_stopped(tx_ring->netdev, tx_ring->queue_index) &&
30eba97a 800 !test_bit(__IXGBE_DOWN, &adapter->state)) {
fc77dc3c 801 netif_wake_subqueue(tx_ring->netdev, tx_ring->queue_index);
5b7da515 802 ++tx_ring->tx_stats.restart_queue;
30eba97a 803 }
e092be60 804 }
9a799d71 805
807540ba 806 return count < tx_ring->work_limit;
9a799d71
AK
807}
808
5dd2d332 809#ifdef CONFIG_IXGBE_DCA
bd0362dd 810static void ixgbe_update_rx_dca(struct ixgbe_adapter *adapter,
33cf09c9
AD
811 struct ixgbe_ring *rx_ring,
812 int cpu)
bd0362dd 813{
33cf09c9 814 struct ixgbe_hw *hw = &adapter->hw;
bd0362dd 815 u32 rxctrl;
33cf09c9
AD
816 u8 reg_idx = rx_ring->reg_idx;
817
818 rxctrl = IXGBE_READ_REG(hw, IXGBE_DCA_RXCTRL(reg_idx));
819 switch (hw->mac.type) {
820 case ixgbe_mac_82598EB:
821 rxctrl &= ~IXGBE_DCA_RXCTRL_CPUID_MASK;
822 rxctrl |= dca3_get_tag(&adapter->pdev->dev, cpu);
823 break;
824 case ixgbe_mac_82599EB:
825 rxctrl &= ~IXGBE_DCA_RXCTRL_CPUID_MASK_82599;
826 rxctrl |= (dca3_get_tag(&adapter->pdev->dev, cpu) <<
827 IXGBE_DCA_RXCTRL_CPUID_SHIFT_82599);
828 break;
829 default:
830 break;
bd0362dd 831 }
33cf09c9
AD
832 rxctrl |= IXGBE_DCA_RXCTRL_DESC_DCA_EN;
833 rxctrl |= IXGBE_DCA_RXCTRL_HEAD_DCA_EN;
834 rxctrl &= ~(IXGBE_DCA_RXCTRL_DESC_RRO_EN);
835 rxctrl &= ~(IXGBE_DCA_RXCTRL_DESC_WRO_EN |
836 IXGBE_DCA_RXCTRL_DESC_HSRO_EN);
837 IXGBE_WRITE_REG(hw, IXGBE_DCA_RXCTRL(reg_idx), rxctrl);
bd0362dd
JC
838}
839
840static void ixgbe_update_tx_dca(struct ixgbe_adapter *adapter,
33cf09c9
AD
841 struct ixgbe_ring *tx_ring,
842 int cpu)
bd0362dd 843{
33cf09c9 844 struct ixgbe_hw *hw = &adapter->hw;
bd0362dd 845 u32 txctrl;
33cf09c9
AD
846 u8 reg_idx = tx_ring->reg_idx;
847
848 switch (hw->mac.type) {
849 case ixgbe_mac_82598EB:
850 txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(reg_idx));
851 txctrl &= ~IXGBE_DCA_TXCTRL_CPUID_MASK;
852 txctrl |= dca3_get_tag(&adapter->pdev->dev, cpu);
853 txctrl |= IXGBE_DCA_TXCTRL_DESC_DCA_EN;
854 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
855 IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(reg_idx), txctrl);
856 break;
857 case ixgbe_mac_82599EB:
858 txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(reg_idx));
859 txctrl &= ~IXGBE_DCA_TXCTRL_CPUID_MASK_82599;
860 txctrl |= (dca3_get_tag(&adapter->pdev->dev, cpu) <<
861 IXGBE_DCA_TXCTRL_CPUID_SHIFT_82599);
862 txctrl |= IXGBE_DCA_TXCTRL_DESC_DCA_EN;
863 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
864 IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(reg_idx), txctrl);
865 break;
866 default:
867 break;
868 }
869}
870
871static void ixgbe_update_dca(struct ixgbe_q_vector *q_vector)
872{
873 struct ixgbe_adapter *adapter = q_vector->adapter;
bd0362dd 874 int cpu = get_cpu();
33cf09c9
AD
875 long r_idx;
876 int i;
bd0362dd 877
33cf09c9
AD
878 if (q_vector->cpu == cpu)
879 goto out_no_update;
880
881 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
882 for (i = 0; i < q_vector->txr_count; i++) {
883 ixgbe_update_tx_dca(adapter, adapter->tx_ring[r_idx], cpu);
884 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
885 r_idx + 1);
bd0362dd 886 }
33cf09c9
AD
887
888 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
889 for (i = 0; i < q_vector->rxr_count; i++) {
890 ixgbe_update_rx_dca(adapter, adapter->rx_ring[r_idx], cpu);
891 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
892 r_idx + 1);
893 }
894
895 q_vector->cpu = cpu;
896out_no_update:
bd0362dd
JC
897 put_cpu();
898}
899
900static void ixgbe_setup_dca(struct ixgbe_adapter *adapter)
901{
33cf09c9 902 int num_q_vectors;
bd0362dd
JC
903 int i;
904
905 if (!(adapter->flags & IXGBE_FLAG_DCA_ENABLED))
906 return;
907
e35ec126
AD
908 /* always use CB2 mode, difference is masked in the CB driver */
909 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 2);
910
33cf09c9
AD
911 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
912 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
913 else
914 num_q_vectors = 1;
915
916 for (i = 0; i < num_q_vectors; i++) {
917 adapter->q_vector[i]->cpu = -1;
918 ixgbe_update_dca(adapter->q_vector[i]);
bd0362dd
JC
919 }
920}
921
922static int __ixgbe_notify_dca(struct device *dev, void *data)
923{
c60fbb00 924 struct ixgbe_adapter *adapter = dev_get_drvdata(dev);
bd0362dd
JC
925 unsigned long event = *(unsigned long *)data;
926
33cf09c9
AD
927 if (!(adapter->flags & IXGBE_FLAG_DCA_ENABLED))
928 return 0;
929
bd0362dd
JC
930 switch (event) {
931 case DCA_PROVIDER_ADD:
96b0e0f6
JB
932 /* if we're already enabled, don't do it again */
933 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
934 break;
652f093f 935 if (dca_add_requester(dev) == 0) {
96b0e0f6 936 adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
bd0362dd
JC
937 ixgbe_setup_dca(adapter);
938 break;
939 }
940 /* Fall Through since DCA is disabled. */
941 case DCA_PROVIDER_REMOVE:
942 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
943 dca_remove_requester(dev);
944 adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
945 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 1);
946 }
947 break;
948 }
949
652f093f 950 return 0;
bd0362dd
JC
951}
952
5dd2d332 953#endif /* CONFIG_IXGBE_DCA */
9a799d71
AK
954/**
955 * ixgbe_receive_skb - Send a completed packet up the stack
956 * @adapter: board private structure
957 * @skb: packet to send up
177db6ff
MC
958 * @status: hardware indication of status of receive
959 * @rx_ring: rx descriptor ring (for a specific queue) to setup
960 * @rx_desc: rx descriptor
9a799d71 961 **/
78b6f4ce 962static void ixgbe_receive_skb(struct ixgbe_q_vector *q_vector,
e8e9f696
JP
963 struct sk_buff *skb, u8 status,
964 struct ixgbe_ring *ring,
965 union ixgbe_adv_rx_desc *rx_desc)
9a799d71 966{
78b6f4ce
HX
967 struct ixgbe_adapter *adapter = q_vector->adapter;
968 struct napi_struct *napi = &q_vector->napi;
177db6ff
MC
969 bool is_vlan = (status & IXGBE_RXD_STAT_VP);
970 u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
9a799d71 971
f62bbb5e
JG
972 if (is_vlan && (tag & VLAN_VID_MASK))
973 __vlan_hwaccel_put_tag(skb, tag);
974
975 if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL))
976 napi_gro_receive(napi, skb);
977 else
978 netif_rx(skb);
9a799d71
AK
979}
980
e59bd25d
AV
981/**
982 * ixgbe_rx_checksum - indicate in skb if hw indicated a good cksum
983 * @adapter: address of board private structure
984 * @status_err: hardware indication of status of receive
985 * @skb: skb currently being received and modified
986 **/
9a799d71 987static inline void ixgbe_rx_checksum(struct ixgbe_adapter *adapter,
8bae1b2b
DS
988 union ixgbe_adv_rx_desc *rx_desc,
989 struct sk_buff *skb)
9a799d71 990{
8bae1b2b
DS
991 u32 status_err = le32_to_cpu(rx_desc->wb.upper.status_error);
992
bc8acf2c 993 skb_checksum_none_assert(skb);
9a799d71 994
712744be
JB
995 /* Rx csum disabled */
996 if (!(adapter->flags & IXGBE_FLAG_RX_CSUM_ENABLED))
9a799d71 997 return;
e59bd25d
AV
998
999 /* if IP and error */
1000 if ((status_err & IXGBE_RXD_STAT_IPCS) &&
1001 (status_err & IXGBE_RXDADV_ERR_IPE)) {
9a799d71
AK
1002 adapter->hw_csum_rx_error++;
1003 return;
1004 }
e59bd25d
AV
1005
1006 if (!(status_err & IXGBE_RXD_STAT_L4CS))
1007 return;
1008
1009 if (status_err & IXGBE_RXDADV_ERR_TCPE) {
8bae1b2b
DS
1010 u16 pkt_info = rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
1011
1012 /*
1013 * 82599 errata, UDP frames with a 0 checksum can be marked as
1014 * checksum errors.
1015 */
1016 if ((pkt_info & IXGBE_RXDADV_PKTTYPE_UDP) &&
1017 (adapter->hw.mac.type == ixgbe_mac_82599EB))
1018 return;
1019
e59bd25d
AV
1020 adapter->hw_csum_rx_error++;
1021 return;
1022 }
1023
9a799d71 1024 /* It must be a TCP or UDP packet with a valid checksum */
e59bd25d 1025 skb->ip_summed = CHECKSUM_UNNECESSARY;
9a799d71
AK
1026}
1027
84ea2591 1028static inline void ixgbe_release_rx_desc(struct ixgbe_ring *rx_ring, u32 val)
e8e26350
PW
1029{
1030 /*
1031 * Force memory writes to complete before letting h/w
1032 * know there are new descriptors to fetch. (Only
1033 * applicable for weak-ordered memory model archs,
1034 * such as IA-64).
1035 */
1036 wmb();
84ea2591 1037 writel(val, rx_ring->tail);
e8e26350
PW
1038}
1039
9a799d71
AK
1040/**
1041 * ixgbe_alloc_rx_buffers - Replace used receive buffers; packet split
fc77dc3c
AD
1042 * @rx_ring: ring to place buffers on
1043 * @cleaned_count: number of buffers to replace
9a799d71 1044 **/
fc77dc3c 1045void ixgbe_alloc_rx_buffers(struct ixgbe_ring *rx_ring, u16 cleaned_count)
9a799d71 1046{
9a799d71 1047 union ixgbe_adv_rx_desc *rx_desc;
3a581073 1048 struct ixgbe_rx_buffer *bi;
d5f398ed
AD
1049 struct sk_buff *skb;
1050 u16 i = rx_ring->next_to_use;
9a799d71 1051
fc77dc3c
AD
1052 /* do nothing if no valid netdev defined */
1053 if (!rx_ring->netdev)
1054 return;
1055
9a799d71 1056 while (cleaned_count--) {
31f05a2d 1057 rx_desc = IXGBE_RX_DESC_ADV(rx_ring, i);
d5f398ed
AD
1058 bi = &rx_ring->rx_buffer_info[i];
1059 skb = bi->skb;
9a799d71 1060
d5f398ed 1061 if (!skb) {
fc77dc3c 1062 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
d5f398ed 1063 rx_ring->rx_buf_len);
9a799d71 1064 if (!skb) {
5b7da515 1065 rx_ring->rx_stats.alloc_rx_buff_failed++;
9a799d71
AK
1066 goto no_buffers;
1067 }
d716a7d8
AD
1068 /* initialize queue mapping */
1069 skb_record_rx_queue(skb, rx_ring->queue_index);
d5f398ed 1070 bi->skb = skb;
d716a7d8 1071 }
9a799d71 1072
d716a7d8 1073 if (!bi->dma) {
b6ec895e 1074 bi->dma = dma_map_single(rx_ring->dev,
d5f398ed 1075 skb->data,
e8e9f696 1076 rx_ring->rx_buf_len,
1b507730 1077 DMA_FROM_DEVICE);
b6ec895e 1078 if (dma_mapping_error(rx_ring->dev, bi->dma)) {
5b7da515 1079 rx_ring->rx_stats.alloc_rx_buff_failed++;
d5f398ed
AD
1080 bi->dma = 0;
1081 goto no_buffers;
1082 }
9a799d71 1083 }
d5f398ed 1084
7d637bcc 1085 if (ring_is_ps_enabled(rx_ring)) {
d5f398ed 1086 if (!bi->page) {
fc77dc3c 1087 bi->page = netdev_alloc_page(rx_ring->netdev);
d5f398ed 1088 if (!bi->page) {
5b7da515 1089 rx_ring->rx_stats.alloc_rx_page_failed++;
d5f398ed
AD
1090 goto no_buffers;
1091 }
1092 }
1093
1094 if (!bi->page_dma) {
1095 /* use a half page if we're re-using */
1096 bi->page_offset ^= PAGE_SIZE / 2;
b6ec895e 1097 bi->page_dma = dma_map_page(rx_ring->dev,
d5f398ed
AD
1098 bi->page,
1099 bi->page_offset,
1100 PAGE_SIZE / 2,
1101 DMA_FROM_DEVICE);
b6ec895e 1102 if (dma_mapping_error(rx_ring->dev,
d5f398ed 1103 bi->page_dma)) {
5b7da515 1104 rx_ring->rx_stats.alloc_rx_page_failed++;
d5f398ed
AD
1105 bi->page_dma = 0;
1106 goto no_buffers;
1107 }
1108 }
1109
1110 /* Refresh the desc even if buffer_addrs didn't change
1111 * because each write-back erases this info. */
3a581073
JB
1112 rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
1113 rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
9a799d71 1114 } else {
3a581073 1115 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
84418e3b 1116 rx_desc->read.hdr_addr = 0;
9a799d71
AK
1117 }
1118
1119 i++;
1120 if (i == rx_ring->count)
1121 i = 0;
9a799d71 1122 }
7c6e0a43 1123
9a799d71
AK
1124no_buffers:
1125 if (rx_ring->next_to_use != i) {
1126 rx_ring->next_to_use = i;
84ea2591 1127 ixgbe_release_rx_desc(rx_ring, i);
9a799d71
AK
1128 }
1129}
1130
7c6e0a43
JB
1131static inline u16 ixgbe_get_hdr_info(union ixgbe_adv_rx_desc *rx_desc)
1132{
1133 return rx_desc->wb.lower.lo_dword.hs_rss.hdr_info;
1134}
1135
1136static inline u16 ixgbe_get_pkt_info(union ixgbe_adv_rx_desc *rx_desc)
1137{
1138 return rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
1139}
1140
f8212f97
AD
1141static inline u32 ixgbe_get_rsc_count(union ixgbe_adv_rx_desc *rx_desc)
1142{
1143 return (le32_to_cpu(rx_desc->wb.lower.lo_dword.data) &
e8e9f696
JP
1144 IXGBE_RXDADV_RSCCNT_MASK) >>
1145 IXGBE_RXDADV_RSCCNT_SHIFT;
f8212f97
AD
1146}
1147
1148/**
1149 * ixgbe_transform_rsc_queue - change rsc queue into a full packet
1150 * @skb: pointer to the last skb in the rsc queue
94b982b2 1151 * @count: pointer to number of packets coalesced in this context
f8212f97
AD
1152 *
1153 * This function changes a queue full of hw rsc buffers into a completed
1154 * packet. It uses the ->prev pointers to find the first packet and then
1155 * turns it into the frag list owner.
1156 **/
94b982b2 1157static inline struct sk_buff *ixgbe_transform_rsc_queue(struct sk_buff *skb,
e8e9f696 1158 u64 *count)
f8212f97
AD
1159{
1160 unsigned int frag_list_size = 0;
1161
1162 while (skb->prev) {
1163 struct sk_buff *prev = skb->prev;
1164 frag_list_size += skb->len;
1165 skb->prev = NULL;
1166 skb = prev;
94b982b2 1167 *count += 1;
f8212f97
AD
1168 }
1169
1170 skb_shinfo(skb)->frag_list = skb->next;
1171 skb->next = NULL;
1172 skb->len += frag_list_size;
1173 skb->data_len += frag_list_size;
1174 skb->truesize += frag_list_size;
1175 return skb;
1176}
1177
43634e82
MC
1178struct ixgbe_rsc_cb {
1179 dma_addr_t dma;
e8171aaa 1180 bool delay_unmap;
43634e82
MC
1181};
1182
1183#define IXGBE_RSC_CB(skb) ((struct ixgbe_rsc_cb *)(skb)->cb)
1184
78b6f4ce 1185static bool ixgbe_clean_rx_irq(struct ixgbe_q_vector *q_vector,
e8e9f696
JP
1186 struct ixgbe_ring *rx_ring,
1187 int *work_done, int work_to_do)
9a799d71 1188{
78b6f4ce 1189 struct ixgbe_adapter *adapter = q_vector->adapter;
9a799d71
AK
1190 union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
1191 struct ixgbe_rx_buffer *rx_buffer_info, *next_buffer;
1192 struct sk_buff *skb;
f8212f97 1193 unsigned int i, rsc_count = 0;
7c6e0a43 1194 u32 len, staterr;
177db6ff
MC
1195 u16 hdr_info;
1196 bool cleaned = false;
9a799d71 1197 int cleaned_count = 0;
d2f4fbe2 1198 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
3d8fd385
YZ
1199#ifdef IXGBE_FCOE
1200 int ddp_bytes = 0;
1201#endif /* IXGBE_FCOE */
9a799d71
AK
1202
1203 i = rx_ring->next_to_clean;
31f05a2d 1204 rx_desc = IXGBE_RX_DESC_ADV(rx_ring, i);
9a799d71
AK
1205 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
1206 rx_buffer_info = &rx_ring->rx_buffer_info[i];
9a799d71
AK
1207
1208 while (staterr & IXGBE_RXD_STAT_DD) {
7c6e0a43 1209 u32 upper_len = 0;
9a799d71
AK
1210 if (*work_done >= work_to_do)
1211 break;
1212 (*work_done)++;
1213
3c945e5b 1214 rmb(); /* read descriptor and rx_buffer_info after status DD */
7d637bcc 1215 if (ring_is_ps_enabled(rx_ring)) {
7c6e0a43
JB
1216 hdr_info = le16_to_cpu(ixgbe_get_hdr_info(rx_desc));
1217 len = (hdr_info & IXGBE_RXDADV_HDRBUFLEN_MASK) >>
762f4c57 1218 IXGBE_RXDADV_HDRBUFLEN_SHIFT;
9a799d71 1219 upper_len = le16_to_cpu(rx_desc->wb.upper.length);
0b746e08
SN
1220 if ((len > IXGBE_RX_HDR_SIZE) ||
1221 (upper_len && !(hdr_info & IXGBE_RXDADV_SPH)))
1222 len = IXGBE_RX_HDR_SIZE;
7c6e0a43 1223 } else {
9a799d71 1224 len = le16_to_cpu(rx_desc->wb.upper.length);
7c6e0a43 1225 }
9a799d71
AK
1226
1227 cleaned = true;
1228 skb = rx_buffer_info->skb;
7ca3bc58 1229 prefetch(skb->data);
9a799d71
AK
1230 rx_buffer_info->skb = NULL;
1231
21fa4e66 1232 if (rx_buffer_info->dma) {
43634e82
MC
1233 if ((adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED) &&
1234 (!(staterr & IXGBE_RXD_STAT_EOP)) &&
e8171aaa 1235 (!(skb->prev))) {
43634e82
MC
1236 /*
1237 * When HWRSC is enabled, delay unmapping
1238 * of the first packet. It carries the
1239 * header information, HW may still
1240 * access the header after the writeback.
1241 * Only unmap it when EOP is reached
1242 */
e8171aaa 1243 IXGBE_RSC_CB(skb)->delay_unmap = true;
43634e82 1244 IXGBE_RSC_CB(skb)->dma = rx_buffer_info->dma;
e8171aaa 1245 } else {
b6ec895e 1246 dma_unmap_single(rx_ring->dev,
e8e9f696
JP
1247 rx_buffer_info->dma,
1248 rx_ring->rx_buf_len,
1249 DMA_FROM_DEVICE);
e8171aaa 1250 }
4f57ca6e 1251 rx_buffer_info->dma = 0;
9a799d71
AK
1252 skb_put(skb, len);
1253 }
1254
1255 if (upper_len) {
b6ec895e
AD
1256 dma_unmap_page(rx_ring->dev,
1257 rx_buffer_info->page_dma,
1258 PAGE_SIZE / 2,
1259 DMA_FROM_DEVICE);
9a799d71
AK
1260 rx_buffer_info->page_dma = 0;
1261 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
e8e9f696
JP
1262 rx_buffer_info->page,
1263 rx_buffer_info->page_offset,
1264 upper_len);
762f4c57
JB
1265
1266 if ((rx_ring->rx_buf_len > (PAGE_SIZE / 2)) ||
1267 (page_count(rx_buffer_info->page) != 1))
1268 rx_buffer_info->page = NULL;
1269 else
1270 get_page(rx_buffer_info->page);
9a799d71
AK
1271
1272 skb->len += upper_len;
1273 skb->data_len += upper_len;
1274 skb->truesize += upper_len;
1275 }
1276
1277 i++;
1278 if (i == rx_ring->count)
1279 i = 0;
9a799d71 1280
31f05a2d 1281 next_rxd = IXGBE_RX_DESC_ADV(rx_ring, i);
9a799d71 1282 prefetch(next_rxd);
9a799d71 1283 cleaned_count++;
f8212f97 1284
7d637bcc 1285 if (ring_is_rsc_enabled(rx_ring))
f8212f97
AD
1286 rsc_count = ixgbe_get_rsc_count(rx_desc);
1287
1288 if (rsc_count) {
1289 u32 nextp = (staterr & IXGBE_RXDADV_NEXTP_MASK) >>
1290 IXGBE_RXDADV_NEXTP_SHIFT;
1291 next_buffer = &rx_ring->rx_buffer_info[nextp];
f8212f97
AD
1292 } else {
1293 next_buffer = &rx_ring->rx_buffer_info[i];
1294 }
1295
9a799d71 1296 if (staterr & IXGBE_RXD_STAT_EOP) {
f8212f97 1297 if (skb->prev)
e8e9f696 1298 skb = ixgbe_transform_rsc_queue(skb,
5b7da515 1299 &(rx_ring->rx_stats.rsc_count));
7d637bcc 1300 if (ring_is_rsc_enabled(rx_ring)) {
e8171aaa 1301 if (IXGBE_RSC_CB(skb)->delay_unmap) {
b6ec895e 1302 dma_unmap_single(rx_ring->dev,
1b507730 1303 IXGBE_RSC_CB(skb)->dma,
e8e9f696 1304 rx_ring->rx_buf_len,
1b507730 1305 DMA_FROM_DEVICE);
fd3686a8 1306 IXGBE_RSC_CB(skb)->dma = 0;
e8171aaa 1307 IXGBE_RSC_CB(skb)->delay_unmap = false;
fd3686a8 1308 }
7d637bcc 1309 if (ring_is_ps_enabled(rx_ring))
5b7da515
AD
1310 rx_ring->rx_stats.rsc_count +=
1311 skb_shinfo(skb)->nr_frags;
94b982b2 1312 else
5b7da515
AD
1313 rx_ring->rx_stats.rsc_count++;
1314 rx_ring->rx_stats.rsc_flush++;
94b982b2 1315 }
de1036b1 1316 u64_stats_update_begin(&rx_ring->syncp);
9a799d71
AK
1317 rx_ring->stats.packets++;
1318 rx_ring->stats.bytes += skb->len;
de1036b1 1319 u64_stats_update_end(&rx_ring->syncp);
9a799d71 1320 } else {
7d637bcc 1321 if (ring_is_ps_enabled(rx_ring)) {
f8212f97
AD
1322 rx_buffer_info->skb = next_buffer->skb;
1323 rx_buffer_info->dma = next_buffer->dma;
1324 next_buffer->skb = skb;
1325 next_buffer->dma = 0;
1326 } else {
1327 skb->next = next_buffer->skb;
1328 skb->next->prev = skb;
1329 }
5b7da515 1330 rx_ring->rx_stats.non_eop_descs++;
9a799d71
AK
1331 goto next_desc;
1332 }
1333
1334 if (staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK) {
1335 dev_kfree_skb_irq(skb);
1336 goto next_desc;
1337 }
1338
8bae1b2b 1339 ixgbe_rx_checksum(adapter, rx_desc, skb);
d2f4fbe2
AV
1340
1341 /* probably a little skewed due to removing CRC */
1342 total_rx_bytes += skb->len;
1343 total_rx_packets++;
1344
fc77dc3c 1345 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
332d4a7d
YZ
1346#ifdef IXGBE_FCOE
1347 /* if ddp, not passing to ULD unless for FCP_RSP or error */
3d8fd385
YZ
1348 if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED) {
1349 ddp_bytes = ixgbe_fcoe_ddp(adapter, rx_desc, skb);
1350 if (!ddp_bytes)
332d4a7d 1351 goto next_desc;
3d8fd385 1352 }
332d4a7d 1353#endif /* IXGBE_FCOE */
fdaff1ce 1354 ixgbe_receive_skb(q_vector, skb, staterr, rx_ring, rx_desc);
9a799d71
AK
1355
1356next_desc:
1357 rx_desc->wb.upper.status_error = 0;
1358
1359 /* return some buffers to hardware, one at a time is too slow */
1360 if (cleaned_count >= IXGBE_RX_BUFFER_WRITE) {
fc77dc3c 1361 ixgbe_alloc_rx_buffers(rx_ring, cleaned_count);
9a799d71
AK
1362 cleaned_count = 0;
1363 }
1364
1365 /* use prefetched values */
1366 rx_desc = next_rxd;
f8212f97 1367 rx_buffer_info = &rx_ring->rx_buffer_info[i];
9a799d71
AK
1368
1369 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
177db6ff
MC
1370 }
1371
9a799d71
AK
1372 rx_ring->next_to_clean = i;
1373 cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
1374
1375 if (cleaned_count)
fc77dc3c 1376 ixgbe_alloc_rx_buffers(rx_ring, cleaned_count);
9a799d71 1377
3d8fd385
YZ
1378#ifdef IXGBE_FCOE
1379 /* include DDPed FCoE data */
1380 if (ddp_bytes > 0) {
1381 unsigned int mss;
1382
fc77dc3c 1383 mss = rx_ring->netdev->mtu - sizeof(struct fcoe_hdr) -
3d8fd385
YZ
1384 sizeof(struct fc_frame_header) -
1385 sizeof(struct fcoe_crc_eof);
1386 if (mss > 512)
1387 mss &= ~511;
1388 total_rx_bytes += ddp_bytes;
1389 total_rx_packets += DIV_ROUND_UP(ddp_bytes, mss);
1390 }
1391#endif /* IXGBE_FCOE */
1392
f494e8fa
AV
1393 rx_ring->total_packets += total_rx_packets;
1394 rx_ring->total_bytes += total_rx_bytes;
f494e8fa 1395
9a799d71
AK
1396 return cleaned;
1397}
1398
021230d4 1399static int ixgbe_clean_rxonly(struct napi_struct *, int);
9a799d71
AK
1400/**
1401 * ixgbe_configure_msix - Configure MSI-X hardware
1402 * @adapter: board private structure
1403 *
1404 * ixgbe_configure_msix sets up the hardware to properly generate MSI-X
1405 * interrupts.
1406 **/
1407static void ixgbe_configure_msix(struct ixgbe_adapter *adapter)
1408{
021230d4
AV
1409 struct ixgbe_q_vector *q_vector;
1410 int i, j, q_vectors, v_idx, r_idx;
1411 u32 mask;
9a799d71 1412
021230d4 1413 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
9a799d71 1414
4df10466
JB
1415 /*
1416 * Populate the IVAR table and set the ITR values to the
021230d4
AV
1417 * corresponding register.
1418 */
1419 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
7a921c93 1420 q_vector = adapter->q_vector[v_idx];
984b3f57 1421 /* XXX for_each_set_bit(...) */
021230d4 1422 r_idx = find_first_bit(q_vector->rxr_idx,
e8e9f696 1423 adapter->num_rx_queues);
021230d4
AV
1424
1425 for (i = 0; i < q_vector->rxr_count; i++) {
4a0b9ca0 1426 j = adapter->rx_ring[r_idx]->reg_idx;
e8e26350 1427 ixgbe_set_ivar(adapter, 0, j, v_idx);
021230d4 1428 r_idx = find_next_bit(q_vector->rxr_idx,
e8e9f696
JP
1429 adapter->num_rx_queues,
1430 r_idx + 1);
021230d4
AV
1431 }
1432 r_idx = find_first_bit(q_vector->txr_idx,
e8e9f696 1433 adapter->num_tx_queues);
021230d4
AV
1434
1435 for (i = 0; i < q_vector->txr_count; i++) {
4a0b9ca0 1436 j = adapter->tx_ring[r_idx]->reg_idx;
e8e26350 1437 ixgbe_set_ivar(adapter, 1, j, v_idx);
021230d4 1438 r_idx = find_next_bit(q_vector->txr_idx,
e8e9f696
JP
1439 adapter->num_tx_queues,
1440 r_idx + 1);
021230d4
AV
1441 }
1442
021230d4 1443 if (q_vector->txr_count && !q_vector->rxr_count)
f7554a2b
NS
1444 /* tx only */
1445 q_vector->eitr = adapter->tx_eitr_param;
509ee935 1446 else if (q_vector->rxr_count)
f7554a2b
NS
1447 /* rx or mixed */
1448 q_vector->eitr = adapter->rx_eitr_param;
021230d4 1449
fe49f04a 1450 ixgbe_write_eitr(q_vector);
b25ebfd2
PW
1451 /* If Flow Director is enabled, set interrupt affinity */
1452 if ((adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) ||
1453 (adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE)) {
1454 /*
1455 * Allocate the affinity_hint cpumask, assign the mask
1456 * for this vector, and set our affinity_hint for
1457 * this irq.
1458 */
1459 if (!alloc_cpumask_var(&q_vector->affinity_mask,
1460 GFP_KERNEL))
1461 return;
1462 cpumask_set_cpu(v_idx, q_vector->affinity_mask);
1463 irq_set_affinity_hint(adapter->msix_entries[v_idx].vector,
1464 q_vector->affinity_mask);
1465 }
9a799d71
AK
1466 }
1467
e8e26350
PW
1468 if (adapter->hw.mac.type == ixgbe_mac_82598EB)
1469 ixgbe_set_ivar(adapter, -1, IXGBE_IVAR_OTHER_CAUSES_INDEX,
e8e9f696 1470 v_idx);
e8e26350
PW
1471 else if (adapter->hw.mac.type == ixgbe_mac_82599EB)
1472 ixgbe_set_ivar(adapter, -1, 1, v_idx);
021230d4
AV
1473 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(v_idx), 1950);
1474
41fb9248 1475 /* set up to autoclear timer, and the vectors */
021230d4 1476 mask = IXGBE_EIMS_ENABLE_MASK;
1cdd1ec8
GR
1477 if (adapter->num_vfs)
1478 mask &= ~(IXGBE_EIMS_OTHER |
1479 IXGBE_EIMS_MAILBOX |
1480 IXGBE_EIMS_LSC);
1481 else
1482 mask &= ~(IXGBE_EIMS_OTHER | IXGBE_EIMS_LSC);
021230d4 1483 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC, mask);
9a799d71
AK
1484}
1485
f494e8fa
AV
1486enum latency_range {
1487 lowest_latency = 0,
1488 low_latency = 1,
1489 bulk_latency = 2,
1490 latency_invalid = 255
1491};
1492
1493/**
1494 * ixgbe_update_itr - update the dynamic ITR value based on statistics
1495 * @adapter: pointer to adapter
1496 * @eitr: eitr setting (ints per sec) to give last timeslice
1497 * @itr_setting: current throttle rate in ints/second
1498 * @packets: the number of packets during this measurement interval
1499 * @bytes: the number of bytes during this measurement interval
1500 *
1501 * Stores a new ITR value based on packets and byte
1502 * counts during the last interrupt. The advantage of per interrupt
1503 * computation is faster updates and more accurate ITR for the current
1504 * traffic pattern. Constants in this function were computed
1505 * based on theoretical maximum wire speed and thresholds were set based
1506 * on testing data as well as attempting to minimize response time
1507 * while increasing bulk throughput.
1508 * this functionality is controlled by the InterruptThrottleRate module
1509 * parameter (see ixgbe_param.c)
1510 **/
1511static u8 ixgbe_update_itr(struct ixgbe_adapter *adapter,
e8e9f696
JP
1512 u32 eitr, u8 itr_setting,
1513 int packets, int bytes)
f494e8fa
AV
1514{
1515 unsigned int retval = itr_setting;
1516 u32 timepassed_us;
1517 u64 bytes_perint;
1518
1519 if (packets == 0)
1520 goto update_itr_done;
1521
1522
1523 /* simple throttlerate management
1524 * 0-20MB/s lowest (100000 ints/s)
1525 * 20-100MB/s low (20000 ints/s)
1526 * 100-1249MB/s bulk (8000 ints/s)
1527 */
1528 /* what was last interrupt timeslice? */
1529 timepassed_us = 1000000/eitr;
1530 bytes_perint = bytes / timepassed_us; /* bytes/usec */
1531
1532 switch (itr_setting) {
1533 case lowest_latency:
1534 if (bytes_perint > adapter->eitr_low)
1535 retval = low_latency;
1536 break;
1537 case low_latency:
1538 if (bytes_perint > adapter->eitr_high)
1539 retval = bulk_latency;
1540 else if (bytes_perint <= adapter->eitr_low)
1541 retval = lowest_latency;
1542 break;
1543 case bulk_latency:
1544 if (bytes_perint <= adapter->eitr_high)
1545 retval = low_latency;
1546 break;
1547 }
1548
1549update_itr_done:
1550 return retval;
1551}
1552
509ee935
JB
1553/**
1554 * ixgbe_write_eitr - write EITR register in hardware specific way
fe49f04a 1555 * @q_vector: structure containing interrupt and ring information
509ee935
JB
1556 *
1557 * This function is made to be called by ethtool and by the driver
1558 * when it needs to update EITR registers at runtime. Hardware
1559 * specific quirks/differences are taken care of here.
1560 */
fe49f04a 1561void ixgbe_write_eitr(struct ixgbe_q_vector *q_vector)
509ee935 1562{
fe49f04a 1563 struct ixgbe_adapter *adapter = q_vector->adapter;
509ee935 1564 struct ixgbe_hw *hw = &adapter->hw;
fe49f04a
AD
1565 int v_idx = q_vector->v_idx;
1566 u32 itr_reg = EITR_INTS_PER_SEC_TO_REG(q_vector->eitr);
1567
509ee935
JB
1568 if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
1569 /* must write high and low 16 bits to reset counter */
1570 itr_reg |= (itr_reg << 16);
1571 } else if (adapter->hw.mac.type == ixgbe_mac_82599EB) {
f8d1dcaf
JB
1572 /*
1573 * 82599 can support a value of zero, so allow it for
1574 * max interrupt rate, but there is an errata where it can
1575 * not be zero with RSC
1576 */
1577 if (itr_reg == 8 &&
1578 !(adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED))
1579 itr_reg = 0;
1580
509ee935
JB
1581 /*
1582 * set the WDIS bit to not clear the timer bits and cause an
1583 * immediate assertion of the interrupt
1584 */
1585 itr_reg |= IXGBE_EITR_CNT_WDIS;
1586 }
1587 IXGBE_WRITE_REG(hw, IXGBE_EITR(v_idx), itr_reg);
1588}
1589
f494e8fa
AV
1590static void ixgbe_set_itr_msix(struct ixgbe_q_vector *q_vector)
1591{
1592 struct ixgbe_adapter *adapter = q_vector->adapter;
f494e8fa
AV
1593 u32 new_itr;
1594 u8 current_itr, ret_itr;
fe49f04a 1595 int i, r_idx;
f494e8fa
AV
1596 struct ixgbe_ring *rx_ring, *tx_ring;
1597
1598 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
1599 for (i = 0; i < q_vector->txr_count; i++) {
4a0b9ca0 1600 tx_ring = adapter->tx_ring[r_idx];
f494e8fa 1601 ret_itr = ixgbe_update_itr(adapter, q_vector->eitr,
e8e9f696
JP
1602 q_vector->tx_itr,
1603 tx_ring->total_packets,
1604 tx_ring->total_bytes);
f494e8fa
AV
1605 /* if the result for this queue would decrease interrupt
1606 * rate for this vector then use that result */
30efa5a3 1607 q_vector->tx_itr = ((q_vector->tx_itr > ret_itr) ?
e8e9f696 1608 q_vector->tx_itr - 1 : ret_itr);
f494e8fa 1609 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
e8e9f696 1610 r_idx + 1);
f494e8fa
AV
1611 }
1612
1613 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
1614 for (i = 0; i < q_vector->rxr_count; i++) {
4a0b9ca0 1615 rx_ring = adapter->rx_ring[r_idx];
f494e8fa 1616 ret_itr = ixgbe_update_itr(adapter, q_vector->eitr,
e8e9f696
JP
1617 q_vector->rx_itr,
1618 rx_ring->total_packets,
1619 rx_ring->total_bytes);
f494e8fa
AV
1620 /* if the result for this queue would decrease interrupt
1621 * rate for this vector then use that result */
30efa5a3 1622 q_vector->rx_itr = ((q_vector->rx_itr > ret_itr) ?
e8e9f696 1623 q_vector->rx_itr - 1 : ret_itr);
f494e8fa 1624 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
e8e9f696 1625 r_idx + 1);
f494e8fa
AV
1626 }
1627
30efa5a3 1628 current_itr = max(q_vector->rx_itr, q_vector->tx_itr);
f494e8fa
AV
1629
1630 switch (current_itr) {
1631 /* counts and packets in update_itr are dependent on these numbers */
1632 case lowest_latency:
1633 new_itr = 100000;
1634 break;
1635 case low_latency:
1636 new_itr = 20000; /* aka hwitr = ~200 */
1637 break;
1638 case bulk_latency:
1639 default:
1640 new_itr = 8000;
1641 break;
1642 }
1643
1644 if (new_itr != q_vector->eitr) {
fe49f04a
AD
1645 /* do an exponential smoothing */
1646 new_itr = ((q_vector->eitr * 90)/100) + ((new_itr * 10)/100);
509ee935
JB
1647
1648 /* save the algorithm value here, not the smoothed one */
1649 q_vector->eitr = new_itr;
fe49f04a
AD
1650
1651 ixgbe_write_eitr(q_vector);
f494e8fa 1652 }
f494e8fa
AV
1653}
1654
119fc60a
MC
1655/**
1656 * ixgbe_check_overtemp_task - worker thread to check over tempurature
1657 * @work: pointer to work_struct containing our data
1658 **/
1659static void ixgbe_check_overtemp_task(struct work_struct *work)
1660{
1661 struct ixgbe_adapter *adapter = container_of(work,
e8e9f696
JP
1662 struct ixgbe_adapter,
1663 check_overtemp_task);
119fc60a
MC
1664 struct ixgbe_hw *hw = &adapter->hw;
1665 u32 eicr = adapter->interrupt_event;
1666
7ca647bd
JP
1667 if (!(adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE))
1668 return;
1669
1670 switch (hw->device_id) {
1671 case IXGBE_DEV_ID_82599_T3_LOM: {
1672 u32 autoneg;
1673 bool link_up = false;
1674
1675 if (hw->mac.ops.check_link)
1676 hw->mac.ops.check_link(hw, &autoneg, &link_up, false);
1677
1678 if (((eicr & IXGBE_EICR_GPI_SDP0) && (!link_up)) ||
1679 (eicr & IXGBE_EICR_LSC))
1680 /* Check if this is due to overtemp */
1681 if (hw->phy.ops.check_overtemp(hw) == IXGBE_ERR_OVERTEMP)
1682 break;
1683 return;
1684 }
1685 default:
1686 if (!(eicr & IXGBE_EICR_GPI_SDP0))
119fc60a 1687 return;
7ca647bd 1688 break;
119fc60a 1689 }
7ca647bd
JP
1690 e_crit(drv,
1691 "Network adapter has been stopped because it has over heated. "
1692 "Restart the computer. If the problem persists, "
1693 "power off the system and replace the adapter\n");
1694 /* write to clear the interrupt */
1695 IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP0);
119fc60a
MC
1696}
1697
0befdb3e
JB
1698static void ixgbe_check_fan_failure(struct ixgbe_adapter *adapter, u32 eicr)
1699{
1700 struct ixgbe_hw *hw = &adapter->hw;
1701
1702 if ((adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE) &&
1703 (eicr & IXGBE_EICR_GPI_SDP1)) {
396e799c 1704 e_crit(probe, "Fan has stopped, replace the adapter\n");
0befdb3e
JB
1705 /* write to clear the interrupt */
1706 IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1);
1707 }
1708}
cf8280ee 1709
e8e26350
PW
1710static void ixgbe_check_sfp_event(struct ixgbe_adapter *adapter, u32 eicr)
1711{
1712 struct ixgbe_hw *hw = &adapter->hw;
1713
73c4b7cd
AD
1714 if (eicr & IXGBE_EICR_GPI_SDP2) {
1715 /* Clear the interrupt */
1716 IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP2);
1717 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1718 schedule_work(&adapter->sfp_config_module_task);
1719 }
1720
e8e26350
PW
1721 if (eicr & IXGBE_EICR_GPI_SDP1) {
1722 /* Clear the interrupt */
1723 IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1);
73c4b7cd
AD
1724 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1725 schedule_work(&adapter->multispeed_fiber_task);
e8e26350
PW
1726 }
1727}
1728
cf8280ee
JB
1729static void ixgbe_check_lsc(struct ixgbe_adapter *adapter)
1730{
1731 struct ixgbe_hw *hw = &adapter->hw;
1732
1733 adapter->lsc_int++;
1734 adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
1735 adapter->link_check_timeout = jiffies;
1736 if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
1737 IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
8a0717f3 1738 IXGBE_WRITE_FLUSH(hw);
cf8280ee
JB
1739 schedule_work(&adapter->watchdog_task);
1740 }
1741}
1742
9a799d71
AK
1743static irqreturn_t ixgbe_msix_lsc(int irq, void *data)
1744{
1745 struct net_device *netdev = data;
1746 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1747 struct ixgbe_hw *hw = &adapter->hw;
54037505
DS
1748 u32 eicr;
1749
1750 /*
1751 * Workaround for Silicon errata. Use clear-by-write instead
1752 * of clear-by-read. Reading with EICS will return the
1753 * interrupt causes without clearing, which later be done
1754 * with the write to EICR.
1755 */
1756 eicr = IXGBE_READ_REG(hw, IXGBE_EICS);
1757 IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr);
9a799d71 1758
cf8280ee
JB
1759 if (eicr & IXGBE_EICR_LSC)
1760 ixgbe_check_lsc(adapter);
d4f80882 1761
1cdd1ec8
GR
1762 if (eicr & IXGBE_EICR_MAILBOX)
1763 ixgbe_msg_task(adapter);
1764
e8e26350
PW
1765 if (hw->mac.type == ixgbe_mac_82598EB)
1766 ixgbe_check_fan_failure(adapter, eicr);
0befdb3e 1767
c4cf55e5 1768 if (hw->mac.type == ixgbe_mac_82599EB) {
e8e26350 1769 ixgbe_check_sfp_event(adapter, eicr);
119fc60a
MC
1770 adapter->interrupt_event = eicr;
1771 if ((adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE) &&
1772 ((eicr & IXGBE_EICR_GPI_SDP0) || (eicr & IXGBE_EICR_LSC)))
1773 schedule_work(&adapter->check_overtemp_task);
c4cf55e5
PWJ
1774
1775 /* Handle Flow Director Full threshold interrupt */
1776 if (eicr & IXGBE_EICR_FLOW_DIR) {
1777 int i;
1778 IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_FLOW_DIR);
1779 /* Disable transmits before FDIR Re-initialization */
1780 netif_tx_stop_all_queues(netdev);
1781 for (i = 0; i < adapter->num_tx_queues; i++) {
1782 struct ixgbe_ring *tx_ring =
e8e9f696 1783 adapter->tx_ring[i];
7d637bcc
AD
1784 if (test_and_clear_bit(__IXGBE_TX_FDIR_INIT_DONE,
1785 &tx_ring->state))
c4cf55e5
PWJ
1786 schedule_work(&adapter->fdir_reinit_task);
1787 }
1788 }
1789 }
d4f80882
AV
1790 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1791 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_OTHER);
9a799d71
AK
1792
1793 return IRQ_HANDLED;
1794}
1795
fe49f04a
AD
1796static inline void ixgbe_irq_enable_queues(struct ixgbe_adapter *adapter,
1797 u64 qmask)
1798{
1799 u32 mask;
1800
1801 if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
1802 mask = (IXGBE_EIMS_RTX_QUEUE & qmask);
1803 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, mask);
1804 } else {
1805 mask = (qmask & 0xFFFFFFFF);
1806 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS_EX(0), mask);
1807 mask = (qmask >> 32);
1808 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS_EX(1), mask);
1809 }
1810 /* skip the flush */
1811}
1812
1813static inline void ixgbe_irq_disable_queues(struct ixgbe_adapter *adapter,
e8e9f696 1814 u64 qmask)
fe49f04a
AD
1815{
1816 u32 mask;
1817
1818 if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
1819 mask = (IXGBE_EIMS_RTX_QUEUE & qmask);
1820 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, mask);
1821 } else {
1822 mask = (qmask & 0xFFFFFFFF);
1823 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(0), mask);
1824 mask = (qmask >> 32);
1825 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(1), mask);
1826 }
1827 /* skip the flush */
1828}
1829
9a799d71
AK
1830static irqreturn_t ixgbe_msix_clean_tx(int irq, void *data)
1831{
021230d4
AV
1832 struct ixgbe_q_vector *q_vector = data;
1833 struct ixgbe_adapter *adapter = q_vector->adapter;
3a581073 1834 struct ixgbe_ring *tx_ring;
021230d4
AV
1835 int i, r_idx;
1836
1837 if (!q_vector->txr_count)
1838 return IRQ_HANDLED;
1839
1840 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
1841 for (i = 0; i < q_vector->txr_count; i++) {
4a0b9ca0 1842 tx_ring = adapter->tx_ring[r_idx];
3a581073
JB
1843 tx_ring->total_bytes = 0;
1844 tx_ring->total_packets = 0;
021230d4 1845 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
e8e9f696 1846 r_idx + 1);
021230d4 1847 }
9a799d71 1848
9b471446 1849 /* EIAM disabled interrupts (on this vector) for us */
91281fd3
AD
1850 napi_schedule(&q_vector->napi);
1851
9a799d71
AK
1852 return IRQ_HANDLED;
1853}
1854
021230d4
AV
1855/**
1856 * ixgbe_msix_clean_rx - single unshared vector rx clean (all queues)
1857 * @irq: unused
1858 * @data: pointer to our q_vector struct for this interrupt vector
1859 **/
9a799d71
AK
1860static irqreturn_t ixgbe_msix_clean_rx(int irq, void *data)
1861{
021230d4
AV
1862 struct ixgbe_q_vector *q_vector = data;
1863 struct ixgbe_adapter *adapter = q_vector->adapter;
3a581073 1864 struct ixgbe_ring *rx_ring;
021230d4 1865 int r_idx;
30efa5a3 1866 int i;
021230d4 1867
33cf09c9
AD
1868#ifdef CONFIG_IXGBE_DCA
1869 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
1870 ixgbe_update_dca(q_vector);
1871#endif
1872
021230d4 1873 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
33cf09c9 1874 for (i = 0; i < q_vector->rxr_count; i++) {
4a0b9ca0 1875 rx_ring = adapter->rx_ring[r_idx];
30efa5a3
JB
1876 rx_ring->total_bytes = 0;
1877 rx_ring->total_packets = 0;
1878 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
e8e9f696 1879 r_idx + 1);
30efa5a3
JB
1880 }
1881
021230d4
AV
1882 if (!q_vector->rxr_count)
1883 return IRQ_HANDLED;
1884
9b471446 1885 /* EIAM disabled interrupts (on this vector) for us */
288379f0 1886 napi_schedule(&q_vector->napi);
021230d4
AV
1887
1888 return IRQ_HANDLED;
1889}
1890
1891static irqreturn_t ixgbe_msix_clean_many(int irq, void *data)
1892{
91281fd3
AD
1893 struct ixgbe_q_vector *q_vector = data;
1894 struct ixgbe_adapter *adapter = q_vector->adapter;
1895 struct ixgbe_ring *ring;
1896 int r_idx;
1897 int i;
1898
1899 if (!q_vector->txr_count && !q_vector->rxr_count)
1900 return IRQ_HANDLED;
1901
1902 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
1903 for (i = 0; i < q_vector->txr_count; i++) {
4a0b9ca0 1904 ring = adapter->tx_ring[r_idx];
91281fd3
AD
1905 ring->total_bytes = 0;
1906 ring->total_packets = 0;
1907 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
e8e9f696 1908 r_idx + 1);
91281fd3
AD
1909 }
1910
1911 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
1912 for (i = 0; i < q_vector->rxr_count; i++) {
4a0b9ca0 1913 ring = adapter->rx_ring[r_idx];
91281fd3
AD
1914 ring->total_bytes = 0;
1915 ring->total_packets = 0;
1916 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
e8e9f696 1917 r_idx + 1);
91281fd3
AD
1918 }
1919
9b471446 1920 /* EIAM disabled interrupts (on this vector) for us */
91281fd3 1921 napi_schedule(&q_vector->napi);
9a799d71 1922
9a799d71
AK
1923 return IRQ_HANDLED;
1924}
1925
021230d4
AV
1926/**
1927 * ixgbe_clean_rxonly - msix (aka one shot) rx clean routine
1928 * @napi: napi struct with our devices info in it
1929 * @budget: amount of work driver is allowed to do this pass, in packets
1930 *
f0848276
JB
1931 * This function is optimized for cleaning one queue only on a single
1932 * q_vector!!!
021230d4 1933 **/
9a799d71
AK
1934static int ixgbe_clean_rxonly(struct napi_struct *napi, int budget)
1935{
021230d4 1936 struct ixgbe_q_vector *q_vector =
e8e9f696 1937 container_of(napi, struct ixgbe_q_vector, napi);
021230d4 1938 struct ixgbe_adapter *adapter = q_vector->adapter;
f0848276 1939 struct ixgbe_ring *rx_ring = NULL;
9a799d71 1940 int work_done = 0;
021230d4 1941 long r_idx;
9a799d71 1942
5dd2d332 1943#ifdef CONFIG_IXGBE_DCA
bd0362dd 1944 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
33cf09c9 1945 ixgbe_update_dca(q_vector);
bd0362dd 1946#endif
9a799d71 1947
33cf09c9
AD
1948 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
1949 rx_ring = adapter->rx_ring[r_idx];
1950
78b6f4ce 1951 ixgbe_clean_rx_irq(q_vector, rx_ring, &work_done, budget);
9a799d71 1952
021230d4
AV
1953 /* If all Rx work done, exit the polling mode */
1954 if (work_done < budget) {
288379f0 1955 napi_complete(napi);
f7554a2b 1956 if (adapter->rx_itr_setting & 1)
f494e8fa 1957 ixgbe_set_itr_msix(q_vector);
9a799d71 1958 if (!test_bit(__IXGBE_DOWN, &adapter->state))
fe49f04a 1959 ixgbe_irq_enable_queues(adapter,
e8e9f696 1960 ((u64)1 << q_vector->v_idx));
9a799d71
AK
1961 }
1962
1963 return work_done;
1964}
1965
f0848276 1966/**
91281fd3 1967 * ixgbe_clean_rxtx_many - msix (aka one shot) rx clean routine
f0848276
JB
1968 * @napi: napi struct with our devices info in it
1969 * @budget: amount of work driver is allowed to do this pass, in packets
1970 *
1971 * This function will clean more than one rx queue associated with a
1972 * q_vector.
1973 **/
91281fd3 1974static int ixgbe_clean_rxtx_many(struct napi_struct *napi, int budget)
f0848276
JB
1975{
1976 struct ixgbe_q_vector *q_vector =
e8e9f696 1977 container_of(napi, struct ixgbe_q_vector, napi);
f0848276 1978 struct ixgbe_adapter *adapter = q_vector->adapter;
91281fd3 1979 struct ixgbe_ring *ring = NULL;
f0848276
JB
1980 int work_done = 0, i;
1981 long r_idx;
91281fd3
AD
1982 bool tx_clean_complete = true;
1983
33cf09c9
AD
1984#ifdef CONFIG_IXGBE_DCA
1985 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
1986 ixgbe_update_dca(q_vector);
1987#endif
1988
91281fd3
AD
1989 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
1990 for (i = 0; i < q_vector->txr_count; i++) {
4a0b9ca0 1991 ring = adapter->tx_ring[r_idx];
91281fd3
AD
1992 tx_clean_complete &= ixgbe_clean_tx_irq(q_vector, ring);
1993 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
e8e9f696 1994 r_idx + 1);
91281fd3 1995 }
f0848276
JB
1996
1997 /* attempt to distribute budget to each queue fairly, but don't allow
1998 * the budget to go below 1 because we'll exit polling */
1999 budget /= (q_vector->rxr_count ?: 1);
2000 budget = max(budget, 1);
2001 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
2002 for (i = 0; i < q_vector->rxr_count; i++) {
4a0b9ca0 2003 ring = adapter->rx_ring[r_idx];
91281fd3 2004 ixgbe_clean_rx_irq(q_vector, ring, &work_done, budget);
f0848276 2005 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
e8e9f696 2006 r_idx + 1);
f0848276
JB
2007 }
2008
2009 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
4a0b9ca0 2010 ring = adapter->rx_ring[r_idx];
f0848276 2011 /* If all Rx work done, exit the polling mode */
7f821875 2012 if (work_done < budget) {
288379f0 2013 napi_complete(napi);
f7554a2b 2014 if (adapter->rx_itr_setting & 1)
f0848276
JB
2015 ixgbe_set_itr_msix(q_vector);
2016 if (!test_bit(__IXGBE_DOWN, &adapter->state))
fe49f04a 2017 ixgbe_irq_enable_queues(adapter,
e8e9f696 2018 ((u64)1 << q_vector->v_idx));
f0848276
JB
2019 return 0;
2020 }
2021
2022 return work_done;
2023}
91281fd3
AD
2024
2025/**
2026 * ixgbe_clean_txonly - msix (aka one shot) tx clean routine
2027 * @napi: napi struct with our devices info in it
2028 * @budget: amount of work driver is allowed to do this pass, in packets
2029 *
2030 * This function is optimized for cleaning one queue only on a single
2031 * q_vector!!!
2032 **/
2033static int ixgbe_clean_txonly(struct napi_struct *napi, int budget)
2034{
2035 struct ixgbe_q_vector *q_vector =
e8e9f696 2036 container_of(napi, struct ixgbe_q_vector, napi);
91281fd3
AD
2037 struct ixgbe_adapter *adapter = q_vector->adapter;
2038 struct ixgbe_ring *tx_ring = NULL;
2039 int work_done = 0;
2040 long r_idx;
2041
91281fd3
AD
2042#ifdef CONFIG_IXGBE_DCA
2043 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
33cf09c9 2044 ixgbe_update_dca(q_vector);
91281fd3
AD
2045#endif
2046
33cf09c9
AD
2047 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
2048 tx_ring = adapter->tx_ring[r_idx];
2049
91281fd3
AD
2050 if (!ixgbe_clean_tx_irq(q_vector, tx_ring))
2051 work_done = budget;
2052
f7554a2b 2053 /* If all Tx work done, exit the polling mode */
91281fd3
AD
2054 if (work_done < budget) {
2055 napi_complete(napi);
f7554a2b 2056 if (adapter->tx_itr_setting & 1)
91281fd3
AD
2057 ixgbe_set_itr_msix(q_vector);
2058 if (!test_bit(__IXGBE_DOWN, &adapter->state))
e8e9f696
JP
2059 ixgbe_irq_enable_queues(adapter,
2060 ((u64)1 << q_vector->v_idx));
91281fd3
AD
2061 }
2062
2063 return work_done;
2064}
2065
021230d4 2066static inline void map_vector_to_rxq(struct ixgbe_adapter *a, int v_idx,
e8e9f696 2067 int r_idx)
021230d4 2068{
7a921c93
AD
2069 struct ixgbe_q_vector *q_vector = a->q_vector[v_idx];
2070
2071 set_bit(r_idx, q_vector->rxr_idx);
2072 q_vector->rxr_count++;
021230d4
AV
2073}
2074
2075static inline void map_vector_to_txq(struct ixgbe_adapter *a, int v_idx,
e8e9f696 2076 int t_idx)
021230d4 2077{
7a921c93
AD
2078 struct ixgbe_q_vector *q_vector = a->q_vector[v_idx];
2079
2080 set_bit(t_idx, q_vector->txr_idx);
2081 q_vector->txr_count++;
021230d4
AV
2082}
2083
9a799d71 2084/**
021230d4
AV
2085 * ixgbe_map_rings_to_vectors - Maps descriptor rings to vectors
2086 * @adapter: board private structure to initialize
2087 * @vectors: allotted vector count for descriptor rings
9a799d71 2088 *
021230d4
AV
2089 * This function maps descriptor rings to the queue-specific vectors
2090 * we were allotted through the MSI-X enabling code. Ideally, we'd have
2091 * one vector per ring/queue, but on a constrained vector budget, we
2092 * group the rings as "efficiently" as possible. You would add new
2093 * mapping configurations in here.
9a799d71 2094 **/
021230d4 2095static int ixgbe_map_rings_to_vectors(struct ixgbe_adapter *adapter,
e8e9f696 2096 int vectors)
021230d4
AV
2097{
2098 int v_start = 0;
2099 int rxr_idx = 0, txr_idx = 0;
2100 int rxr_remaining = adapter->num_rx_queues;
2101 int txr_remaining = adapter->num_tx_queues;
2102 int i, j;
2103 int rqpv, tqpv;
2104 int err = 0;
2105
2106 /* No mapping required if MSI-X is disabled. */
2107 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
2108 goto out;
9a799d71 2109
021230d4
AV
2110 /*
2111 * The ideal configuration...
2112 * We have enough vectors to map one per queue.
2113 */
2114 if (vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
2115 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
2116 map_vector_to_rxq(adapter, v_start, rxr_idx);
9a799d71 2117
021230d4
AV
2118 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
2119 map_vector_to_txq(adapter, v_start, txr_idx);
9a799d71 2120
9a799d71 2121 goto out;
021230d4 2122 }
9a799d71 2123
021230d4
AV
2124 /*
2125 * If we don't have enough vectors for a 1-to-1
2126 * mapping, we'll have to group them so there are
2127 * multiple queues per vector.
2128 */
2129 /* Re-adjusting *qpv takes care of the remainder. */
2130 for (i = v_start; i < vectors; i++) {
2131 rqpv = DIV_ROUND_UP(rxr_remaining, vectors - i);
2132 for (j = 0; j < rqpv; j++) {
2133 map_vector_to_rxq(adapter, i, rxr_idx);
2134 rxr_idx++;
2135 rxr_remaining--;
2136 }
2137 }
2138 for (i = v_start; i < vectors; i++) {
2139 tqpv = DIV_ROUND_UP(txr_remaining, vectors - i);
2140 for (j = 0; j < tqpv; j++) {
2141 map_vector_to_txq(adapter, i, txr_idx);
2142 txr_idx++;
2143 txr_remaining--;
9a799d71 2144 }
9a799d71
AK
2145 }
2146
021230d4
AV
2147out:
2148 return err;
2149}
2150
2151/**
2152 * ixgbe_request_msix_irqs - Initialize MSI-X interrupts
2153 * @adapter: board private structure
2154 *
2155 * ixgbe_request_msix_irqs allocates MSI-X vectors and requests
2156 * interrupts from the kernel.
2157 **/
2158static int ixgbe_request_msix_irqs(struct ixgbe_adapter *adapter)
2159{
2160 struct net_device *netdev = adapter->netdev;
2161 irqreturn_t (*handler)(int, void *);
2162 int i, vector, q_vectors, err;
e8e9f696 2163 int ri = 0, ti = 0;
021230d4
AV
2164
2165 /* Decrement for Other and TCP Timer vectors */
2166 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2167
2168 /* Map the Tx/Rx rings to the vectors we were allotted. */
2169 err = ixgbe_map_rings_to_vectors(adapter, q_vectors);
2170 if (err)
2171 goto out;
2172
2173#define SET_HANDLER(_v) ((!(_v)->rxr_count) ? &ixgbe_msix_clean_tx : \
e8e9f696
JP
2174 (!(_v)->txr_count) ? &ixgbe_msix_clean_rx : \
2175 &ixgbe_msix_clean_many)
021230d4 2176 for (vector = 0; vector < q_vectors; vector++) {
7a921c93 2177 handler = SET_HANDLER(adapter->q_vector[vector]);
cb13fc20 2178
e8e9f696 2179 if (handler == &ixgbe_msix_clean_rx) {
cb13fc20
RO
2180 sprintf(adapter->name[vector], "%s-%s-%d",
2181 netdev->name, "rx", ri++);
e8e9f696 2182 } else if (handler == &ixgbe_msix_clean_tx) {
cb13fc20
RO
2183 sprintf(adapter->name[vector], "%s-%s-%d",
2184 netdev->name, "tx", ti++);
32aa77a4 2185 } else {
cb13fc20 2186 sprintf(adapter->name[vector], "%s-%s-%d",
32aa77a4
AD
2187 netdev->name, "TxRx", ri++);
2188 ti++;
2189 }
cb13fc20 2190
021230d4 2191 err = request_irq(adapter->msix_entries[vector].vector,
e8e9f696
JP
2192 handler, 0, adapter->name[vector],
2193 adapter->q_vector[vector]);
9a799d71 2194 if (err) {
396e799c 2195 e_err(probe, "request_irq failed for MSIX interrupt "
849c4542 2196 "Error: %d\n", err);
021230d4 2197 goto free_queue_irqs;
9a799d71 2198 }
9a799d71
AK
2199 }
2200
021230d4
AV
2201 sprintf(adapter->name[vector], "%s:lsc", netdev->name);
2202 err = request_irq(adapter->msix_entries[vector].vector,
e8e9f696 2203 ixgbe_msix_lsc, 0, adapter->name[vector], netdev);
9a799d71 2204 if (err) {
396e799c 2205 e_err(probe, "request_irq for msix_lsc failed: %d\n", err);
021230d4 2206 goto free_queue_irqs;
9a799d71
AK
2207 }
2208
9a799d71
AK
2209 return 0;
2210
021230d4
AV
2211free_queue_irqs:
2212 for (i = vector - 1; i >= 0; i--)
2213 free_irq(adapter->msix_entries[--vector].vector,
e8e9f696 2214 adapter->q_vector[i]);
021230d4
AV
2215 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
2216 pci_disable_msix(adapter->pdev);
9a799d71
AK
2217 kfree(adapter->msix_entries);
2218 adapter->msix_entries = NULL;
021230d4 2219out:
9a799d71
AK
2220 return err;
2221}
2222
f494e8fa
AV
2223static void ixgbe_set_itr(struct ixgbe_adapter *adapter)
2224{
7a921c93 2225 struct ixgbe_q_vector *q_vector = adapter->q_vector[0];
f494e8fa
AV
2226 u8 current_itr;
2227 u32 new_itr = q_vector->eitr;
4a0b9ca0
PW
2228 struct ixgbe_ring *rx_ring = adapter->rx_ring[0];
2229 struct ixgbe_ring *tx_ring = adapter->tx_ring[0];
f494e8fa 2230
30efa5a3 2231 q_vector->tx_itr = ixgbe_update_itr(adapter, new_itr,
e8e9f696
JP
2232 q_vector->tx_itr,
2233 tx_ring->total_packets,
2234 tx_ring->total_bytes);
30efa5a3 2235 q_vector->rx_itr = ixgbe_update_itr(adapter, new_itr,
e8e9f696
JP
2236 q_vector->rx_itr,
2237 rx_ring->total_packets,
2238 rx_ring->total_bytes);
f494e8fa 2239
30efa5a3 2240 current_itr = max(q_vector->rx_itr, q_vector->tx_itr);
f494e8fa
AV
2241
2242 switch (current_itr) {
2243 /* counts and packets in update_itr are dependent on these numbers */
2244 case lowest_latency:
2245 new_itr = 100000;
2246 break;
2247 case low_latency:
2248 new_itr = 20000; /* aka hwitr = ~200 */
2249 break;
2250 case bulk_latency:
2251 new_itr = 8000;
2252 break;
2253 default:
2254 break;
2255 }
2256
2257 if (new_itr != q_vector->eitr) {
fe49f04a
AD
2258 /* do an exponential smoothing */
2259 new_itr = ((q_vector->eitr * 90)/100) + ((new_itr * 10)/100);
509ee935
JB
2260
2261 /* save the algorithm value here, not the smoothed one */
2262 q_vector->eitr = new_itr;
fe49f04a
AD
2263
2264 ixgbe_write_eitr(q_vector);
f494e8fa 2265 }
f494e8fa
AV
2266}
2267
79aefa45
AD
2268/**
2269 * ixgbe_irq_enable - Enable default interrupt generation settings
2270 * @adapter: board private structure
2271 **/
6af3b9eb
ET
2272static inline void ixgbe_irq_enable(struct ixgbe_adapter *adapter, bool queues,
2273 bool flush)
79aefa45
AD
2274{
2275 u32 mask;
835462fc
NS
2276
2277 mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
119fc60a
MC
2278 if (adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE)
2279 mask |= IXGBE_EIMS_GPI_SDP0;
6ab33d51
DM
2280 if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE)
2281 mask |= IXGBE_EIMS_GPI_SDP1;
e8e26350 2282 if (adapter->hw.mac.type == ixgbe_mac_82599EB) {
2a41ff81 2283 mask |= IXGBE_EIMS_ECC;
e8e26350
PW
2284 mask |= IXGBE_EIMS_GPI_SDP1;
2285 mask |= IXGBE_EIMS_GPI_SDP2;
1cdd1ec8
GR
2286 if (adapter->num_vfs)
2287 mask |= IXGBE_EIMS_MAILBOX;
e8e26350 2288 }
c4cf55e5
PWJ
2289 if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE ||
2290 adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE)
2291 mask |= IXGBE_EIMS_FLOW_DIR;
e8e26350 2292
79aefa45 2293 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, mask);
6af3b9eb
ET
2294 if (queues)
2295 ixgbe_irq_enable_queues(adapter, ~0);
2296 if (flush)
2297 IXGBE_WRITE_FLUSH(&adapter->hw);
1cdd1ec8
GR
2298
2299 if (adapter->num_vfs > 32) {
2300 u32 eitrsel = (1 << (adapter->num_vfs - 32)) - 1;
2301 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITRSEL, eitrsel);
2302 }
79aefa45 2303}
021230d4 2304
9a799d71 2305/**
021230d4 2306 * ixgbe_intr - legacy mode Interrupt Handler
9a799d71
AK
2307 * @irq: interrupt number
2308 * @data: pointer to a network interface device structure
9a799d71
AK
2309 **/
2310static irqreturn_t ixgbe_intr(int irq, void *data)
2311{
2312 struct net_device *netdev = data;
2313 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2314 struct ixgbe_hw *hw = &adapter->hw;
7a921c93 2315 struct ixgbe_q_vector *q_vector = adapter->q_vector[0];
9a799d71
AK
2316 u32 eicr;
2317
54037505 2318 /*
6af3b9eb 2319 * Workaround for silicon errata on 82598. Mask the interrupts
54037505
DS
2320 * before the read of EICR.
2321 */
2322 IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_IRQ_CLEAR_MASK);
2323
021230d4
AV
2324 /* for NAPI, using EIAM to auto-mask tx/rx interrupt bits on read
2325 * therefore no explict interrupt disable is necessary */
2326 eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
f47cf66e 2327 if (!eicr) {
6af3b9eb
ET
2328 /*
2329 * shared interrupt alert!
f47cf66e 2330 * make sure interrupts are enabled because the read will
6af3b9eb
ET
2331 * have disabled interrupts due to EIAM
2332 * finish the workaround of silicon errata on 82598. Unmask
2333 * the interrupt that we masked before the EICR read.
2334 */
2335 if (!test_bit(__IXGBE_DOWN, &adapter->state))
2336 ixgbe_irq_enable(adapter, true, true);
9a799d71 2337 return IRQ_NONE; /* Not our interrupt */
f47cf66e 2338 }
9a799d71 2339
cf8280ee
JB
2340 if (eicr & IXGBE_EICR_LSC)
2341 ixgbe_check_lsc(adapter);
021230d4 2342
e8e26350
PW
2343 if (hw->mac.type == ixgbe_mac_82599EB)
2344 ixgbe_check_sfp_event(adapter, eicr);
2345
0befdb3e 2346 ixgbe_check_fan_failure(adapter, eicr);
119fc60a
MC
2347 if ((adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE) &&
2348 ((eicr & IXGBE_EICR_GPI_SDP0) || (eicr & IXGBE_EICR_LSC)))
2349 schedule_work(&adapter->check_overtemp_task);
0befdb3e 2350
7a921c93 2351 if (napi_schedule_prep(&(q_vector->napi))) {
4a0b9ca0
PW
2352 adapter->tx_ring[0]->total_packets = 0;
2353 adapter->tx_ring[0]->total_bytes = 0;
2354 adapter->rx_ring[0]->total_packets = 0;
2355 adapter->rx_ring[0]->total_bytes = 0;
021230d4 2356 /* would disable interrupts here but EIAM disabled it */
7a921c93 2357 __napi_schedule(&(q_vector->napi));
9a799d71
AK
2358 }
2359
6af3b9eb
ET
2360 /*
2361 * re-enable link(maybe) and non-queue interrupts, no flush.
2362 * ixgbe_poll will re-enable the queue interrupts
2363 */
2364
2365 if (!test_bit(__IXGBE_DOWN, &adapter->state))
2366 ixgbe_irq_enable(adapter, false, false);
2367
9a799d71
AK
2368 return IRQ_HANDLED;
2369}
2370
021230d4
AV
2371static inline void ixgbe_reset_q_vectors(struct ixgbe_adapter *adapter)
2372{
2373 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2374
2375 for (i = 0; i < q_vectors; i++) {
7a921c93 2376 struct ixgbe_q_vector *q_vector = adapter->q_vector[i];
021230d4
AV
2377 bitmap_zero(q_vector->rxr_idx, MAX_RX_QUEUES);
2378 bitmap_zero(q_vector->txr_idx, MAX_TX_QUEUES);
2379 q_vector->rxr_count = 0;
2380 q_vector->txr_count = 0;
2381 }
2382}
2383
9a799d71
AK
2384/**
2385 * ixgbe_request_irq - initialize interrupts
2386 * @adapter: board private structure
2387 *
2388 * Attempts to configure interrupts using the best available
2389 * capabilities of the hardware and kernel.
2390 **/
021230d4 2391static int ixgbe_request_irq(struct ixgbe_adapter *adapter)
9a799d71
AK
2392{
2393 struct net_device *netdev = adapter->netdev;
021230d4 2394 int err;
9a799d71 2395
021230d4
AV
2396 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
2397 err = ixgbe_request_msix_irqs(adapter);
2398 } else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED) {
a0607fd3 2399 err = request_irq(adapter->pdev->irq, ixgbe_intr, 0,
e8e9f696 2400 netdev->name, netdev);
021230d4 2401 } else {
a0607fd3 2402 err = request_irq(adapter->pdev->irq, ixgbe_intr, IRQF_SHARED,
e8e9f696 2403 netdev->name, netdev);
9a799d71
AK
2404 }
2405
9a799d71 2406 if (err)
396e799c 2407 e_err(probe, "request_irq failed, Error %d\n", err);
9a799d71 2408
9a799d71
AK
2409 return err;
2410}
2411
2412static void ixgbe_free_irq(struct ixgbe_adapter *adapter)
2413{
2414 struct net_device *netdev = adapter->netdev;
2415
2416 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
021230d4 2417 int i, q_vectors;
9a799d71 2418
021230d4
AV
2419 q_vectors = adapter->num_msix_vectors;
2420
2421 i = q_vectors - 1;
9a799d71 2422 free_irq(adapter->msix_entries[i].vector, netdev);
9a799d71 2423
021230d4
AV
2424 i--;
2425 for (; i >= 0; i--) {
2426 free_irq(adapter->msix_entries[i].vector,
e8e9f696 2427 adapter->q_vector[i]);
021230d4
AV
2428 }
2429
2430 ixgbe_reset_q_vectors(adapter);
2431 } else {
2432 free_irq(adapter->pdev->irq, netdev);
9a799d71
AK
2433 }
2434}
2435
22d5a71b
JB
2436/**
2437 * ixgbe_irq_disable - Mask off interrupt generation on the NIC
2438 * @adapter: board private structure
2439 **/
2440static inline void ixgbe_irq_disable(struct ixgbe_adapter *adapter)
2441{
835462fc
NS
2442 if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
2443 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~0);
2444 } else {
2445 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, 0xFFFF0000);
2446 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(0), ~0);
22d5a71b 2447 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(1), ~0);
1cdd1ec8
GR
2448 if (adapter->num_vfs > 32)
2449 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITRSEL, 0);
22d5a71b
JB
2450 }
2451 IXGBE_WRITE_FLUSH(&adapter->hw);
2452 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
2453 int i;
2454 for (i = 0; i < adapter->num_msix_vectors; i++)
2455 synchronize_irq(adapter->msix_entries[i].vector);
2456 } else {
2457 synchronize_irq(adapter->pdev->irq);
2458 }
2459}
2460
9a799d71
AK
2461/**
2462 * ixgbe_configure_msi_and_legacy - Initialize PIN (INTA...) and MSI interrupts
2463 *
2464 **/
2465static void ixgbe_configure_msi_and_legacy(struct ixgbe_adapter *adapter)
2466{
9a799d71
AK
2467 struct ixgbe_hw *hw = &adapter->hw;
2468
021230d4 2469 IXGBE_WRITE_REG(hw, IXGBE_EITR(0),
e8e9f696 2470 EITR_INTS_PER_SEC_TO_REG(adapter->rx_eitr_param));
9a799d71 2471
e8e26350
PW
2472 ixgbe_set_ivar(adapter, 0, 0, 0);
2473 ixgbe_set_ivar(adapter, 1, 0, 0);
021230d4
AV
2474
2475 map_vector_to_rxq(adapter, 0, 0);
2476 map_vector_to_txq(adapter, 0, 0);
2477
396e799c 2478 e_info(hw, "Legacy interrupt IVAR setup done\n");
9a799d71
AK
2479}
2480
43e69bf0
AD
2481/**
2482 * ixgbe_configure_tx_ring - Configure 8259x Tx ring after Reset
2483 * @adapter: board private structure
2484 * @ring: structure containing ring specific data
2485 *
2486 * Configure the Tx descriptor ring after a reset.
2487 **/
84418e3b
AD
2488void ixgbe_configure_tx_ring(struct ixgbe_adapter *adapter,
2489 struct ixgbe_ring *ring)
43e69bf0
AD
2490{
2491 struct ixgbe_hw *hw = &adapter->hw;
2492 u64 tdba = ring->dma;
2f1860b8
AD
2493 int wait_loop = 10;
2494 u32 txdctl;
43e69bf0
AD
2495 u16 reg_idx = ring->reg_idx;
2496
2f1860b8
AD
2497 /* disable queue to avoid issues while updating state */
2498 txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(reg_idx));
2499 IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(reg_idx),
2500 txdctl & ~IXGBE_TXDCTL_ENABLE);
2501 IXGBE_WRITE_FLUSH(hw);
2502
43e69bf0 2503 IXGBE_WRITE_REG(hw, IXGBE_TDBAL(reg_idx),
e8e9f696 2504 (tdba & DMA_BIT_MASK(32)));
43e69bf0
AD
2505 IXGBE_WRITE_REG(hw, IXGBE_TDBAH(reg_idx), (tdba >> 32));
2506 IXGBE_WRITE_REG(hw, IXGBE_TDLEN(reg_idx),
2507 ring->count * sizeof(union ixgbe_adv_tx_desc));
2508 IXGBE_WRITE_REG(hw, IXGBE_TDH(reg_idx), 0);
2509 IXGBE_WRITE_REG(hw, IXGBE_TDT(reg_idx), 0);
84ea2591 2510 ring->tail = hw->hw_addr + IXGBE_TDT(reg_idx);
43e69bf0 2511
2f1860b8
AD
2512 /* configure fetching thresholds */
2513 if (adapter->rx_itr_setting == 0) {
2514 /* cannot set wthresh when itr==0 */
2515 txdctl &= ~0x007F0000;
2516 } else {
2517 /* enable WTHRESH=8 descriptors, to encourage burst writeback */
2518 txdctl |= (8 << 16);
2519 }
2520 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
2521 /* PThresh workaround for Tx hang with DFP enabled. */
2522 txdctl |= 32;
2523 }
2524
2525 /* reinitialize flowdirector state */
7d637bcc 2526 set_bit(__IXGBE_TX_FDIR_INIT_DONE, &ring->state);
2f1860b8
AD
2527
2528 /* enable queue */
2529 txdctl |= IXGBE_TXDCTL_ENABLE;
2530 IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(reg_idx), txdctl);
2531
2532 /* TXDCTL.EN will return 0 on 82598 if link is down, so skip it */
2533 if (hw->mac.type == ixgbe_mac_82598EB &&
2534 !(IXGBE_READ_REG(hw, IXGBE_LINKS) & IXGBE_LINKS_UP))
2535 return;
2536
2537 /* poll to verify queue is enabled */
2538 do {
2539 msleep(1);
2540 txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(reg_idx));
2541 } while (--wait_loop && !(txdctl & IXGBE_TXDCTL_ENABLE));
2542 if (!wait_loop)
2543 e_err(drv, "Could not enable Tx Queue %d\n", reg_idx);
43e69bf0
AD
2544}
2545
120ff942
AD
2546static void ixgbe_setup_mtqc(struct ixgbe_adapter *adapter)
2547{
2548 struct ixgbe_hw *hw = &adapter->hw;
2549 u32 rttdcs;
2550 u32 mask;
2551
2552 if (hw->mac.type == ixgbe_mac_82598EB)
2553 return;
2554
2555 /* disable the arbiter while setting MTQC */
2556 rttdcs = IXGBE_READ_REG(hw, IXGBE_RTTDCS);
2557 rttdcs |= IXGBE_RTTDCS_ARBDIS;
2558 IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
2559
2560 /* set transmit pool layout */
2561 mask = (IXGBE_FLAG_SRIOV_ENABLED | IXGBE_FLAG_DCB_ENABLED);
2562 switch (adapter->flags & mask) {
2563
2564 case (IXGBE_FLAG_SRIOV_ENABLED):
2565 IXGBE_WRITE_REG(hw, IXGBE_MTQC,
2566 (IXGBE_MTQC_VT_ENA | IXGBE_MTQC_64VF));
2567 break;
2568
2569 case (IXGBE_FLAG_DCB_ENABLED):
2570 /* We enable 8 traffic classes, DCB only */
2571 IXGBE_WRITE_REG(hw, IXGBE_MTQC,
2572 (IXGBE_MTQC_RT_ENA | IXGBE_MTQC_8TC_8TQ));
2573 break;
2574
2575 default:
2576 IXGBE_WRITE_REG(hw, IXGBE_MTQC, IXGBE_MTQC_64Q_1PB);
2577 break;
2578 }
2579
2580 /* re-enable the arbiter */
2581 rttdcs &= ~IXGBE_RTTDCS_ARBDIS;
2582 IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
2583}
2584
9a799d71 2585/**
3a581073 2586 * ixgbe_configure_tx - Configure 8259x Transmit Unit after Reset
9a799d71
AK
2587 * @adapter: board private structure
2588 *
2589 * Configure the Tx unit of the MAC after a reset.
2590 **/
2591static void ixgbe_configure_tx(struct ixgbe_adapter *adapter)
2592{
2f1860b8
AD
2593 struct ixgbe_hw *hw = &adapter->hw;
2594 u32 dmatxctl;
43e69bf0 2595 u32 i;
9a799d71 2596
2f1860b8
AD
2597 ixgbe_setup_mtqc(adapter);
2598
2599 if (hw->mac.type != ixgbe_mac_82598EB) {
2600 /* DMATXCTL.EN must be before Tx queues are enabled */
2601 dmatxctl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
2602 dmatxctl |= IXGBE_DMATXCTL_TE;
2603 IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, dmatxctl);
2604 }
2605
9a799d71 2606 /* Setup the HW Tx Head and Tail descriptor pointers */
43e69bf0
AD
2607 for (i = 0; i < adapter->num_tx_queues; i++)
2608 ixgbe_configure_tx_ring(adapter, adapter->tx_ring[i]);
9a799d71
AK
2609}
2610
e8e26350 2611#define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
cc41ac7c 2612
a6616b42 2613static void ixgbe_configure_srrctl(struct ixgbe_adapter *adapter,
e8e9f696 2614 struct ixgbe_ring *rx_ring)
cc41ac7c 2615{
cc41ac7c 2616 u32 srrctl;
a6616b42 2617 int index;
0cefafad 2618 struct ixgbe_ring_feature *feature = adapter->ring_feature;
3be1adfb 2619
a6616b42
YZ
2620 index = rx_ring->reg_idx;
2621 if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
2622 unsigned long mask;
0cefafad 2623 mask = (unsigned long) feature[RING_F_RSS].mask;
3be1adfb 2624 index = index & mask;
cc41ac7c 2625 }
cc41ac7c
JB
2626 srrctl = IXGBE_READ_REG(&adapter->hw, IXGBE_SRRCTL(index));
2627
2628 srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
2629 srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
9e10e045
AD
2630 if (adapter->num_vfs)
2631 srrctl |= IXGBE_SRRCTL_DROP_EN;
cc41ac7c 2632
afafd5b0
AD
2633 srrctl |= (IXGBE_RX_HDR_SIZE << IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT) &
2634 IXGBE_SRRCTL_BSIZEHDR_MASK;
2635
7d637bcc 2636 if (ring_is_ps_enabled(rx_ring)) {
afafd5b0
AD
2637#if (PAGE_SIZE / 2) > IXGBE_MAX_RXBUFFER
2638 srrctl |= IXGBE_MAX_RXBUFFER >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
2639#else
2640 srrctl |= (PAGE_SIZE / 2) >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
2641#endif
cc41ac7c 2642 srrctl |= IXGBE_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
cc41ac7c 2643 } else {
afafd5b0
AD
2644 srrctl |= ALIGN(rx_ring->rx_buf_len, 1024) >>
2645 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
cc41ac7c 2646 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
cc41ac7c 2647 }
e8e26350 2648
cc41ac7c
JB
2649 IXGBE_WRITE_REG(&adapter->hw, IXGBE_SRRCTL(index), srrctl);
2650}
9a799d71 2651
05abb126 2652static void ixgbe_setup_mrqc(struct ixgbe_adapter *adapter)
0cefafad 2653{
05abb126
AD
2654 struct ixgbe_hw *hw = &adapter->hw;
2655 static const u32 seed[10] = { 0xE291D73D, 0x1805EC6C, 0x2A94B30D,
e8e9f696
JP
2656 0xA54F2BEC, 0xEA49AF7C, 0xE214AD3D, 0xB855AABE,
2657 0x6A3E67EA, 0x14364D17, 0x3BED200D};
05abb126
AD
2658 u32 mrqc = 0, reta = 0;
2659 u32 rxcsum;
2660 int i, j;
0cefafad
JB
2661 int mask;
2662
05abb126
AD
2663 /* Fill out hash function seeds */
2664 for (i = 0; i < 10; i++)
2665 IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), seed[i]);
2666
2667 /* Fill out redirection table */
2668 for (i = 0, j = 0; i < 128; i++, j++) {
2669 if (j == adapter->ring_feature[RING_F_RSS].indices)
2670 j = 0;
2671 /* reta = 4-byte sliding window of
2672 * 0x00..(indices-1)(indices-1)00..etc. */
2673 reta = (reta << 8) | (j * 0x11);
2674 if ((i & 3) == 3)
2675 IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
2676 }
0cefafad 2677
05abb126
AD
2678 /* Disable indicating checksum in descriptor, enables RSS hash */
2679 rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
2680 rxcsum |= IXGBE_RXCSUM_PCSD;
2681 IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
2682
2683 if (adapter->hw.mac.type == ixgbe_mac_82598EB)
2684 mask = adapter->flags & IXGBE_FLAG_RSS_ENABLED;
2685 else
2686 mask = adapter->flags & (IXGBE_FLAG_RSS_ENABLED
0cefafad 2687#ifdef CONFIG_IXGBE_DCB
05abb126 2688 | IXGBE_FLAG_DCB_ENABLED
0cefafad 2689#endif
05abb126
AD
2690 | IXGBE_FLAG_SRIOV_ENABLED
2691 );
0cefafad
JB
2692
2693 switch (mask) {
2694 case (IXGBE_FLAG_RSS_ENABLED):
2695 mrqc = IXGBE_MRQC_RSSEN;
2696 break;
1cdd1ec8
GR
2697 case (IXGBE_FLAG_SRIOV_ENABLED):
2698 mrqc = IXGBE_MRQC_VMDQEN;
2699 break;
0cefafad
JB
2700#ifdef CONFIG_IXGBE_DCB
2701 case (IXGBE_FLAG_DCB_ENABLED):
2702 mrqc = IXGBE_MRQC_RT8TCEN;
2703 break;
2704#endif /* CONFIG_IXGBE_DCB */
2705 default:
2706 break;
2707 }
2708
05abb126
AD
2709 /* Perform hash on these packet types */
2710 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4
2711 | IXGBE_MRQC_RSS_FIELD_IPV4_TCP
2712 | IXGBE_MRQC_RSS_FIELD_IPV6
2713 | IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
2714
2715 IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
0cefafad
JB
2716}
2717
bb5a9ad2
NS
2718/**
2719 * ixgbe_configure_rscctl - enable RSC for the indicated ring
2720 * @adapter: address of board private structure
2721 * @index: index of ring to set
bb5a9ad2 2722 **/
7367096a
AD
2723static void ixgbe_configure_rscctl(struct ixgbe_adapter *adapter,
2724 struct ixgbe_ring *ring)
bb5a9ad2 2725{
bb5a9ad2 2726 struct ixgbe_hw *hw = &adapter->hw;
bb5a9ad2 2727 u32 rscctrl;
edd2ea55 2728 int rx_buf_len;
7367096a
AD
2729 u16 reg_idx = ring->reg_idx;
2730
7d637bcc 2731 if (!ring_is_rsc_enabled(ring))
7367096a 2732 return;
bb5a9ad2 2733
7367096a
AD
2734 rx_buf_len = ring->rx_buf_len;
2735 rscctrl = IXGBE_READ_REG(hw, IXGBE_RSCCTL(reg_idx));
bb5a9ad2
NS
2736 rscctrl |= IXGBE_RSCCTL_RSCEN;
2737 /*
2738 * we must limit the number of descriptors so that the
2739 * total size of max desc * buf_len is not greater
2740 * than 65535
2741 */
7d637bcc 2742 if (ring_is_ps_enabled(ring)) {
bb5a9ad2
NS
2743#if (MAX_SKB_FRAGS > 16)
2744 rscctrl |= IXGBE_RSCCTL_MAXDESC_16;
2745#elif (MAX_SKB_FRAGS > 8)
2746 rscctrl |= IXGBE_RSCCTL_MAXDESC_8;
2747#elif (MAX_SKB_FRAGS > 4)
2748 rscctrl |= IXGBE_RSCCTL_MAXDESC_4;
2749#else
2750 rscctrl |= IXGBE_RSCCTL_MAXDESC_1;
2751#endif
2752 } else {
2753 if (rx_buf_len < IXGBE_RXBUFFER_4096)
2754 rscctrl |= IXGBE_RSCCTL_MAXDESC_16;
2755 else if (rx_buf_len < IXGBE_RXBUFFER_8192)
2756 rscctrl |= IXGBE_RSCCTL_MAXDESC_8;
2757 else
2758 rscctrl |= IXGBE_RSCCTL_MAXDESC_4;
2759 }
7367096a 2760 IXGBE_WRITE_REG(hw, IXGBE_RSCCTL(reg_idx), rscctrl);
bb5a9ad2
NS
2761}
2762
9e10e045
AD
2763/**
2764 * ixgbe_set_uta - Set unicast filter table address
2765 * @adapter: board private structure
2766 *
2767 * The unicast table address is a register array of 32-bit registers.
2768 * The table is meant to be used in a way similar to how the MTA is used
2769 * however due to certain limitations in the hardware it is necessary to
2770 * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscuous
2771 * enable bit to allow vlan tag stripping when promiscuous mode is enabled
2772 **/
2773static void ixgbe_set_uta(struct ixgbe_adapter *adapter)
2774{
2775 struct ixgbe_hw *hw = &adapter->hw;
2776 int i;
2777
2778 /* The UTA table only exists on 82599 hardware and newer */
2779 if (hw->mac.type < ixgbe_mac_82599EB)
2780 return;
2781
2782 /* we only need to do this if VMDq is enabled */
2783 if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
2784 return;
2785
2786 for (i = 0; i < 128; i++)
2787 IXGBE_WRITE_REG(hw, IXGBE_UTA(i), ~0);
2788}
2789
2790#define IXGBE_MAX_RX_DESC_POLL 10
2791static void ixgbe_rx_desc_queue_enable(struct ixgbe_adapter *adapter,
2792 struct ixgbe_ring *ring)
2793{
2794 struct ixgbe_hw *hw = &adapter->hw;
2795 int reg_idx = ring->reg_idx;
2796 int wait_loop = IXGBE_MAX_RX_DESC_POLL;
2797 u32 rxdctl;
2798
2799 /* RXDCTL.EN will return 0 on 82598 if link is down, so skip it */
2800 if (hw->mac.type == ixgbe_mac_82598EB &&
2801 !(IXGBE_READ_REG(hw, IXGBE_LINKS) & IXGBE_LINKS_UP))
2802 return;
2803
2804 do {
2805 msleep(1);
2806 rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(reg_idx));
2807 } while (--wait_loop && !(rxdctl & IXGBE_RXDCTL_ENABLE));
2808
2809 if (!wait_loop) {
2810 e_err(drv, "RXDCTL.ENABLE on Rx queue %d not set within "
2811 "the polling period\n", reg_idx);
2812 }
2813}
2814
84418e3b
AD
2815void ixgbe_configure_rx_ring(struct ixgbe_adapter *adapter,
2816 struct ixgbe_ring *ring)
acd37177
AD
2817{
2818 struct ixgbe_hw *hw = &adapter->hw;
2819 u64 rdba = ring->dma;
9e10e045 2820 u32 rxdctl;
acd37177
AD
2821 u16 reg_idx = ring->reg_idx;
2822
9e10e045
AD
2823 /* disable queue to avoid issues while updating state */
2824 rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(reg_idx));
2825 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(reg_idx),
2826 rxdctl & ~IXGBE_RXDCTL_ENABLE);
2827 IXGBE_WRITE_FLUSH(hw);
2828
acd37177
AD
2829 IXGBE_WRITE_REG(hw, IXGBE_RDBAL(reg_idx), (rdba & DMA_BIT_MASK(32)));
2830 IXGBE_WRITE_REG(hw, IXGBE_RDBAH(reg_idx), (rdba >> 32));
2831 IXGBE_WRITE_REG(hw, IXGBE_RDLEN(reg_idx),
2832 ring->count * sizeof(union ixgbe_adv_rx_desc));
2833 IXGBE_WRITE_REG(hw, IXGBE_RDH(reg_idx), 0);
2834 IXGBE_WRITE_REG(hw, IXGBE_RDT(reg_idx), 0);
84ea2591 2835 ring->tail = hw->hw_addr + IXGBE_RDT(reg_idx);
9e10e045
AD
2836
2837 ixgbe_configure_srrctl(adapter, ring);
2838 ixgbe_configure_rscctl(adapter, ring);
2839
2840 if (hw->mac.type == ixgbe_mac_82598EB) {
2841 /*
2842 * enable cache line friendly hardware writes:
2843 * PTHRESH=32 descriptors (half the internal cache),
2844 * this also removes ugly rx_no_buffer_count increment
2845 * HTHRESH=4 descriptors (to minimize latency on fetch)
2846 * WTHRESH=8 burst writeback up to two cache lines
2847 */
2848 rxdctl &= ~0x3FFFFF;
2849 rxdctl |= 0x080420;
2850 }
2851
2852 /* enable receive descriptor ring */
2853 rxdctl |= IXGBE_RXDCTL_ENABLE;
2854 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(reg_idx), rxdctl);
2855
2856 ixgbe_rx_desc_queue_enable(adapter, ring);
fc77dc3c 2857 ixgbe_alloc_rx_buffers(ring, IXGBE_DESC_UNUSED(ring));
acd37177
AD
2858}
2859
48654521
AD
2860static void ixgbe_setup_psrtype(struct ixgbe_adapter *adapter)
2861{
2862 struct ixgbe_hw *hw = &adapter->hw;
2863 int p;
2864
2865 /* PSRTYPE must be initialized in non 82598 adapters */
2866 u32 psrtype = IXGBE_PSRTYPE_TCPHDR |
e8e9f696
JP
2867 IXGBE_PSRTYPE_UDPHDR |
2868 IXGBE_PSRTYPE_IPV4HDR |
48654521 2869 IXGBE_PSRTYPE_L2HDR |
e8e9f696 2870 IXGBE_PSRTYPE_IPV6HDR;
48654521
AD
2871
2872 if (hw->mac.type == ixgbe_mac_82598EB)
2873 return;
2874
2875 if (adapter->flags & IXGBE_FLAG_RSS_ENABLED)
2876 psrtype |= (adapter->num_rx_queues_per_pool << 29);
2877
2878 for (p = 0; p < adapter->num_rx_pools; p++)
2879 IXGBE_WRITE_REG(hw, IXGBE_PSRTYPE(adapter->num_vfs + p),
2880 psrtype);
2881}
2882
f5b4a52e
AD
2883static void ixgbe_configure_virtualization(struct ixgbe_adapter *adapter)
2884{
2885 struct ixgbe_hw *hw = &adapter->hw;
2886 u32 gcr_ext;
2887 u32 vt_reg_bits;
2888 u32 reg_offset, vf_shift;
2889 u32 vmdctl;
2890
2891 if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
2892 return;
2893
2894 vmdctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
2895 vt_reg_bits = IXGBE_VMD_CTL_VMDQ_EN | IXGBE_VT_CTL_REPLEN;
2896 vt_reg_bits |= (adapter->num_vfs << IXGBE_VT_CTL_POOL_SHIFT);
2897 IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vmdctl | vt_reg_bits);
2898
2899 vf_shift = adapter->num_vfs % 32;
2900 reg_offset = (adapter->num_vfs > 32) ? 1 : 0;
2901
2902 /* Enable only the PF's pool for Tx/Rx */
2903 IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), (1 << vf_shift));
2904 IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset ^ 1), 0);
2905 IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), (1 << vf_shift));
2906 IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset ^ 1), 0);
2907 IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
2908
2909 /* Map PF MAC address in RAR Entry 0 to first pool following VFs */
2910 hw->mac.ops.set_vmdq(hw, 0, adapter->num_vfs);
2911
2912 /*
2913 * Set up VF register offsets for selected VT Mode,
2914 * i.e. 32 or 64 VFs for SR-IOV
2915 */
2916 gcr_ext = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
2917 gcr_ext |= IXGBE_GCR_EXT_MSIX_EN;
2918 gcr_ext |= IXGBE_GCR_EXT_VT_MODE_64;
2919 IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr_ext);
2920
2921 /* enable Tx loopback for VF/PF communication */
2922 IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
2923}
2924
477de6ed 2925static void ixgbe_set_rx_buffer_len(struct ixgbe_adapter *adapter)
9a799d71 2926{
9a799d71
AK
2927 struct ixgbe_hw *hw = &adapter->hw;
2928 struct net_device *netdev = adapter->netdev;
2929 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
7c6e0a43 2930 int rx_buf_len;
477de6ed
AD
2931 struct ixgbe_ring *rx_ring;
2932 int i;
2933 u32 mhadd, hlreg0;
48654521 2934
9a799d71 2935 /* Decide whether to use packet split mode or not */
1cdd1ec8
GR
2936 /* Do not use packet split if we're in SR-IOV Mode */
2937 if (!adapter->num_vfs)
2938 adapter->flags |= IXGBE_FLAG_RX_PS_ENABLED;
9a799d71
AK
2939
2940 /* Set the RX buffer length according to the mode */
2941 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
7c6e0a43 2942 rx_buf_len = IXGBE_RX_HDR_SIZE;
9a799d71 2943 } else {
0c19d6af 2944 if (!(adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED) &&
f8212f97 2945 (netdev->mtu <= ETH_DATA_LEN))
7c6e0a43 2946 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
9a799d71 2947 else
477de6ed 2948 rx_buf_len = ALIGN(max_frame + VLAN_HLEN, 1024);
9a799d71
AK
2949 }
2950
63f39bd1 2951#ifdef IXGBE_FCOE
477de6ed
AD
2952 /* adjust max frame to be able to do baby jumbo for FCoE */
2953 if ((adapter->flags & IXGBE_FLAG_FCOE_ENABLED) &&
2954 (max_frame < IXGBE_FCOE_JUMBO_FRAME_SIZE))
2955 max_frame = IXGBE_FCOE_JUMBO_FRAME_SIZE;
9a799d71 2956
477de6ed
AD
2957#endif /* IXGBE_FCOE */
2958 mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
2959 if (max_frame != (mhadd >> IXGBE_MHADD_MFS_SHIFT)) {
2960 mhadd &= ~IXGBE_MHADD_MFS_MASK;
2961 mhadd |= max_frame << IXGBE_MHADD_MFS_SHIFT;
2962
2963 IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
2964 }
2965
2966 hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
2967 /* set jumbo enable since MHADD.MFS is keeping size locked at max_frame */
2968 hlreg0 |= IXGBE_HLREG0_JUMBOEN;
2969 IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
9a799d71 2970
0cefafad
JB
2971 /*
2972 * Setup the HW Rx Head and Tail Descriptor Pointers and
2973 * the Base and Length of the Rx Descriptor Ring
2974 */
9a799d71 2975 for (i = 0; i < adapter->num_rx_queues; i++) {
4a0b9ca0 2976 rx_ring = adapter->rx_ring[i];
a6616b42 2977 rx_ring->rx_buf_len = rx_buf_len;
cc41ac7c 2978
6e455b89 2979 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED)
7d637bcc
AD
2980 set_ring_ps_enabled(rx_ring);
2981 else
2982 clear_ring_ps_enabled(rx_ring);
2983
2984 if (adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED)
2985 set_ring_rsc_enabled(rx_ring);
1b3ff02e 2986 else
7d637bcc 2987 clear_ring_rsc_enabled(rx_ring);
cc41ac7c 2988
63f39bd1 2989#ifdef IXGBE_FCOE
e8e9f696 2990 if (netdev->features & NETIF_F_FCOE_MTU) {
63f39bd1
YZ
2991 struct ixgbe_ring_feature *f;
2992 f = &adapter->ring_feature[RING_F_FCOE];
6e455b89 2993 if ((i >= f->mask) && (i < f->mask + f->indices)) {
7d637bcc 2994 clear_ring_ps_enabled(rx_ring);
6e455b89
YZ
2995 if (rx_buf_len < IXGBE_FCOE_JUMBO_FRAME_SIZE)
2996 rx_ring->rx_buf_len =
e8e9f696 2997 IXGBE_FCOE_JUMBO_FRAME_SIZE;
7d637bcc
AD
2998 } else if (!ring_is_rsc_enabled(rx_ring) &&
2999 !ring_is_ps_enabled(rx_ring)) {
3000 rx_ring->rx_buf_len =
3001 IXGBE_FCOE_JUMBO_FRAME_SIZE;
6e455b89 3002 }
63f39bd1 3003 }
63f39bd1 3004#endif /* IXGBE_FCOE */
477de6ed
AD
3005 }
3006
3007}
3008
7367096a
AD
3009static void ixgbe_setup_rdrxctl(struct ixgbe_adapter *adapter)
3010{
3011 struct ixgbe_hw *hw = &adapter->hw;
3012 u32 rdrxctl = IXGBE_READ_REG(hw, IXGBE_RDRXCTL);
3013
3014 switch (hw->mac.type) {
3015 case ixgbe_mac_82598EB:
3016 /*
3017 * For VMDq support of different descriptor types or
3018 * buffer sizes through the use of multiple SRRCTL
3019 * registers, RDRXCTL.MVMEN must be set to 1
3020 *
3021 * also, the manual doesn't mention it clearly but DCA hints
3022 * will only use queue 0's tags unless this bit is set. Side
3023 * effects of setting this bit are only that SRRCTL must be
3024 * fully programmed [0..15]
3025 */
3026 rdrxctl |= IXGBE_RDRXCTL_MVMEN;
3027 break;
3028 case ixgbe_mac_82599EB:
3029 /* Disable RSC for ACK packets */
3030 IXGBE_WRITE_REG(hw, IXGBE_RSCDBU,
3031 (IXGBE_RSCDBU_RSCACKDIS | IXGBE_READ_REG(hw, IXGBE_RSCDBU)));
3032 rdrxctl &= ~IXGBE_RDRXCTL_RSCFRSTSIZE;
3033 /* hardware requires some bits to be set by default */
3034 rdrxctl |= (IXGBE_RDRXCTL_RSCACKC | IXGBE_RDRXCTL_FCOE_WRFIX);
3035 rdrxctl |= IXGBE_RDRXCTL_CRCSTRIP;
3036 break;
3037 default:
3038 /* We should do nothing since we don't know this hardware */
3039 return;
3040 }
3041
3042 IXGBE_WRITE_REG(hw, IXGBE_RDRXCTL, rdrxctl);
3043}
3044
477de6ed
AD
3045/**
3046 * ixgbe_configure_rx - Configure 8259x Receive Unit after Reset
3047 * @adapter: board private structure
3048 *
3049 * Configure the Rx unit of the MAC after a reset.
3050 **/
3051static void ixgbe_configure_rx(struct ixgbe_adapter *adapter)
3052{
3053 struct ixgbe_hw *hw = &adapter->hw;
477de6ed
AD
3054 int i;
3055 u32 rxctrl;
477de6ed
AD
3056
3057 /* disable receives while setting up the descriptors */
3058 rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
3059 IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl & ~IXGBE_RXCTRL_RXEN);
3060
3061 ixgbe_setup_psrtype(adapter);
7367096a 3062 ixgbe_setup_rdrxctl(adapter);
477de6ed 3063
9e10e045 3064 /* Program registers for the distribution of queues */
f5b4a52e 3065 ixgbe_setup_mrqc(adapter);
f5b4a52e 3066
9e10e045
AD
3067 ixgbe_set_uta(adapter);
3068
477de6ed
AD
3069 /* set_rx_buffer_len must be called before ring initialization */
3070 ixgbe_set_rx_buffer_len(adapter);
3071
3072 /*
3073 * Setup the HW Rx Head and Tail Descriptor Pointers and
3074 * the Base and Length of the Rx Descriptor Ring
3075 */
9e10e045
AD
3076 for (i = 0; i < adapter->num_rx_queues; i++)
3077 ixgbe_configure_rx_ring(adapter, adapter->rx_ring[i]);
177db6ff 3078
9e10e045
AD
3079 /* disable drop enable for 82598 parts */
3080 if (hw->mac.type == ixgbe_mac_82598EB)
3081 rxctrl |= IXGBE_RXCTRL_DMBYPS;
3082
3083 /* enable all receives */
3084 rxctrl |= IXGBE_RXCTRL_RXEN;
3085 hw->mac.ops.enable_rx_dma(hw, rxctrl);
9a799d71
AK
3086}
3087
068c89b0
DS
3088static void ixgbe_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
3089{
3090 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3091 struct ixgbe_hw *hw = &adapter->hw;
1ada1b1b 3092 int pool_ndx = adapter->num_vfs;
068c89b0
DS
3093
3094 /* add VID to filter table */
1ada1b1b 3095 hw->mac.ops.set_vfta(&adapter->hw, vid, pool_ndx, true);
f62bbb5e 3096 set_bit(vid, adapter->active_vlans);
068c89b0
DS
3097}
3098
3099static void ixgbe_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
3100{
3101 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3102 struct ixgbe_hw *hw = &adapter->hw;
1ada1b1b 3103 int pool_ndx = adapter->num_vfs;
068c89b0 3104
068c89b0 3105 /* remove VID from filter table */
1ada1b1b 3106 hw->mac.ops.set_vfta(&adapter->hw, vid, pool_ndx, false);
f62bbb5e 3107 clear_bit(vid, adapter->active_vlans);
068c89b0
DS
3108}
3109
5f6c0181
JB
3110/**
3111 * ixgbe_vlan_filter_disable - helper to disable hw vlan filtering
3112 * @adapter: driver data
3113 */
3114static void ixgbe_vlan_filter_disable(struct ixgbe_adapter *adapter)
3115{
3116 struct ixgbe_hw *hw = &adapter->hw;
f62bbb5e
JG
3117 u32 vlnctrl;
3118
3119 vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
3120 vlnctrl &= ~(IXGBE_VLNCTRL_VFE | IXGBE_VLNCTRL_CFIEN);
3121 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
3122}
3123
3124/**
3125 * ixgbe_vlan_filter_enable - helper to enable hw vlan filtering
3126 * @adapter: driver data
3127 */
3128static void ixgbe_vlan_filter_enable(struct ixgbe_adapter *adapter)
3129{
3130 struct ixgbe_hw *hw = &adapter->hw;
3131 u32 vlnctrl;
3132
3133 vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
3134 vlnctrl |= IXGBE_VLNCTRL_VFE;
3135 vlnctrl &= ~IXGBE_VLNCTRL_CFIEN;
3136 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
3137}
3138
3139/**
3140 * ixgbe_vlan_strip_disable - helper to disable hw vlan stripping
3141 * @adapter: driver data
3142 */
3143static void ixgbe_vlan_strip_disable(struct ixgbe_adapter *adapter)
3144{
3145 struct ixgbe_hw *hw = &adapter->hw;
3146 u32 vlnctrl;
5f6c0181
JB
3147 int i, j;
3148
3149 switch (hw->mac.type) {
3150 case ixgbe_mac_82598EB:
f62bbb5e
JG
3151 vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
3152 vlnctrl &= ~IXGBE_VLNCTRL_VME;
5f6c0181
JB
3153 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
3154 break;
3155 case ixgbe_mac_82599EB:
5f6c0181
JB
3156 for (i = 0; i < adapter->num_rx_queues; i++) {
3157 j = adapter->rx_ring[i]->reg_idx;
3158 vlnctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(j));
3159 vlnctrl &= ~IXGBE_RXDCTL_VME;
3160 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(j), vlnctrl);
3161 }
3162 break;
3163 default:
3164 break;
3165 }
3166}
3167
3168/**
f62bbb5e 3169 * ixgbe_vlan_strip_enable - helper to enable hw vlan stripping
5f6c0181
JB
3170 * @adapter: driver data
3171 */
f62bbb5e 3172static void ixgbe_vlan_strip_enable(struct ixgbe_adapter *adapter)
5f6c0181
JB
3173{
3174 struct ixgbe_hw *hw = &adapter->hw;
f62bbb5e 3175 u32 vlnctrl;
5f6c0181
JB
3176 int i, j;
3177
3178 switch (hw->mac.type) {
3179 case ixgbe_mac_82598EB:
f62bbb5e
JG
3180 vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
3181 vlnctrl |= IXGBE_VLNCTRL_VME;
5f6c0181
JB
3182 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
3183 break;
3184 case ixgbe_mac_82599EB:
5f6c0181
JB
3185 for (i = 0; i < adapter->num_rx_queues; i++) {
3186 j = adapter->rx_ring[i]->reg_idx;
3187 vlnctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(j));
3188 vlnctrl |= IXGBE_RXDCTL_VME;
3189 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(j), vlnctrl);
3190 }
3191 break;
3192 default:
3193 break;
3194 }
3195}
3196
9a799d71
AK
3197static void ixgbe_restore_vlan(struct ixgbe_adapter *adapter)
3198{
f62bbb5e 3199 u16 vid;
9a799d71 3200
f62bbb5e
JG
3201 ixgbe_vlan_rx_add_vid(adapter->netdev, 0);
3202
3203 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
3204 ixgbe_vlan_rx_add_vid(adapter->netdev, vid);
9a799d71
AK
3205}
3206
2850062a
AD
3207/**
3208 * ixgbe_write_uc_addr_list - write unicast addresses to RAR table
3209 * @netdev: network interface device structure
3210 *
3211 * Writes unicast address list to the RAR table.
3212 * Returns: -ENOMEM on failure/insufficient address space
3213 * 0 on no addresses written
3214 * X on writing X addresses to the RAR table
3215 **/
3216static int ixgbe_write_uc_addr_list(struct net_device *netdev)
3217{
3218 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3219 struct ixgbe_hw *hw = &adapter->hw;
3220 unsigned int vfn = adapter->num_vfs;
3221 unsigned int rar_entries = hw->mac.num_rar_entries - (vfn + 1);
3222 int count = 0;
3223
3224 /* return ENOMEM indicating insufficient memory for addresses */
3225 if (netdev_uc_count(netdev) > rar_entries)
3226 return -ENOMEM;
3227
3228 if (!netdev_uc_empty(netdev) && rar_entries) {
3229 struct netdev_hw_addr *ha;
3230 /* return error if we do not support writing to RAR table */
3231 if (!hw->mac.ops.set_rar)
3232 return -ENOMEM;
3233
3234 netdev_for_each_uc_addr(ha, netdev) {
3235 if (!rar_entries)
3236 break;
3237 hw->mac.ops.set_rar(hw, rar_entries--, ha->addr,
3238 vfn, IXGBE_RAH_AV);
3239 count++;
3240 }
3241 }
3242 /* write the addresses in reverse order to avoid write combining */
3243 for (; rar_entries > 0 ; rar_entries--)
3244 hw->mac.ops.clear_rar(hw, rar_entries);
3245
3246 return count;
3247}
3248
9a799d71 3249/**
2c5645cf 3250 * ixgbe_set_rx_mode - Unicast, Multicast and Promiscuous mode set
9a799d71
AK
3251 * @netdev: network interface device structure
3252 *
2c5645cf
CL
3253 * The set_rx_method entry point is called whenever the unicast/multicast
3254 * address list or the network interface flags are updated. This routine is
3255 * responsible for configuring the hardware for proper unicast, multicast and
3256 * promiscuous mode.
9a799d71 3257 **/
7f870475 3258void ixgbe_set_rx_mode(struct net_device *netdev)
9a799d71
AK
3259{
3260 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3261 struct ixgbe_hw *hw = &adapter->hw;
2850062a
AD
3262 u32 fctrl, vmolr = IXGBE_VMOLR_BAM | IXGBE_VMOLR_AUPE;
3263 int count;
9a799d71
AK
3264
3265 /* Check for Promiscuous and All Multicast modes */
3266
3267 fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
3268
f5dc442b
AD
3269 /* set all bits that we expect to always be set */
3270 fctrl |= IXGBE_FCTRL_BAM;
3271 fctrl |= IXGBE_FCTRL_DPF; /* discard pause frames when FC enabled */
3272 fctrl |= IXGBE_FCTRL_PMCF;
3273
2850062a
AD
3274 /* clear the bits we are changing the status of */
3275 fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
3276
9a799d71 3277 if (netdev->flags & IFF_PROMISC) {
e433ea1f 3278 hw->addr_ctrl.user_set_promisc = true;
9a799d71 3279 fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
2850062a 3280 vmolr |= (IXGBE_VMOLR_ROPE | IXGBE_VMOLR_MPE);
5f6c0181
JB
3281 /* don't hardware filter vlans in promisc mode */
3282 ixgbe_vlan_filter_disable(adapter);
9a799d71 3283 } else {
746b9f02
PM
3284 if (netdev->flags & IFF_ALLMULTI) {
3285 fctrl |= IXGBE_FCTRL_MPE;
2850062a
AD
3286 vmolr |= IXGBE_VMOLR_MPE;
3287 } else {
3288 /*
3289 * Write addresses to the MTA, if the attempt fails
3290 * then we should just turn on promiscous mode so
3291 * that we can at least receive multicast traffic
3292 */
3293 hw->mac.ops.update_mc_addr_list(hw, netdev);
3294 vmolr |= IXGBE_VMOLR_ROMPE;
746b9f02 3295 }
5f6c0181 3296 ixgbe_vlan_filter_enable(adapter);
e433ea1f 3297 hw->addr_ctrl.user_set_promisc = false;
2850062a
AD
3298 /*
3299 * Write addresses to available RAR registers, if there is not
3300 * sufficient space to store all the addresses then enable
3301 * unicast promiscous mode
3302 */
3303 count = ixgbe_write_uc_addr_list(netdev);
3304 if (count < 0) {
3305 fctrl |= IXGBE_FCTRL_UPE;
3306 vmolr |= IXGBE_VMOLR_ROPE;
3307 }
9a799d71
AK
3308 }
3309
2850062a 3310 if (adapter->num_vfs) {
1cdd1ec8 3311 ixgbe_restore_vf_multicasts(adapter);
2850062a
AD
3312 vmolr |= IXGBE_READ_REG(hw, IXGBE_VMOLR(adapter->num_vfs)) &
3313 ~(IXGBE_VMOLR_MPE | IXGBE_VMOLR_ROMPE |
3314 IXGBE_VMOLR_ROPE);
3315 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(adapter->num_vfs), vmolr);
3316 }
3317
3318 IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
f62bbb5e
JG
3319
3320 if (netdev->features & NETIF_F_HW_VLAN_RX)
3321 ixgbe_vlan_strip_enable(adapter);
3322 else
3323 ixgbe_vlan_strip_disable(adapter);
9a799d71
AK
3324}
3325
021230d4
AV
3326static void ixgbe_napi_enable_all(struct ixgbe_adapter *adapter)
3327{
3328 int q_idx;
3329 struct ixgbe_q_vector *q_vector;
3330 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
3331
3332 /* legacy and MSI only use one vector */
3333 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
3334 q_vectors = 1;
3335
3336 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
f0848276 3337 struct napi_struct *napi;
7a921c93 3338 q_vector = adapter->q_vector[q_idx];
f0848276 3339 napi = &q_vector->napi;
91281fd3
AD
3340 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
3341 if (!q_vector->rxr_count || !q_vector->txr_count) {
3342 if (q_vector->txr_count == 1)
3343 napi->poll = &ixgbe_clean_txonly;
3344 else if (q_vector->rxr_count == 1)
3345 napi->poll = &ixgbe_clean_rxonly;
3346 }
3347 }
f0848276
JB
3348
3349 napi_enable(napi);
021230d4
AV
3350 }
3351}
3352
3353static void ixgbe_napi_disable_all(struct ixgbe_adapter *adapter)
3354{
3355 int q_idx;
3356 struct ixgbe_q_vector *q_vector;
3357 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
3358
3359 /* legacy and MSI only use one vector */
3360 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
3361 q_vectors = 1;
3362
3363 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
7a921c93 3364 q_vector = adapter->q_vector[q_idx];
021230d4
AV
3365 napi_disable(&q_vector->napi);
3366 }
3367}
3368
7a6b6f51 3369#ifdef CONFIG_IXGBE_DCB
2f90b865
AD
3370/*
3371 * ixgbe_configure_dcb - Configure DCB hardware
3372 * @adapter: ixgbe adapter struct
3373 *
3374 * This is called by the driver on open to configure the DCB hardware.
3375 * This is also called by the gennetlink interface when reconfiguring
3376 * the DCB state.
3377 */
3378static void ixgbe_configure_dcb(struct ixgbe_adapter *adapter)
3379{
3380 struct ixgbe_hw *hw = &adapter->hw;
9806307a 3381 int max_frame = adapter->netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
2f90b865 3382
67ebd791
AD
3383 if (!(adapter->flags & IXGBE_FLAG_DCB_ENABLED)) {
3384 if (hw->mac.type == ixgbe_mac_82598EB)
3385 netif_set_gso_max_size(adapter->netdev, 65536);
3386 return;
3387 }
3388
3389 if (hw->mac.type == ixgbe_mac_82598EB)
3390 netif_set_gso_max_size(adapter->netdev, 32768);
3391
9806307a
JF
3392#ifdef CONFIG_FCOE
3393 if (adapter->netdev->features & NETIF_F_FCOE_MTU)
3394 max_frame = max(max_frame, IXGBE_FCOE_JUMBO_FRAME_SIZE);
3395#endif
3396
80ab193d 3397 ixgbe_dcb_calculate_tc_credits(hw, &adapter->dcb_cfg, max_frame,
9806307a 3398 DCB_TX_CONFIG);
80ab193d 3399 ixgbe_dcb_calculate_tc_credits(hw, &adapter->dcb_cfg, max_frame,
9806307a 3400 DCB_RX_CONFIG);
2f90b865 3401
2f90b865 3402 /* Enable VLAN tag insert/strip */
f62bbb5e 3403 adapter->netdev->features |= NETIF_F_HW_VLAN_RX;
5f6c0181 3404
2f90b865 3405 hw->mac.ops.set_vfta(&adapter->hw, 0, 0, true);
01fa7d90
AD
3406
3407 /* reconfigure the hardware */
3408 ixgbe_dcb_hw_config(hw, &adapter->dcb_cfg);
2f90b865
AD
3409}
3410
3411#endif
9a799d71
AK
3412static void ixgbe_configure(struct ixgbe_adapter *adapter)
3413{
3414 struct net_device *netdev = adapter->netdev;
c4cf55e5 3415 struct ixgbe_hw *hw = &adapter->hw;
9a799d71
AK
3416 int i;
3417
7a6b6f51 3418#ifdef CONFIG_IXGBE_DCB
67ebd791 3419 ixgbe_configure_dcb(adapter);
2f90b865 3420#endif
9a799d71 3421
f62bbb5e
JG
3422 ixgbe_set_rx_mode(netdev);
3423 ixgbe_restore_vlan(adapter);
3424
eacd73f7
YZ
3425#ifdef IXGBE_FCOE
3426 if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED)
3427 ixgbe_configure_fcoe(adapter);
3428
3429#endif /* IXGBE_FCOE */
c4cf55e5
PWJ
3430 if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) {
3431 for (i = 0; i < adapter->num_tx_queues; i++)
4a0b9ca0 3432 adapter->tx_ring[i]->atr_sample_rate =
e8e9f696 3433 adapter->atr_sample_rate;
c4cf55e5
PWJ
3434 ixgbe_init_fdir_signature_82599(hw, adapter->fdir_pballoc);
3435 } else if (adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE) {
3436 ixgbe_init_fdir_perfect_82599(hw, adapter->fdir_pballoc);
3437 }
933d41f1 3438 ixgbe_configure_virtualization(adapter);
c4cf55e5 3439
9a799d71
AK
3440 ixgbe_configure_tx(adapter);
3441 ixgbe_configure_rx(adapter);
9a799d71
AK
3442}
3443
e8e26350
PW
3444static inline bool ixgbe_is_sfp(struct ixgbe_hw *hw)
3445{
3446 switch (hw->phy.type) {
3447 case ixgbe_phy_sfp_avago:
3448 case ixgbe_phy_sfp_ftl:
3449 case ixgbe_phy_sfp_intel:
3450 case ixgbe_phy_sfp_unknown:
ea0a04df
DS
3451 case ixgbe_phy_sfp_passive_tyco:
3452 case ixgbe_phy_sfp_passive_unknown:
3453 case ixgbe_phy_sfp_active_unknown:
3454 case ixgbe_phy_sfp_ftl_active:
e8e26350
PW
3455 return true;
3456 default:
3457 return false;
3458 }
3459}
3460
0ecc061d 3461/**
e8e26350
PW
3462 * ixgbe_sfp_link_config - set up SFP+ link
3463 * @adapter: pointer to private adapter struct
3464 **/
3465static void ixgbe_sfp_link_config(struct ixgbe_adapter *adapter)
3466{
3467 struct ixgbe_hw *hw = &adapter->hw;
3468
3469 if (hw->phy.multispeed_fiber) {
3470 /*
3471 * In multispeed fiber setups, the device may not have
3472 * had a physical connection when the driver loaded.
3473 * If that's the case, the initial link configuration
3474 * couldn't get the MAC into 10G or 1G mode, so we'll
3475 * never have a link status change interrupt fire.
3476 * We need to try and force an autonegotiation
3477 * session, then bring up link.
3478 */
3479 hw->mac.ops.setup_sfp(hw);
3480 if (!(adapter->flags & IXGBE_FLAG_IN_SFP_LINK_TASK))
3481 schedule_work(&adapter->multispeed_fiber_task);
3482 } else {
3483 /*
3484 * Direct Attach Cu and non-multispeed fiber modules
3485 * still need to be configured properly prior to
3486 * attempting link.
3487 */
3488 if (!(adapter->flags & IXGBE_FLAG_IN_SFP_MOD_TASK))
3489 schedule_work(&adapter->sfp_config_module_task);
3490 }
3491}
3492
3493/**
3494 * ixgbe_non_sfp_link_config - set up non-SFP+ link
0ecc061d
PWJ
3495 * @hw: pointer to private hardware struct
3496 *
3497 * Returns 0 on success, negative on failure
3498 **/
e8e26350 3499static int ixgbe_non_sfp_link_config(struct ixgbe_hw *hw)
0ecc061d
PWJ
3500{
3501 u32 autoneg;
8620a103 3502 bool negotiation, link_up = false;
0ecc061d
PWJ
3503 u32 ret = IXGBE_ERR_LINK_SETUP;
3504
3505 if (hw->mac.ops.check_link)
3506 ret = hw->mac.ops.check_link(hw, &autoneg, &link_up, false);
3507
3508 if (ret)
3509 goto link_cfg_out;
3510
3511 if (hw->mac.ops.get_link_capabilities)
e8e9f696
JP
3512 ret = hw->mac.ops.get_link_capabilities(hw, &autoneg,
3513 &negotiation);
0ecc061d
PWJ
3514 if (ret)
3515 goto link_cfg_out;
3516
8620a103
MC
3517 if (hw->mac.ops.setup_link)
3518 ret = hw->mac.ops.setup_link(hw, autoneg, negotiation, link_up);
0ecc061d
PWJ
3519link_cfg_out:
3520 return ret;
3521}
3522
a34bcfff 3523static void ixgbe_setup_gpie(struct ixgbe_adapter *adapter)
9a799d71 3524{
9a799d71 3525 struct ixgbe_hw *hw = &adapter->hw;
a34bcfff 3526 u32 gpie = 0;
9a799d71 3527
9b471446 3528 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
a34bcfff
AD
3529 gpie = IXGBE_GPIE_MSIX_MODE | IXGBE_GPIE_PBA_SUPPORT |
3530 IXGBE_GPIE_OCD;
3531 gpie |= IXGBE_GPIE_EIAME;
9b471446
JB
3532 /*
3533 * use EIAM to auto-mask when MSI-X interrupt is asserted
3534 * this saves a register write for every interrupt
3535 */
3536 switch (hw->mac.type) {
3537 case ixgbe_mac_82598EB:
3538 IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
3539 break;
3540 default:
3541 case ixgbe_mac_82599EB:
3542 IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(0), 0xFFFFFFFF);
3543 IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(1), 0xFFFFFFFF);
3544 break;
3545 }
3546 } else {
021230d4
AV
3547 /* legacy interrupts, use EIAM to auto-mask when reading EICR,
3548 * specifically only auto mask tx and rx interrupts */
3549 IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
3550 }
9a799d71 3551
a34bcfff
AD
3552 /* XXX: to interrupt immediately for EICS writes, enable this */
3553 /* gpie |= IXGBE_GPIE_EIMEN; */
3554
3555 if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) {
3556 gpie &= ~IXGBE_GPIE_VTMODE_MASK;
3557 gpie |= IXGBE_GPIE_VTMODE_64;
119fc60a
MC
3558 }
3559
a34bcfff
AD
3560 /* Enable fan failure interrupt */
3561 if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE)
0befdb3e 3562 gpie |= IXGBE_SDP1_GPIEN;
0befdb3e 3563
a34bcfff 3564 if (hw->mac.type == ixgbe_mac_82599EB)
e8e26350
PW
3565 gpie |= IXGBE_SDP1_GPIEN;
3566 gpie |= IXGBE_SDP2_GPIEN;
a34bcfff
AD
3567
3568 IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
3569}
3570
3571static int ixgbe_up_complete(struct ixgbe_adapter *adapter)
3572{
3573 struct ixgbe_hw *hw = &adapter->hw;
a34bcfff 3574 int err;
a34bcfff
AD
3575 u32 ctrl_ext;
3576
3577 ixgbe_get_hw_control(adapter);
3578 ixgbe_setup_gpie(adapter);
e8e26350 3579
9a799d71
AK
3580 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
3581 ixgbe_configure_msix(adapter);
3582 else
3583 ixgbe_configure_msi_and_legacy(adapter);
3584
61fac744
PW
3585 /* enable the optics */
3586 if (hw->phy.multispeed_fiber)
3587 hw->mac.ops.enable_tx_laser(hw);
3588
9a799d71 3589 clear_bit(__IXGBE_DOWN, &adapter->state);
021230d4
AV
3590 ixgbe_napi_enable_all(adapter);
3591
73c4b7cd
AD
3592 if (ixgbe_is_sfp(hw)) {
3593 ixgbe_sfp_link_config(adapter);
3594 } else {
3595 err = ixgbe_non_sfp_link_config(hw);
3596 if (err)
3597 e_err(probe, "link_config FAILED %d\n", err);
3598 }
3599
021230d4
AV
3600 /* clear any pending interrupts, may auto mask */
3601 IXGBE_READ_REG(hw, IXGBE_EICR);
6af3b9eb 3602 ixgbe_irq_enable(adapter, true, true);
9a799d71 3603
bf069c97
DS
3604 /*
3605 * If this adapter has a fan, check to see if we had a failure
3606 * before we enabled the interrupt.
3607 */
3608 if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE) {
3609 u32 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
3610 if (esdp & IXGBE_ESDP_SDP1)
396e799c 3611 e_crit(drv, "Fan has stopped, replace the adapter\n");
bf069c97
DS
3612 }
3613
e8e26350
PW
3614 /*
3615 * For hot-pluggable SFP+ devices, a new SFP+ module may have
19343de2
DS
3616 * arrived before interrupts were enabled but after probe. Such
3617 * devices wouldn't have their type identified yet. We need to
3618 * kick off the SFP+ module setup first, then try to bring up link.
e8e26350
PW
3619 * If we're not hot-pluggable SFP+, we just need to configure link
3620 * and bring it up.
3621 */
73c4b7cd
AD
3622 if (hw->phy.type == ixgbe_phy_unknown)
3623 schedule_work(&adapter->sfp_config_module_task);
0ecc061d 3624
1da100bb 3625 /* enable transmits */
477de6ed 3626 netif_tx_start_all_queues(adapter->netdev);
1da100bb 3627
9a799d71
AK
3628 /* bring the link up in the watchdog, this could race with our first
3629 * link up interrupt but shouldn't be a problem */
cf8280ee
JB
3630 adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
3631 adapter->link_check_timeout = jiffies;
9a799d71 3632 mod_timer(&adapter->watchdog_timer, jiffies);
c9205697
GR
3633
3634 /* Set PF Reset Done bit so PF/VF Mail Ops can work */
3635 ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
3636 ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
3637 IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
3638
9a799d71
AK
3639 return 0;
3640}
3641
d4f80882
AV
3642void ixgbe_reinit_locked(struct ixgbe_adapter *adapter)
3643{
3644 WARN_ON(in_interrupt());
3645 while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
3646 msleep(1);
3647 ixgbe_down(adapter);
5809a1ae
GR
3648 /*
3649 * If SR-IOV enabled then wait a bit before bringing the adapter
3650 * back up to give the VFs time to respond to the reset. The
3651 * two second wait is based upon the watchdog timer cycle in
3652 * the VF driver.
3653 */
3654 if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
3655 msleep(2000);
d4f80882
AV
3656 ixgbe_up(adapter);
3657 clear_bit(__IXGBE_RESETTING, &adapter->state);
3658}
3659
9a799d71
AK
3660int ixgbe_up(struct ixgbe_adapter *adapter)
3661{
3662 /* hardware has been reset, we need to reload some things */
3663 ixgbe_configure(adapter);
3664
3665 return ixgbe_up_complete(adapter);
3666}
3667
3668void ixgbe_reset(struct ixgbe_adapter *adapter)
3669{
c44ade9e 3670 struct ixgbe_hw *hw = &adapter->hw;
8ca783ab
DS
3671 int err;
3672
3673 err = hw->mac.ops.init_hw(hw);
da4dd0f7
PWJ
3674 switch (err) {
3675 case 0:
3676 case IXGBE_ERR_SFP_NOT_PRESENT:
3677 break;
3678 case IXGBE_ERR_MASTER_REQUESTS_PENDING:
849c4542 3679 e_dev_err("master disable timed out\n");
da4dd0f7 3680 break;
794caeb2
PWJ
3681 case IXGBE_ERR_EEPROM_VERSION:
3682 /* We are running on a pre-production device, log a warning */
849c4542
ET
3683 e_dev_warn("This device is a pre-production adapter/LOM. "
3684 "Please be aware there may be issuesassociated with "
3685 "your hardware. If you are experiencing problems "
3686 "please contact your Intel or hardware "
3687 "representative who provided you with this "
3688 "hardware.\n");
794caeb2 3689 break;
da4dd0f7 3690 default:
849c4542 3691 e_dev_err("Hardware Error: %d\n", err);
da4dd0f7 3692 }
9a799d71
AK
3693
3694 /* reprogram the RAR[0] in case user changed it. */
1cdd1ec8
GR
3695 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, adapter->num_vfs,
3696 IXGBE_RAH_AV);
9a799d71
AK
3697}
3698
9a799d71
AK
3699/**
3700 * ixgbe_clean_rx_ring - Free Rx Buffers per Queue
9a799d71
AK
3701 * @rx_ring: ring to free buffers from
3702 **/
b6ec895e 3703static void ixgbe_clean_rx_ring(struct ixgbe_ring *rx_ring)
9a799d71 3704{
b6ec895e 3705 struct device *dev = rx_ring->dev;
9a799d71 3706 unsigned long size;
b6ec895e 3707 u16 i;
9a799d71 3708
84418e3b
AD
3709 /* ring already cleared, nothing to do */
3710 if (!rx_ring->rx_buffer_info)
3711 return;
9a799d71 3712
84418e3b 3713 /* Free all the Rx ring sk_buffs */
9a799d71
AK
3714 for (i = 0; i < rx_ring->count; i++) {
3715 struct ixgbe_rx_buffer *rx_buffer_info;
3716
3717 rx_buffer_info = &rx_ring->rx_buffer_info[i];
3718 if (rx_buffer_info->dma) {
b6ec895e 3719 dma_unmap_single(rx_ring->dev, rx_buffer_info->dma,
e8e9f696 3720 rx_ring->rx_buf_len,
1b507730 3721 DMA_FROM_DEVICE);
9a799d71
AK
3722 rx_buffer_info->dma = 0;
3723 }
3724 if (rx_buffer_info->skb) {
f8212f97 3725 struct sk_buff *skb = rx_buffer_info->skb;
9a799d71 3726 rx_buffer_info->skb = NULL;
f8212f97
AD
3727 do {
3728 struct sk_buff *this = skb;
e8171aaa 3729 if (IXGBE_RSC_CB(this)->delay_unmap) {
b6ec895e 3730 dma_unmap_single(dev,
1b507730 3731 IXGBE_RSC_CB(this)->dma,
e8e9f696 3732 rx_ring->rx_buf_len,
1b507730 3733 DMA_FROM_DEVICE);
fd3686a8 3734 IXGBE_RSC_CB(this)->dma = 0;
e8171aaa 3735 IXGBE_RSC_CB(skb)->delay_unmap = false;
fd3686a8 3736 }
f8212f97
AD
3737 skb = skb->prev;
3738 dev_kfree_skb(this);
3739 } while (skb);
9a799d71
AK
3740 }
3741 if (!rx_buffer_info->page)
3742 continue;
4f57ca6e 3743 if (rx_buffer_info->page_dma) {
b6ec895e 3744 dma_unmap_page(dev, rx_buffer_info->page_dma,
1b507730 3745 PAGE_SIZE / 2, DMA_FROM_DEVICE);
4f57ca6e
JB
3746 rx_buffer_info->page_dma = 0;
3747 }
9a799d71
AK
3748 put_page(rx_buffer_info->page);
3749 rx_buffer_info->page = NULL;
762f4c57 3750 rx_buffer_info->page_offset = 0;
9a799d71
AK
3751 }
3752
3753 size = sizeof(struct ixgbe_rx_buffer) * rx_ring->count;
3754 memset(rx_ring->rx_buffer_info, 0, size);
3755
3756 /* Zero out the descriptor ring */
3757 memset(rx_ring->desc, 0, rx_ring->size);
3758
3759 rx_ring->next_to_clean = 0;
3760 rx_ring->next_to_use = 0;
9a799d71
AK
3761}
3762
3763/**
3764 * ixgbe_clean_tx_ring - Free Tx Buffers
9a799d71
AK
3765 * @tx_ring: ring to be cleaned
3766 **/
b6ec895e 3767static void ixgbe_clean_tx_ring(struct ixgbe_ring *tx_ring)
9a799d71
AK
3768{
3769 struct ixgbe_tx_buffer *tx_buffer_info;
3770 unsigned long size;
b6ec895e 3771 u16 i;
9a799d71 3772
84418e3b
AD
3773 /* ring already cleared, nothing to do */
3774 if (!tx_ring->tx_buffer_info)
3775 return;
9a799d71 3776
84418e3b 3777 /* Free all the Tx ring sk_buffs */
9a799d71
AK
3778 for (i = 0; i < tx_ring->count; i++) {
3779 tx_buffer_info = &tx_ring->tx_buffer_info[i];
b6ec895e 3780 ixgbe_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
9a799d71
AK
3781 }
3782
3783 size = sizeof(struct ixgbe_tx_buffer) * tx_ring->count;
3784 memset(tx_ring->tx_buffer_info, 0, size);
3785
3786 /* Zero out the descriptor ring */
3787 memset(tx_ring->desc, 0, tx_ring->size);
3788
3789 tx_ring->next_to_use = 0;
3790 tx_ring->next_to_clean = 0;
9a799d71
AK
3791}
3792
3793/**
021230d4 3794 * ixgbe_clean_all_rx_rings - Free Rx Buffers for all queues
9a799d71
AK
3795 * @adapter: board private structure
3796 **/
021230d4 3797static void ixgbe_clean_all_rx_rings(struct ixgbe_adapter *adapter)
9a799d71
AK
3798{
3799 int i;
3800
021230d4 3801 for (i = 0; i < adapter->num_rx_queues; i++)
b6ec895e 3802 ixgbe_clean_rx_ring(adapter->rx_ring[i]);
9a799d71
AK
3803}
3804
3805/**
021230d4 3806 * ixgbe_clean_all_tx_rings - Free Tx Buffers for all queues
9a799d71
AK
3807 * @adapter: board private structure
3808 **/
021230d4 3809static void ixgbe_clean_all_tx_rings(struct ixgbe_adapter *adapter)
9a799d71
AK
3810{
3811 int i;
3812
021230d4 3813 for (i = 0; i < adapter->num_tx_queues; i++)
b6ec895e 3814 ixgbe_clean_tx_ring(adapter->tx_ring[i]);
9a799d71
AK
3815}
3816
3817void ixgbe_down(struct ixgbe_adapter *adapter)
3818{
3819 struct net_device *netdev = adapter->netdev;
7f821875 3820 struct ixgbe_hw *hw = &adapter->hw;
9a799d71 3821 u32 rxctrl;
7f821875
JB
3822 u32 txdctl;
3823 int i, j;
b25ebfd2 3824 int num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
9a799d71
AK
3825
3826 /* signal that we are down to the interrupt handler */
3827 set_bit(__IXGBE_DOWN, &adapter->state);
3828
767081ad
GR
3829 /* disable receive for all VFs and wait one second */
3830 if (adapter->num_vfs) {
767081ad
GR
3831 /* ping all the active vfs to let them know we are going down */
3832 ixgbe_ping_all_vfs(adapter);
581d1aa7 3833
767081ad
GR
3834 /* Disable all VFTE/VFRE TX/RX */
3835 ixgbe_disable_tx_rx(adapter);
581d1aa7
GR
3836
3837 /* Mark all the VFs as inactive */
3838 for (i = 0 ; i < adapter->num_vfs; i++)
3839 adapter->vfinfo[i].clear_to_send = 0;
767081ad
GR
3840 }
3841
9a799d71 3842 /* disable receives */
7f821875
JB
3843 rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
3844 IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl & ~IXGBE_RXCTRL_RXEN);
9a799d71 3845
7f821875 3846 IXGBE_WRITE_FLUSH(hw);
9a799d71
AK
3847 msleep(10);
3848
7f821875
JB
3849 netif_tx_stop_all_queues(netdev);
3850
0a1f87cb
DS
3851 clear_bit(__IXGBE_SFP_MODULE_NOT_FOUND, &adapter->state);
3852 del_timer_sync(&adapter->sfp_timer);
9a799d71 3853 del_timer_sync(&adapter->watchdog_timer);
cf8280ee 3854 cancel_work_sync(&adapter->watchdog_task);
9a799d71 3855
c0dfb90e
JF
3856 netif_carrier_off(netdev);
3857 netif_tx_disable(netdev);
3858
3859 ixgbe_irq_disable(adapter);
3860
3861 ixgbe_napi_disable_all(adapter);
3862
b25ebfd2
PW
3863 /* Cleanup the affinity_hint CPU mask memory and callback */
3864 for (i = 0; i < num_q_vectors; i++) {
3865 struct ixgbe_q_vector *q_vector = adapter->q_vector[i];
3866 /* clear the affinity_mask in the IRQ descriptor */
3867 irq_set_affinity_hint(adapter->msix_entries[i]. vector, NULL);
3868 /* release the CPU mask memory */
3869 free_cpumask_var(q_vector->affinity_mask);
3870 }
3871
c4cf55e5
PWJ
3872 if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE ||
3873 adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE)
3874 cancel_work_sync(&adapter->fdir_reinit_task);
3875
119fc60a
MC
3876 if (adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE)
3877 cancel_work_sync(&adapter->check_overtemp_task);
3878
7f821875
JB
3879 /* disable transmits in the hardware now that interrupts are off */
3880 for (i = 0; i < adapter->num_tx_queues; i++) {
4a0b9ca0 3881 j = adapter->tx_ring[i]->reg_idx;
7f821875
JB
3882 txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(j));
3883 IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(j),
e8e9f696 3884 (txdctl & ~IXGBE_TXDCTL_ENABLE));
7f821875 3885 }
88512539
PW
3886 /* Disable the Tx DMA engine on 82599 */
3887 if (hw->mac.type == ixgbe_mac_82599EB)
3888 IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL,
e8e9f696
JP
3889 (IXGBE_READ_REG(hw, IXGBE_DMATXCTL) &
3890 ~IXGBE_DMATXCTL_TE));
7f821875 3891
9f756f01
JF
3892 /* power down the optics */
3893 if (hw->phy.multispeed_fiber)
3894 hw->mac.ops.disable_tx_laser(hw);
3895
9a713e7c
PW
3896 /* clear n-tuple filters that are cached */
3897 ethtool_ntuple_flush(netdev);
3898
6f4a0e45
PL
3899 if (!pci_channel_offline(adapter->pdev))
3900 ixgbe_reset(adapter);
9a799d71
AK
3901 ixgbe_clean_all_tx_rings(adapter);
3902 ixgbe_clean_all_rx_rings(adapter);
3903
5dd2d332 3904#ifdef CONFIG_IXGBE_DCA
96b0e0f6 3905 /* since we reset the hardware DCA settings were cleared */
e35ec126 3906 ixgbe_setup_dca(adapter);
96b0e0f6 3907#endif
9a799d71
AK
3908}
3909
9a799d71 3910/**
021230d4
AV
3911 * ixgbe_poll - NAPI Rx polling callback
3912 * @napi: structure for representing this polling device
3913 * @budget: how many packets driver is allowed to clean
3914 *
3915 * This function is used for legacy and MSI, NAPI mode
9a799d71 3916 **/
021230d4 3917static int ixgbe_poll(struct napi_struct *napi, int budget)
9a799d71 3918{
9a1a69ad 3919 struct ixgbe_q_vector *q_vector =
e8e9f696 3920 container_of(napi, struct ixgbe_q_vector, napi);
021230d4 3921 struct ixgbe_adapter *adapter = q_vector->adapter;
9a1a69ad 3922 int tx_clean_complete, work_done = 0;
9a799d71 3923
5dd2d332 3924#ifdef CONFIG_IXGBE_DCA
33cf09c9
AD
3925 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
3926 ixgbe_update_dca(q_vector);
bd0362dd
JC
3927#endif
3928
4a0b9ca0
PW
3929 tx_clean_complete = ixgbe_clean_tx_irq(q_vector, adapter->tx_ring[0]);
3930 ixgbe_clean_rx_irq(q_vector, adapter->rx_ring[0], &work_done, budget);
9a799d71 3931
9a1a69ad 3932 if (!tx_clean_complete)
d2c7ddd6
DM
3933 work_done = budget;
3934
53e52c72
DM
3935 /* If budget not fully consumed, exit the polling mode */
3936 if (work_done < budget) {
288379f0 3937 napi_complete(napi);
f7554a2b 3938 if (adapter->rx_itr_setting & 1)
f494e8fa 3939 ixgbe_set_itr(adapter);
d4f80882 3940 if (!test_bit(__IXGBE_DOWN, &adapter->state))
835462fc 3941 ixgbe_irq_enable_queues(adapter, IXGBE_EIMS_RTX_QUEUE);
9a799d71 3942 }
9a799d71
AK
3943 return work_done;
3944}
3945
3946/**
3947 * ixgbe_tx_timeout - Respond to a Tx Hang
3948 * @netdev: network interface device structure
3949 **/
3950static void ixgbe_tx_timeout(struct net_device *netdev)
3951{
3952 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3953
3954 /* Do the reset outside of interrupt context */
3955 schedule_work(&adapter->reset_task);
3956}
3957
3958static void ixgbe_reset_task(struct work_struct *work)
3959{
3960 struct ixgbe_adapter *adapter;
3961 adapter = container_of(work, struct ixgbe_adapter, reset_task);
3962
2f90b865
AD
3963 /* If we're already down or resetting, just bail */
3964 if (test_bit(__IXGBE_DOWN, &adapter->state) ||
3965 test_bit(__IXGBE_RESETTING, &adapter->state))
3966 return;
3967
9a799d71
AK
3968 adapter->tx_timeout_count++;
3969
dcd79aeb
TI
3970 ixgbe_dump(adapter);
3971 netdev_err(adapter->netdev, "Reset adapter\n");
d4f80882 3972 ixgbe_reinit_locked(adapter);
9a799d71
AK
3973}
3974
bc97114d
PWJ
3975#ifdef CONFIG_IXGBE_DCB
3976static inline bool ixgbe_set_dcb_queues(struct ixgbe_adapter *adapter)
b9804972 3977{
bc97114d 3978 bool ret = false;
0cefafad 3979 struct ixgbe_ring_feature *f = &adapter->ring_feature[RING_F_DCB];
b9804972 3980
0cefafad
JB
3981 if (!(adapter->flags & IXGBE_FLAG_DCB_ENABLED))
3982 return ret;
3983
3984 f->mask = 0x7 << 3;
3985 adapter->num_rx_queues = f->indices;
3986 adapter->num_tx_queues = f->indices;
3987 ret = true;
2f90b865 3988
bc97114d
PWJ
3989 return ret;
3990}
3991#endif
3992
4df10466
JB
3993/**
3994 * ixgbe_set_rss_queues: Allocate queues for RSS
3995 * @adapter: board private structure to initialize
3996 *
3997 * This is our "base" multiqueue mode. RSS (Receive Side Scaling) will try
3998 * to allocate one Rx queue per CPU, and if available, one Tx queue per CPU.
3999 *
4000 **/
bc97114d
PWJ
4001static inline bool ixgbe_set_rss_queues(struct ixgbe_adapter *adapter)
4002{
4003 bool ret = false;
0cefafad 4004 struct ixgbe_ring_feature *f = &adapter->ring_feature[RING_F_RSS];
bc97114d
PWJ
4005
4006 if (adapter->flags & IXGBE_FLAG_RSS_ENABLED) {
0cefafad
JB
4007 f->mask = 0xF;
4008 adapter->num_rx_queues = f->indices;
4009 adapter->num_tx_queues = f->indices;
bc97114d
PWJ
4010 ret = true;
4011 } else {
bc97114d 4012 ret = false;
b9804972
JB
4013 }
4014
bc97114d
PWJ
4015 return ret;
4016}
4017
c4cf55e5
PWJ
4018/**
4019 * ixgbe_set_fdir_queues: Allocate queues for Flow Director
4020 * @adapter: board private structure to initialize
4021 *
4022 * Flow Director is an advanced Rx filter, attempting to get Rx flows back
4023 * to the original CPU that initiated the Tx session. This runs in addition
4024 * to RSS, so if a packet doesn't match an FDIR filter, we can still spread the
4025 * Rx load across CPUs using RSS.
4026 *
4027 **/
e8e9f696 4028static inline bool ixgbe_set_fdir_queues(struct ixgbe_adapter *adapter)
c4cf55e5
PWJ
4029{
4030 bool ret = false;
4031 struct ixgbe_ring_feature *f_fdir = &adapter->ring_feature[RING_F_FDIR];
4032
4033 f_fdir->indices = min((int)num_online_cpus(), f_fdir->indices);
4034 f_fdir->mask = 0;
4035
4036 /* Flow Director must have RSS enabled */
4037 if (adapter->flags & IXGBE_FLAG_RSS_ENABLED &&
4038 ((adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE ||
4039 (adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE)))) {
4040 adapter->num_tx_queues = f_fdir->indices;
4041 adapter->num_rx_queues = f_fdir->indices;
4042 ret = true;
4043 } else {
4044 adapter->flags &= ~IXGBE_FLAG_FDIR_HASH_CAPABLE;
4045 adapter->flags &= ~IXGBE_FLAG_FDIR_PERFECT_CAPABLE;
4046 }
4047 return ret;
4048}
4049
0331a832
YZ
4050#ifdef IXGBE_FCOE
4051/**
4052 * ixgbe_set_fcoe_queues: Allocate queues for Fiber Channel over Ethernet (FCoE)
4053 * @adapter: board private structure to initialize
4054 *
4055 * FCoE RX FCRETA can use up to 8 rx queues for up to 8 different exchanges.
4056 * The ring feature mask is not used as a mask for FCoE, as it can take any 8
4057 * rx queues out of the max number of rx queues, instead, it is used as the
4058 * index of the first rx queue used by FCoE.
4059 *
4060 **/
4061static inline bool ixgbe_set_fcoe_queues(struct ixgbe_adapter *adapter)
4062{
4063 bool ret = false;
4064 struct ixgbe_ring_feature *f = &adapter->ring_feature[RING_F_FCOE];
4065
4066 f->indices = min((int)num_online_cpus(), f->indices);
4067 if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED) {
8de8b2e6
YZ
4068 adapter->num_rx_queues = 1;
4069 adapter->num_tx_queues = 1;
0331a832
YZ
4070#ifdef CONFIG_IXGBE_DCB
4071 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
396e799c 4072 e_info(probe, "FCoE enabled with DCB\n");
0331a832
YZ
4073 ixgbe_set_dcb_queues(adapter);
4074 }
4075#endif
4076 if (adapter->flags & IXGBE_FLAG_RSS_ENABLED) {
396e799c 4077 e_info(probe, "FCoE enabled with RSS\n");
8faa2a78
YZ
4078 if ((adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) ||
4079 (adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE))
4080 ixgbe_set_fdir_queues(adapter);
4081 else
4082 ixgbe_set_rss_queues(adapter);
0331a832
YZ
4083 }
4084 /* adding FCoE rx rings to the end */
4085 f->mask = adapter->num_rx_queues;
4086 adapter->num_rx_queues += f->indices;
8de8b2e6 4087 adapter->num_tx_queues += f->indices;
0331a832
YZ
4088
4089 ret = true;
4090 }
4091
4092 return ret;
4093}
4094
4095#endif /* IXGBE_FCOE */
1cdd1ec8
GR
4096/**
4097 * ixgbe_set_sriov_queues: Allocate queues for IOV use
4098 * @adapter: board private structure to initialize
4099 *
4100 * IOV doesn't actually use anything, so just NAK the
4101 * request for now and let the other queue routines
4102 * figure out what to do.
4103 */
4104static inline bool ixgbe_set_sriov_queues(struct ixgbe_adapter *adapter)
4105{
4106 return false;
4107}
4108
4df10466
JB
4109/*
4110 * ixgbe_set_num_queues: Allocate queues for device, feature dependant
4111 * @adapter: board private structure to initialize
4112 *
4113 * This is the top level queue allocation routine. The order here is very
4114 * important, starting with the "most" number of features turned on at once,
4115 * and ending with the smallest set of features. This way large combinations
4116 * can be allocated if they're turned on, and smaller combinations are the
4117 * fallthrough conditions.
4118 *
4119 **/
847f53ff 4120static int ixgbe_set_num_queues(struct ixgbe_adapter *adapter)
bc97114d 4121{
1cdd1ec8
GR
4122 /* Start with base case */
4123 adapter->num_rx_queues = 1;
4124 adapter->num_tx_queues = 1;
4125 adapter->num_rx_pools = adapter->num_rx_queues;
4126 adapter->num_rx_queues_per_pool = 1;
4127
4128 if (ixgbe_set_sriov_queues(adapter))
847f53ff 4129 goto done;
1cdd1ec8 4130
0331a832
YZ
4131#ifdef IXGBE_FCOE
4132 if (ixgbe_set_fcoe_queues(adapter))
4133 goto done;
4134
4135#endif /* IXGBE_FCOE */
bc97114d
PWJ
4136#ifdef CONFIG_IXGBE_DCB
4137 if (ixgbe_set_dcb_queues(adapter))
af22ab1b 4138 goto done;
bc97114d
PWJ
4139
4140#endif
c4cf55e5
PWJ
4141 if (ixgbe_set_fdir_queues(adapter))
4142 goto done;
4143
bc97114d 4144 if (ixgbe_set_rss_queues(adapter))
af22ab1b
WF
4145 goto done;
4146
4147 /* fallback to base case */
4148 adapter->num_rx_queues = 1;
4149 adapter->num_tx_queues = 1;
4150
4151done:
847f53ff 4152 /* Notify the stack of the (possibly) reduced queue counts. */
f0796d5c 4153 netif_set_real_num_tx_queues(adapter->netdev, adapter->num_tx_queues);
847f53ff
BH
4154 return netif_set_real_num_rx_queues(adapter->netdev,
4155 adapter->num_rx_queues);
b9804972
JB
4156}
4157
021230d4 4158static void ixgbe_acquire_msix_vectors(struct ixgbe_adapter *adapter,
e8e9f696 4159 int vectors)
021230d4
AV
4160{
4161 int err, vector_threshold;
4162
4163 /* We'll want at least 3 (vector_threshold):
4164 * 1) TxQ[0] Cleanup
4165 * 2) RxQ[0] Cleanup
4166 * 3) Other (Link Status Change, etc.)
4167 * 4) TCP Timer (optional)
4168 */
4169 vector_threshold = MIN_MSIX_COUNT;
4170
4171 /* The more we get, the more we will assign to Tx/Rx Cleanup
4172 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
4173 * Right now, we simply care about how many we'll get; we'll
4174 * set them up later while requesting irq's.
4175 */
4176 while (vectors >= vector_threshold) {
4177 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
e8e9f696 4178 vectors);
021230d4
AV
4179 if (!err) /* Success in acquiring all requested vectors. */
4180 break;
4181 else if (err < 0)
4182 vectors = 0; /* Nasty failure, quit now */
4183 else /* err == number of vectors we should try again with */
4184 vectors = err;
4185 }
4186
4187 if (vectors < vector_threshold) {
4188 /* Can't allocate enough MSI-X interrupts? Oh well.
4189 * This just means we'll go with either a single MSI
4190 * vector or fall back to legacy interrupts.
4191 */
849c4542
ET
4192 netif_printk(adapter, hw, KERN_DEBUG, adapter->netdev,
4193 "Unable to allocate MSI-X interrupts\n");
021230d4
AV
4194 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
4195 kfree(adapter->msix_entries);
4196 adapter->msix_entries = NULL;
021230d4
AV
4197 } else {
4198 adapter->flags |= IXGBE_FLAG_MSIX_ENABLED; /* Woot! */
eb7f139c
PWJ
4199 /*
4200 * Adjust for only the vectors we'll use, which is minimum
4201 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
4202 * vectors we were allocated.
4203 */
4204 adapter->num_msix_vectors = min(vectors,
e8e9f696 4205 adapter->max_msix_q_vectors + NON_Q_VECTORS);
021230d4
AV
4206 }
4207}
4208
021230d4 4209/**
bc97114d 4210 * ixgbe_cache_ring_rss - Descriptor ring to register mapping for RSS
021230d4
AV
4211 * @adapter: board private structure to initialize
4212 *
bc97114d
PWJ
4213 * Cache the descriptor ring offsets for RSS to the assigned rings.
4214 *
021230d4 4215 **/
bc97114d 4216static inline bool ixgbe_cache_ring_rss(struct ixgbe_adapter *adapter)
021230d4 4217{
bc97114d
PWJ
4218 int i;
4219 bool ret = false;
4220
4221 if (adapter->flags & IXGBE_FLAG_RSS_ENABLED) {
4222 for (i = 0; i < adapter->num_rx_queues; i++)
4a0b9ca0 4223 adapter->rx_ring[i]->reg_idx = i;
bc97114d 4224 for (i = 0; i < adapter->num_tx_queues; i++)
4a0b9ca0 4225 adapter->tx_ring[i]->reg_idx = i;
bc97114d
PWJ
4226 ret = true;
4227 } else {
4228 ret = false;
4229 }
4230
4231 return ret;
4232}
4233
4234#ifdef CONFIG_IXGBE_DCB
4235/**
4236 * ixgbe_cache_ring_dcb - Descriptor ring to register mapping for DCB
4237 * @adapter: board private structure to initialize
4238 *
4239 * Cache the descriptor ring offsets for DCB to the assigned rings.
4240 *
4241 **/
4242static inline bool ixgbe_cache_ring_dcb(struct ixgbe_adapter *adapter)
4243{
4244 int i;
4245 bool ret = false;
4246 int dcb_i = adapter->ring_feature[RING_F_DCB].indices;
4247
4248 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
4249 if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
2f90b865
AD
4250 /* the number of queues is assumed to be symmetric */
4251 for (i = 0; i < dcb_i; i++) {
4a0b9ca0
PW
4252 adapter->rx_ring[i]->reg_idx = i << 3;
4253 adapter->tx_ring[i]->reg_idx = i << 2;
2f90b865 4254 }
bc97114d 4255 ret = true;
e8e26350 4256 } else if (adapter->hw.mac.type == ixgbe_mac_82599EB) {
f92ef202
PW
4257 if (dcb_i == 8) {
4258 /*
4259 * Tx TC0 starts at: descriptor queue 0
4260 * Tx TC1 starts at: descriptor queue 32
4261 * Tx TC2 starts at: descriptor queue 64
4262 * Tx TC3 starts at: descriptor queue 80
4263 * Tx TC4 starts at: descriptor queue 96
4264 * Tx TC5 starts at: descriptor queue 104
4265 * Tx TC6 starts at: descriptor queue 112
4266 * Tx TC7 starts at: descriptor queue 120
4267 *
4268 * Rx TC0-TC7 are offset by 16 queues each
4269 */
4270 for (i = 0; i < 3; i++) {
4a0b9ca0
PW
4271 adapter->tx_ring[i]->reg_idx = i << 5;
4272 adapter->rx_ring[i]->reg_idx = i << 4;
f92ef202
PW
4273 }
4274 for ( ; i < 5; i++) {
4a0b9ca0 4275 adapter->tx_ring[i]->reg_idx =
e8e9f696 4276 ((i + 2) << 4);
4a0b9ca0 4277 adapter->rx_ring[i]->reg_idx = i << 4;
f92ef202
PW
4278 }
4279 for ( ; i < dcb_i; i++) {
4a0b9ca0 4280 adapter->tx_ring[i]->reg_idx =
e8e9f696 4281 ((i + 8) << 3);
4a0b9ca0 4282 adapter->rx_ring[i]->reg_idx = i << 4;
f92ef202
PW
4283 }
4284
4285 ret = true;
4286 } else if (dcb_i == 4) {
4287 /*
4288 * Tx TC0 starts at: descriptor queue 0
4289 * Tx TC1 starts at: descriptor queue 64
4290 * Tx TC2 starts at: descriptor queue 96
4291 * Tx TC3 starts at: descriptor queue 112
4292 *
4293 * Rx TC0-TC3 are offset by 32 queues each
4294 */
4a0b9ca0
PW
4295 adapter->tx_ring[0]->reg_idx = 0;
4296 adapter->tx_ring[1]->reg_idx = 64;
4297 adapter->tx_ring[2]->reg_idx = 96;
4298 adapter->tx_ring[3]->reg_idx = 112;
f92ef202 4299 for (i = 0 ; i < dcb_i; i++)
4a0b9ca0 4300 adapter->rx_ring[i]->reg_idx = i << 5;
f92ef202
PW
4301
4302 ret = true;
4303 } else {
4304 ret = false;
e8e26350 4305 }
bc97114d
PWJ
4306 } else {
4307 ret = false;
021230d4 4308 }
bc97114d
PWJ
4309 } else {
4310 ret = false;
021230d4 4311 }
bc97114d
PWJ
4312
4313 return ret;
4314}
4315#endif
4316
c4cf55e5
PWJ
4317/**
4318 * ixgbe_cache_ring_fdir - Descriptor ring to register mapping for Flow Director
4319 * @adapter: board private structure to initialize
4320 *
4321 * Cache the descriptor ring offsets for Flow Director to the assigned rings.
4322 *
4323 **/
e8e9f696 4324static inline bool ixgbe_cache_ring_fdir(struct ixgbe_adapter *adapter)
c4cf55e5
PWJ
4325{
4326 int i;
4327 bool ret = false;
4328
4329 if (adapter->flags & IXGBE_FLAG_RSS_ENABLED &&
4330 ((adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) ||
4331 (adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE))) {
4332 for (i = 0; i < adapter->num_rx_queues; i++)
4a0b9ca0 4333 adapter->rx_ring[i]->reg_idx = i;
c4cf55e5 4334 for (i = 0; i < adapter->num_tx_queues; i++)
4a0b9ca0 4335 adapter->tx_ring[i]->reg_idx = i;
c4cf55e5
PWJ
4336 ret = true;
4337 }
4338
4339 return ret;
4340}
4341
0331a832
YZ
4342#ifdef IXGBE_FCOE
4343/**
4344 * ixgbe_cache_ring_fcoe - Descriptor ring to register mapping for the FCoE
4345 * @adapter: board private structure to initialize
4346 *
4347 * Cache the descriptor ring offsets for FCoE mode to the assigned rings.
4348 *
4349 */
4350static inline bool ixgbe_cache_ring_fcoe(struct ixgbe_adapter *adapter)
4351{
8de8b2e6 4352 int i, fcoe_rx_i = 0, fcoe_tx_i = 0;
0331a832
YZ
4353 bool ret = false;
4354 struct ixgbe_ring_feature *f = &adapter->ring_feature[RING_F_FCOE];
4355
4356 if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED) {
4357#ifdef CONFIG_IXGBE_DCB
4358 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
8de8b2e6
YZ
4359 struct ixgbe_fcoe *fcoe = &adapter->fcoe;
4360
0331a832 4361 ixgbe_cache_ring_dcb(adapter);
8de8b2e6 4362 /* find out queues in TC for FCoE */
4a0b9ca0
PW
4363 fcoe_rx_i = adapter->rx_ring[fcoe->tc]->reg_idx + 1;
4364 fcoe_tx_i = adapter->tx_ring[fcoe->tc]->reg_idx + 1;
8de8b2e6
YZ
4365 /*
4366 * In 82599, the number of Tx queues for each traffic
4367 * class for both 8-TC and 4-TC modes are:
4368 * TCs : TC0 TC1 TC2 TC3 TC4 TC5 TC6 TC7
4369 * 8 TCs: 32 32 16 16 8 8 8 8
4370 * 4 TCs: 64 64 32 32
4371 * We have max 8 queues for FCoE, where 8 the is
4372 * FCoE redirection table size. If TC for FCoE is
4373 * less than or equal to TC3, we have enough queues
4374 * to add max of 8 queues for FCoE, so we start FCoE
4375 * tx descriptor from the next one, i.e., reg_idx + 1.
4376 * If TC for FCoE is above TC3, implying 8 TC mode,
4377 * and we need 8 for FCoE, we have to take all queues
4378 * in that traffic class for FCoE.
4379 */
4380 if ((f->indices == IXGBE_FCRETA_SIZE) && (fcoe->tc > 3))
4381 fcoe_tx_i--;
0331a832
YZ
4382 }
4383#endif /* CONFIG_IXGBE_DCB */
4384 if (adapter->flags & IXGBE_FLAG_RSS_ENABLED) {
8faa2a78
YZ
4385 if ((adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) ||
4386 (adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE))
4387 ixgbe_cache_ring_fdir(adapter);
4388 else
4389 ixgbe_cache_ring_rss(adapter);
4390
8de8b2e6
YZ
4391 fcoe_rx_i = f->mask;
4392 fcoe_tx_i = f->mask;
4393 }
4394 for (i = 0; i < f->indices; i++, fcoe_rx_i++, fcoe_tx_i++) {
4a0b9ca0
PW
4395 adapter->rx_ring[f->mask + i]->reg_idx = fcoe_rx_i;
4396 adapter->tx_ring[f->mask + i]->reg_idx = fcoe_tx_i;
0331a832 4397 }
0331a832
YZ
4398 ret = true;
4399 }
4400 return ret;
4401}
4402
4403#endif /* IXGBE_FCOE */
1cdd1ec8
GR
4404/**
4405 * ixgbe_cache_ring_sriov - Descriptor ring to register mapping for sriov
4406 * @adapter: board private structure to initialize
4407 *
4408 * SR-IOV doesn't use any descriptor rings but changes the default if
4409 * no other mapping is used.
4410 *
4411 */
4412static inline bool ixgbe_cache_ring_sriov(struct ixgbe_adapter *adapter)
4413{
4a0b9ca0
PW
4414 adapter->rx_ring[0]->reg_idx = adapter->num_vfs * 2;
4415 adapter->tx_ring[0]->reg_idx = adapter->num_vfs * 2;
1cdd1ec8
GR
4416 if (adapter->num_vfs)
4417 return true;
4418 else
4419 return false;
4420}
4421
bc97114d
PWJ
4422/**
4423 * ixgbe_cache_ring_register - Descriptor ring to register mapping
4424 * @adapter: board private structure to initialize
4425 *
4426 * Once we know the feature-set enabled for the device, we'll cache
4427 * the register offset the descriptor ring is assigned to.
4428 *
4429 * Note, the order the various feature calls is important. It must start with
4430 * the "most" features enabled at the same time, then trickle down to the
4431 * least amount of features turned on at once.
4432 **/
4433static void ixgbe_cache_ring_register(struct ixgbe_adapter *adapter)
4434{
4435 /* start with default case */
4a0b9ca0
PW
4436 adapter->rx_ring[0]->reg_idx = 0;
4437 adapter->tx_ring[0]->reg_idx = 0;
bc97114d 4438
1cdd1ec8
GR
4439 if (ixgbe_cache_ring_sriov(adapter))
4440 return;
4441
0331a832
YZ
4442#ifdef IXGBE_FCOE
4443 if (ixgbe_cache_ring_fcoe(adapter))
4444 return;
4445
4446#endif /* IXGBE_FCOE */
bc97114d
PWJ
4447#ifdef CONFIG_IXGBE_DCB
4448 if (ixgbe_cache_ring_dcb(adapter))
4449 return;
4450
4451#endif
c4cf55e5
PWJ
4452 if (ixgbe_cache_ring_fdir(adapter))
4453 return;
4454
bc97114d
PWJ
4455 if (ixgbe_cache_ring_rss(adapter))
4456 return;
021230d4
AV
4457}
4458
9a799d71
AK
4459/**
4460 * ixgbe_alloc_queues - Allocate memory for all rings
4461 * @adapter: board private structure to initialize
4462 *
4463 * We allocate one ring per queue at run-time since we don't know the
4df10466
JB
4464 * number of queues at compile-time. The polling_netdev array is
4465 * intended for Multiqueue, but should work fine with a single queue.
9a799d71 4466 **/
2f90b865 4467static int ixgbe_alloc_queues(struct ixgbe_adapter *adapter)
9a799d71
AK
4468{
4469 int i;
b6ec895e 4470 int rx_count;
4a0b9ca0 4471 int orig_node = adapter->node;
9a799d71 4472
021230d4 4473 for (i = 0; i < adapter->num_tx_queues; i++) {
4a0b9ca0
PW
4474 struct ixgbe_ring *ring = adapter->tx_ring[i];
4475 if (orig_node == -1) {
4476 int cur_node = next_online_node(adapter->node);
4477 if (cur_node == MAX_NUMNODES)
4478 cur_node = first_online_node;
4479 adapter->node = cur_node;
4480 }
4481 ring = kzalloc_node(sizeof(struct ixgbe_ring), GFP_KERNEL,
e8e9f696 4482 adapter->node);
4a0b9ca0
PW
4483 if (!ring)
4484 ring = kzalloc(sizeof(struct ixgbe_ring), GFP_KERNEL);
4485 if (!ring)
4486 goto err_tx_ring_allocation;
4487 ring->count = adapter->tx_ring_count;
4488 ring->queue_index = i;
b6ec895e 4489 ring->dev = &adapter->pdev->dev;
fc77dc3c 4490 ring->netdev = adapter->netdev;
4a0b9ca0
PW
4491 ring->numa_node = adapter->node;
4492
4493 adapter->tx_ring[i] = ring;
021230d4 4494 }
b9804972 4495
4a0b9ca0
PW
4496 /* Restore the adapter's original node */
4497 adapter->node = orig_node;
4498
b6ec895e 4499 rx_count = adapter->rx_ring_count;
9a799d71 4500 for (i = 0; i < adapter->num_rx_queues; i++) {
4a0b9ca0
PW
4501 struct ixgbe_ring *ring = adapter->rx_ring[i];
4502 if (orig_node == -1) {
4503 int cur_node = next_online_node(adapter->node);
4504 if (cur_node == MAX_NUMNODES)
4505 cur_node = first_online_node;
4506 adapter->node = cur_node;
4507 }
4508 ring = kzalloc_node(sizeof(struct ixgbe_ring), GFP_KERNEL,
e8e9f696 4509 adapter->node);
4a0b9ca0
PW
4510 if (!ring)
4511 ring = kzalloc(sizeof(struct ixgbe_ring), GFP_KERNEL);
4512 if (!ring)
4513 goto err_rx_ring_allocation;
b6ec895e 4514 ring->count = rx_count;
4a0b9ca0 4515 ring->queue_index = i;
b6ec895e 4516 ring->dev = &adapter->pdev->dev;
fc77dc3c 4517 ring->netdev = adapter->netdev;
4a0b9ca0
PW
4518 ring->numa_node = adapter->node;
4519
4520 adapter->rx_ring[i] = ring;
021230d4
AV
4521 }
4522
4a0b9ca0
PW
4523 /* Restore the adapter's original node */
4524 adapter->node = orig_node;
4525
021230d4
AV
4526 ixgbe_cache_ring_register(adapter);
4527
4528 return 0;
4529
4530err_rx_ring_allocation:
4a0b9ca0
PW
4531 for (i = 0; i < adapter->num_tx_queues; i++)
4532 kfree(adapter->tx_ring[i]);
021230d4
AV
4533err_tx_ring_allocation:
4534 return -ENOMEM;
4535}
4536
4537/**
4538 * ixgbe_set_interrupt_capability - set MSI-X or MSI if supported
4539 * @adapter: board private structure to initialize
4540 *
4541 * Attempt to configure the interrupts using the best available
4542 * capabilities of the hardware and the kernel.
4543 **/
feea6a57 4544static int ixgbe_set_interrupt_capability(struct ixgbe_adapter *adapter)
021230d4 4545{
8be0e467 4546 struct ixgbe_hw *hw = &adapter->hw;
021230d4
AV
4547 int err = 0;
4548 int vector, v_budget;
4549
4550 /*
4551 * It's easy to be greedy for MSI-X vectors, but it really
4552 * doesn't do us much good if we have a lot more vectors
4553 * than CPU's. So let's be conservative and only ask for
342bde1b 4554 * (roughly) the same number of vectors as there are CPU's.
021230d4
AV
4555 */
4556 v_budget = min(adapter->num_rx_queues + adapter->num_tx_queues,
e8e9f696 4557 (int)num_online_cpus()) + NON_Q_VECTORS;
021230d4
AV
4558
4559 /*
4560 * At the same time, hardware can only support a maximum of
8be0e467
PW
4561 * hw.mac->max_msix_vectors vectors. With features
4562 * such as RSS and VMDq, we can easily surpass the number of Rx and Tx
4563 * descriptor queues supported by our device. Thus, we cap it off in
4564 * those rare cases where the cpu count also exceeds our vector limit.
021230d4 4565 */
8be0e467 4566 v_budget = min(v_budget, (int)hw->mac.max_msix_vectors);
021230d4
AV
4567
4568 /* A failure in MSI-X entry allocation isn't fatal, but it does
4569 * mean we disable MSI-X capabilities of the adapter. */
4570 adapter->msix_entries = kcalloc(v_budget,
e8e9f696 4571 sizeof(struct msix_entry), GFP_KERNEL);
7a921c93
AD
4572 if (adapter->msix_entries) {
4573 for (vector = 0; vector < v_budget; vector++)
4574 adapter->msix_entries[vector].entry = vector;
021230d4 4575
7a921c93 4576 ixgbe_acquire_msix_vectors(adapter, v_budget);
021230d4 4577
7a921c93
AD
4578 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
4579 goto out;
4580 }
26d27844 4581
7a921c93
AD
4582 adapter->flags &= ~IXGBE_FLAG_DCB_ENABLED;
4583 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
c4cf55e5
PWJ
4584 adapter->flags &= ~IXGBE_FLAG_FDIR_HASH_CAPABLE;
4585 adapter->flags &= ~IXGBE_FLAG_FDIR_PERFECT_CAPABLE;
4586 adapter->atr_sample_rate = 0;
1cdd1ec8
GR
4587 if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
4588 ixgbe_disable_sriov(adapter);
4589
847f53ff
BH
4590 err = ixgbe_set_num_queues(adapter);
4591 if (err)
4592 return err;
021230d4 4593
021230d4
AV
4594 err = pci_enable_msi(adapter->pdev);
4595 if (!err) {
4596 adapter->flags |= IXGBE_FLAG_MSI_ENABLED;
4597 } else {
849c4542
ET
4598 netif_printk(adapter, hw, KERN_DEBUG, adapter->netdev,
4599 "Unable to allocate MSI interrupt, "
4600 "falling back to legacy. Error: %d\n", err);
021230d4
AV
4601 /* reset err */
4602 err = 0;
4603 }
4604
4605out:
021230d4
AV
4606 return err;
4607}
4608
7a921c93
AD
4609/**
4610 * ixgbe_alloc_q_vectors - Allocate memory for interrupt vectors
4611 * @adapter: board private structure to initialize
4612 *
4613 * We allocate one q_vector per queue interrupt. If allocation fails we
4614 * return -ENOMEM.
4615 **/
4616static int ixgbe_alloc_q_vectors(struct ixgbe_adapter *adapter)
4617{
4618 int q_idx, num_q_vectors;
4619 struct ixgbe_q_vector *q_vector;
4620 int napi_vectors;
4621 int (*poll)(struct napi_struct *, int);
4622
4623 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
4624 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
4625 napi_vectors = adapter->num_rx_queues;
91281fd3 4626 poll = &ixgbe_clean_rxtx_many;
7a921c93
AD
4627 } else {
4628 num_q_vectors = 1;
4629 napi_vectors = 1;
4630 poll = &ixgbe_poll;
4631 }
4632
4633 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1a6c14a2 4634 q_vector = kzalloc_node(sizeof(struct ixgbe_q_vector),
e8e9f696 4635 GFP_KERNEL, adapter->node);
1a6c14a2
JB
4636 if (!q_vector)
4637 q_vector = kzalloc(sizeof(struct ixgbe_q_vector),
e8e9f696 4638 GFP_KERNEL);
7a921c93
AD
4639 if (!q_vector)
4640 goto err_out;
4641 q_vector->adapter = adapter;
f7554a2b
NS
4642 if (q_vector->txr_count && !q_vector->rxr_count)
4643 q_vector->eitr = adapter->tx_eitr_param;
4644 else
4645 q_vector->eitr = adapter->rx_eitr_param;
fe49f04a 4646 q_vector->v_idx = q_idx;
91281fd3 4647 netif_napi_add(adapter->netdev, &q_vector->napi, (*poll), 64);
7a921c93
AD
4648 adapter->q_vector[q_idx] = q_vector;
4649 }
4650
4651 return 0;
4652
4653err_out:
4654 while (q_idx) {
4655 q_idx--;
4656 q_vector = adapter->q_vector[q_idx];
4657 netif_napi_del(&q_vector->napi);
4658 kfree(q_vector);
4659 adapter->q_vector[q_idx] = NULL;
4660 }
4661 return -ENOMEM;
4662}
4663
4664/**
4665 * ixgbe_free_q_vectors - Free memory allocated for interrupt vectors
4666 * @adapter: board private structure to initialize
4667 *
4668 * This function frees the memory allocated to the q_vectors. In addition if
4669 * NAPI is enabled it will delete any references to the NAPI struct prior
4670 * to freeing the q_vector.
4671 **/
4672static void ixgbe_free_q_vectors(struct ixgbe_adapter *adapter)
4673{
4674 int q_idx, num_q_vectors;
7a921c93 4675
91281fd3 4676 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
7a921c93 4677 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
91281fd3 4678 else
7a921c93 4679 num_q_vectors = 1;
7a921c93
AD
4680
4681 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
4682 struct ixgbe_q_vector *q_vector = adapter->q_vector[q_idx];
7a921c93 4683 adapter->q_vector[q_idx] = NULL;
91281fd3 4684 netif_napi_del(&q_vector->napi);
7a921c93
AD
4685 kfree(q_vector);
4686 }
4687}
4688
7b25cdba 4689static void ixgbe_reset_interrupt_capability(struct ixgbe_adapter *adapter)
021230d4
AV
4690{
4691 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
4692 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
4693 pci_disable_msix(adapter->pdev);
4694 kfree(adapter->msix_entries);
4695 adapter->msix_entries = NULL;
4696 } else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED) {
4697 adapter->flags &= ~IXGBE_FLAG_MSI_ENABLED;
4698 pci_disable_msi(adapter->pdev);
4699 }
021230d4
AV
4700}
4701
4702/**
4703 * ixgbe_init_interrupt_scheme - Determine proper interrupt scheme
4704 * @adapter: board private structure to initialize
4705 *
4706 * We determine which interrupt scheme to use based on...
4707 * - Kernel support (MSI, MSI-X)
4708 * - which can be user-defined (via MODULE_PARAM)
4709 * - Hardware queue count (num_*_queues)
4710 * - defined by miscellaneous hardware support/features (RSS, etc.)
4711 **/
2f90b865 4712int ixgbe_init_interrupt_scheme(struct ixgbe_adapter *adapter)
021230d4
AV
4713{
4714 int err;
4715
4716 /* Number of supported queues */
847f53ff
BH
4717 err = ixgbe_set_num_queues(adapter);
4718 if (err)
4719 return err;
021230d4 4720
021230d4
AV
4721 err = ixgbe_set_interrupt_capability(adapter);
4722 if (err) {
849c4542 4723 e_dev_err("Unable to setup interrupt capabilities\n");
021230d4 4724 goto err_set_interrupt;
9a799d71
AK
4725 }
4726
7a921c93
AD
4727 err = ixgbe_alloc_q_vectors(adapter);
4728 if (err) {
849c4542 4729 e_dev_err("Unable to allocate memory for queue vectors\n");
7a921c93
AD
4730 goto err_alloc_q_vectors;
4731 }
4732
4733 err = ixgbe_alloc_queues(adapter);
4734 if (err) {
849c4542 4735 e_dev_err("Unable to allocate memory for queues\n");
7a921c93
AD
4736 goto err_alloc_queues;
4737 }
4738
849c4542 4739 e_dev_info("Multiqueue %s: Rx Queue count = %u, Tx Queue count = %u\n",
396e799c
ET
4740 (adapter->num_rx_queues > 1) ? "Enabled" : "Disabled",
4741 adapter->num_rx_queues, adapter->num_tx_queues);
021230d4
AV
4742
4743 set_bit(__IXGBE_DOWN, &adapter->state);
4744
9a799d71 4745 return 0;
021230d4 4746
7a921c93
AD
4747err_alloc_queues:
4748 ixgbe_free_q_vectors(adapter);
4749err_alloc_q_vectors:
4750 ixgbe_reset_interrupt_capability(adapter);
021230d4 4751err_set_interrupt:
7a921c93
AD
4752 return err;
4753}
4754
1a51502b
ED
4755static void ring_free_rcu(struct rcu_head *head)
4756{
4757 kfree(container_of(head, struct ixgbe_ring, rcu));
4758}
4759
7a921c93
AD
4760/**
4761 * ixgbe_clear_interrupt_scheme - Clear the current interrupt scheme settings
4762 * @adapter: board private structure to clear interrupt scheme on
4763 *
4764 * We go through and clear interrupt specific resources and reset the structure
4765 * to pre-load conditions
4766 **/
4767void ixgbe_clear_interrupt_scheme(struct ixgbe_adapter *adapter)
4768{
4a0b9ca0
PW
4769 int i;
4770
4771 for (i = 0; i < adapter->num_tx_queues; i++) {
4772 kfree(adapter->tx_ring[i]);
4773 adapter->tx_ring[i] = NULL;
4774 }
4775 for (i = 0; i < adapter->num_rx_queues; i++) {
1a51502b
ED
4776 struct ixgbe_ring *ring = adapter->rx_ring[i];
4777
4778 /* ixgbe_get_stats64() might access this ring, we must wait
4779 * a grace period before freeing it.
4780 */
4781 call_rcu(&ring->rcu, ring_free_rcu);
4a0b9ca0
PW
4782 adapter->rx_ring[i] = NULL;
4783 }
7a921c93
AD
4784
4785 ixgbe_free_q_vectors(adapter);
4786 ixgbe_reset_interrupt_capability(adapter);
9a799d71
AK
4787}
4788
c4900be0
DS
4789/**
4790 * ixgbe_sfp_timer - worker thread to find a missing module
4791 * @data: pointer to our adapter struct
4792 **/
4793static void ixgbe_sfp_timer(unsigned long data)
4794{
4795 struct ixgbe_adapter *adapter = (struct ixgbe_adapter *)data;
4796
4df10466
JB
4797 /*
4798 * Do the sfp_timer outside of interrupt context due to the
c4900be0
DS
4799 * delays that sfp+ detection requires
4800 */
4801 schedule_work(&adapter->sfp_task);
4802}
4803
4804/**
4805 * ixgbe_sfp_task - worker thread to find a missing module
4806 * @work: pointer to work_struct containing our data
4807 **/
4808static void ixgbe_sfp_task(struct work_struct *work)
4809{
4810 struct ixgbe_adapter *adapter = container_of(work,
e8e9f696
JP
4811 struct ixgbe_adapter,
4812 sfp_task);
c4900be0
DS
4813 struct ixgbe_hw *hw = &adapter->hw;
4814
4815 if ((hw->phy.type == ixgbe_phy_nl) &&
4816 (hw->phy.sfp_type == ixgbe_sfp_type_not_present)) {
4817 s32 ret = hw->phy.ops.identify_sfp(hw);
63d6e1d8 4818 if (ret == IXGBE_ERR_SFP_NOT_PRESENT)
c4900be0
DS
4819 goto reschedule;
4820 ret = hw->phy.ops.reset(hw);
4821 if (ret == IXGBE_ERR_SFP_NOT_SUPPORTED) {
849c4542
ET
4822 e_dev_err("failed to initialize because an unsupported "
4823 "SFP+ module type was detected.\n");
4824 e_dev_err("Reload the driver after installing a "
4825 "supported module.\n");
c4900be0
DS
4826 unregister_netdev(adapter->netdev);
4827 } else {
396e799c 4828 e_info(probe, "detected SFP+: %d\n", hw->phy.sfp_type);
c4900be0
DS
4829 }
4830 /* don't need this routine any more */
4831 clear_bit(__IXGBE_SFP_MODULE_NOT_FOUND, &adapter->state);
4832 }
4833 return;
4834reschedule:
4835 if (test_bit(__IXGBE_SFP_MODULE_NOT_FOUND, &adapter->state))
4836 mod_timer(&adapter->sfp_timer,
e8e9f696 4837 round_jiffies(jiffies + (2 * HZ)));
c4900be0
DS
4838}
4839
9a799d71
AK
4840/**
4841 * ixgbe_sw_init - Initialize general software structures (struct ixgbe_adapter)
4842 * @adapter: board private structure to initialize
4843 *
4844 * ixgbe_sw_init initializes the Adapter private data structure.
4845 * Fields are initialized based on PCI device information and
4846 * OS network device settings (MTU size).
4847 **/
4848static int __devinit ixgbe_sw_init(struct ixgbe_adapter *adapter)
4849{
4850 struct ixgbe_hw *hw = &adapter->hw;
4851 struct pci_dev *pdev = adapter->pdev;
9a713e7c 4852 struct net_device *dev = adapter->netdev;
021230d4 4853 unsigned int rss;
7a6b6f51 4854#ifdef CONFIG_IXGBE_DCB
2f90b865
AD
4855 int j;
4856 struct tc_configuration *tc;
4857#endif
16b61beb 4858 int max_frame = dev->mtu + ETH_HLEN + ETH_FCS_LEN;
021230d4 4859
c44ade9e
JB
4860 /* PCI config space info */
4861
4862 hw->vendor_id = pdev->vendor;
4863 hw->device_id = pdev->device;
4864 hw->revision_id = pdev->revision;
4865 hw->subsystem_vendor_id = pdev->subsystem_vendor;
4866 hw->subsystem_device_id = pdev->subsystem_device;
4867
021230d4
AV
4868 /* Set capability flags */
4869 rss = min(IXGBE_MAX_RSS_INDICES, (int)num_online_cpus());
4870 adapter->ring_feature[RING_F_RSS].indices = rss;
4871 adapter->flags |= IXGBE_FLAG_RSS_ENABLED;
2f90b865 4872 adapter->ring_feature[RING_F_DCB].indices = IXGBE_MAX_DCB_INDICES;
bf069c97
DS
4873 if (hw->mac.type == ixgbe_mac_82598EB) {
4874 if (hw->device_id == IXGBE_DEV_ID_82598AT)
4875 adapter->flags |= IXGBE_FLAG_FAN_FAIL_CAPABLE;
e8e26350 4876 adapter->max_msix_q_vectors = MAX_MSIX_Q_VECTORS_82598;
bf069c97 4877 } else if (hw->mac.type == ixgbe_mac_82599EB) {
e8e26350 4878 adapter->max_msix_q_vectors = MAX_MSIX_Q_VECTORS_82599;
0c19d6af
PWJ
4879 adapter->flags2 |= IXGBE_FLAG2_RSC_CAPABLE;
4880 adapter->flags2 |= IXGBE_FLAG2_RSC_ENABLED;
119fc60a
MC
4881 if (hw->device_id == IXGBE_DEV_ID_82599_T3_LOM)
4882 adapter->flags2 |= IXGBE_FLAG2_TEMP_SENSOR_CAPABLE;
9a713e7c
PW
4883 if (dev->features & NETIF_F_NTUPLE) {
4884 /* Flow Director perfect filter enabled */
4885 adapter->flags |= IXGBE_FLAG_FDIR_PERFECT_CAPABLE;
4886 adapter->atr_sample_rate = 0;
4887 spin_lock_init(&adapter->fdir_perfect_lock);
4888 } else {
4889 /* Flow Director hash filters enabled */
4890 adapter->flags |= IXGBE_FLAG_FDIR_HASH_CAPABLE;
4891 adapter->atr_sample_rate = 20;
4892 }
c4cf55e5 4893 adapter->ring_feature[RING_F_FDIR].indices =
e8e9f696 4894 IXGBE_MAX_FDIR_INDICES;
c4cf55e5 4895 adapter->fdir_pballoc = 0;
eacd73f7 4896#ifdef IXGBE_FCOE
0d551589
YZ
4897 adapter->flags |= IXGBE_FLAG_FCOE_CAPABLE;
4898 adapter->flags &= ~IXGBE_FLAG_FCOE_ENABLED;
4899 adapter->ring_feature[RING_F_FCOE].indices = 0;
61a0f421 4900#ifdef CONFIG_IXGBE_DCB
6ee16520
YZ
4901 /* Default traffic class to use for FCoE */
4902 adapter->fcoe.tc = IXGBE_FCOE_DEFTC;
56075a98 4903 adapter->fcoe.up = IXGBE_FCOE_DEFTC;
61a0f421 4904#endif
eacd73f7 4905#endif /* IXGBE_FCOE */
f8212f97 4906 }
2f90b865 4907
7a6b6f51 4908#ifdef CONFIG_IXGBE_DCB
2f90b865
AD
4909 /* Configure DCB traffic classes */
4910 for (j = 0; j < MAX_TRAFFIC_CLASS; j++) {
4911 tc = &adapter->dcb_cfg.tc_config[j];
4912 tc->path[DCB_TX_CONFIG].bwg_id = 0;
4913 tc->path[DCB_TX_CONFIG].bwg_percent = 12 + (j & 1);
4914 tc->path[DCB_RX_CONFIG].bwg_id = 0;
4915 tc->path[DCB_RX_CONFIG].bwg_percent = 12 + (j & 1);
4916 tc->dcb_pfc = pfc_disabled;
4917 }
4918 adapter->dcb_cfg.bw_percentage[DCB_TX_CONFIG][0] = 100;
4919 adapter->dcb_cfg.bw_percentage[DCB_RX_CONFIG][0] = 100;
4920 adapter->dcb_cfg.rx_pba_cfg = pba_equal;
264857b8 4921 adapter->dcb_cfg.pfc_mode_enable = false;
2f90b865
AD
4922 adapter->dcb_cfg.round_robin_enable = false;
4923 adapter->dcb_set_bitmap = 0x00;
4924 ixgbe_copy_dcb_cfg(&adapter->dcb_cfg, &adapter->temp_dcb_cfg,
e8e9f696 4925 adapter->ring_feature[RING_F_DCB].indices);
2f90b865
AD
4926
4927#endif
9a799d71
AK
4928
4929 /* default flow control settings */
cd7664f6 4930 hw->fc.requested_mode = ixgbe_fc_full;
71fd570b 4931 hw->fc.current_mode = ixgbe_fc_full; /* init for ethtool output */
264857b8
PWJ
4932#ifdef CONFIG_DCB
4933 adapter->last_lfc_mode = hw->fc.current_mode;
4934#endif
16b61beb
JF
4935 hw->fc.high_water = FC_HIGH_WATER(max_frame);
4936 hw->fc.low_water = FC_LOW_WATER(max_frame);
2b9ade93
JB
4937 hw->fc.pause_time = IXGBE_DEFAULT_FCPAUSE;
4938 hw->fc.send_xon = true;
71fd570b 4939 hw->fc.disable_fc_autoneg = false;
9a799d71 4940
30efa5a3 4941 /* enable itr by default in dynamic mode */
f7554a2b
NS
4942 adapter->rx_itr_setting = 1;
4943 adapter->rx_eitr_param = 20000;
4944 adapter->tx_itr_setting = 1;
4945 adapter->tx_eitr_param = 10000;
30efa5a3
JB
4946
4947 /* set defaults for eitr in MegaBytes */
4948 adapter->eitr_low = 10;
4949 adapter->eitr_high = 20;
4950
4951 /* set default ring sizes */
4952 adapter->tx_ring_count = IXGBE_DEFAULT_TXD;
4953 adapter->rx_ring_count = IXGBE_DEFAULT_RXD;
4954
9a799d71 4955 /* initialize eeprom parameters */
c44ade9e 4956 if (ixgbe_init_eeprom_params_generic(hw)) {
849c4542 4957 e_dev_err("EEPROM initialization failed\n");
9a799d71
AK
4958 return -EIO;
4959 }
4960
021230d4 4961 /* enable rx csum by default */
9a799d71
AK
4962 adapter->flags |= IXGBE_FLAG_RX_CSUM_ENABLED;
4963
1a6c14a2
JB
4964 /* get assigned NUMA node */
4965 adapter->node = dev_to_node(&pdev->dev);
4966
9a799d71
AK
4967 set_bit(__IXGBE_DOWN, &adapter->state);
4968
4969 return 0;
4970}
4971
4972/**
4973 * ixgbe_setup_tx_resources - allocate Tx resources (Descriptors)
3a581073 4974 * @tx_ring: tx descriptor ring (for a specific queue) to setup
9a799d71
AK
4975 *
4976 * Return 0 on success, negative on failure
4977 **/
b6ec895e 4978int ixgbe_setup_tx_resources(struct ixgbe_ring *tx_ring)
9a799d71 4979{
b6ec895e 4980 struct device *dev = tx_ring->dev;
9a799d71
AK
4981 int size;
4982
3a581073 4983 size = sizeof(struct ixgbe_tx_buffer) * tx_ring->count;
4a0b9ca0 4984 tx_ring->tx_buffer_info = vmalloc_node(size, tx_ring->numa_node);
1a6c14a2
JB
4985 if (!tx_ring->tx_buffer_info)
4986 tx_ring->tx_buffer_info = vmalloc(size);
e01c31a5
JB
4987 if (!tx_ring->tx_buffer_info)
4988 goto err;
3a581073 4989 memset(tx_ring->tx_buffer_info, 0, size);
9a799d71
AK
4990
4991 /* round up to nearest 4K */
12207e49 4992 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
3a581073 4993 tx_ring->size = ALIGN(tx_ring->size, 4096);
9a799d71 4994
b6ec895e 4995 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
1b507730 4996 &tx_ring->dma, GFP_KERNEL);
e01c31a5
JB
4997 if (!tx_ring->desc)
4998 goto err;
9a799d71 4999
3a581073
JB
5000 tx_ring->next_to_use = 0;
5001 tx_ring->next_to_clean = 0;
5002 tx_ring->work_limit = tx_ring->count;
9a799d71 5003 return 0;
e01c31a5
JB
5004
5005err:
5006 vfree(tx_ring->tx_buffer_info);
5007 tx_ring->tx_buffer_info = NULL;
b6ec895e 5008 dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n");
e01c31a5 5009 return -ENOMEM;
9a799d71
AK
5010}
5011
69888674
AD
5012/**
5013 * ixgbe_setup_all_tx_resources - allocate all queues Tx resources
5014 * @adapter: board private structure
5015 *
5016 * If this function returns with an error, then it's possible one or
5017 * more of the rings is populated (while the rest are not). It is the
5018 * callers duty to clean those orphaned rings.
5019 *
5020 * Return 0 on success, negative on failure
5021 **/
5022static int ixgbe_setup_all_tx_resources(struct ixgbe_adapter *adapter)
5023{
5024 int i, err = 0;
5025
5026 for (i = 0; i < adapter->num_tx_queues; i++) {
b6ec895e 5027 err = ixgbe_setup_tx_resources(adapter->tx_ring[i]);
69888674
AD
5028 if (!err)
5029 continue;
396e799c 5030 e_err(probe, "Allocation for Tx Queue %u failed\n", i);
69888674
AD
5031 break;
5032 }
5033
5034 return err;
5035}
5036
9a799d71
AK
5037/**
5038 * ixgbe_setup_rx_resources - allocate Rx resources (Descriptors)
3a581073 5039 * @rx_ring: rx descriptor ring (for a specific queue) to setup
9a799d71
AK
5040 *
5041 * Returns 0 on success, negative on failure
5042 **/
b6ec895e 5043int ixgbe_setup_rx_resources(struct ixgbe_ring *rx_ring)
9a799d71 5044{
b6ec895e 5045 struct device *dev = rx_ring->dev;
021230d4 5046 int size;
9a799d71 5047
3a581073 5048 size = sizeof(struct ixgbe_rx_buffer) * rx_ring->count;
b6ec895e 5049 rx_ring->rx_buffer_info = vmalloc_node(size, rx_ring->numa_node);
1a6c14a2
JB
5050 if (!rx_ring->rx_buffer_info)
5051 rx_ring->rx_buffer_info = vmalloc(size);
b6ec895e
AD
5052 if (!rx_ring->rx_buffer_info)
5053 goto err;
3a581073 5054 memset(rx_ring->rx_buffer_info, 0, size);
9a799d71 5055
9a799d71 5056 /* Round up to nearest 4K */
3a581073
JB
5057 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
5058 rx_ring->size = ALIGN(rx_ring->size, 4096);
9a799d71 5059
b6ec895e 5060 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
1b507730 5061 &rx_ring->dma, GFP_KERNEL);
9a799d71 5062
b6ec895e
AD
5063 if (!rx_ring->desc)
5064 goto err;
9a799d71 5065
3a581073
JB
5066 rx_ring->next_to_clean = 0;
5067 rx_ring->next_to_use = 0;
9a799d71
AK
5068
5069 return 0;
b6ec895e
AD
5070err:
5071 vfree(rx_ring->rx_buffer_info);
5072 rx_ring->rx_buffer_info = NULL;
5073 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n");
177db6ff 5074 return -ENOMEM;
9a799d71
AK
5075}
5076
69888674
AD
5077/**
5078 * ixgbe_setup_all_rx_resources - allocate all queues Rx resources
5079 * @adapter: board private structure
5080 *
5081 * If this function returns with an error, then it's possible one or
5082 * more of the rings is populated (while the rest are not). It is the
5083 * callers duty to clean those orphaned rings.
5084 *
5085 * Return 0 on success, negative on failure
5086 **/
69888674
AD
5087static int ixgbe_setup_all_rx_resources(struct ixgbe_adapter *adapter)
5088{
5089 int i, err = 0;
5090
5091 for (i = 0; i < adapter->num_rx_queues; i++) {
b6ec895e 5092 err = ixgbe_setup_rx_resources(adapter->rx_ring[i]);
69888674
AD
5093 if (!err)
5094 continue;
396e799c 5095 e_err(probe, "Allocation for Rx Queue %u failed\n", i);
69888674
AD
5096 break;
5097 }
5098
5099 return err;
5100}
5101
9a799d71
AK
5102/**
5103 * ixgbe_free_tx_resources - Free Tx Resources per Queue
9a799d71
AK
5104 * @tx_ring: Tx descriptor ring for a specific queue
5105 *
5106 * Free all transmit software resources
5107 **/
b6ec895e 5108void ixgbe_free_tx_resources(struct ixgbe_ring *tx_ring)
9a799d71 5109{
b6ec895e 5110 ixgbe_clean_tx_ring(tx_ring);
9a799d71
AK
5111
5112 vfree(tx_ring->tx_buffer_info);
5113 tx_ring->tx_buffer_info = NULL;
5114
b6ec895e
AD
5115 /* if not set, then don't free */
5116 if (!tx_ring->desc)
5117 return;
5118
5119 dma_free_coherent(tx_ring->dev, tx_ring->size,
5120 tx_ring->desc, tx_ring->dma);
9a799d71
AK
5121
5122 tx_ring->desc = NULL;
5123}
5124
5125/**
5126 * ixgbe_free_all_tx_resources - Free Tx Resources for All Queues
5127 * @adapter: board private structure
5128 *
5129 * Free all transmit software resources
5130 **/
5131static void ixgbe_free_all_tx_resources(struct ixgbe_adapter *adapter)
5132{
5133 int i;
5134
5135 for (i = 0; i < adapter->num_tx_queues; i++)
4a0b9ca0 5136 if (adapter->tx_ring[i]->desc)
b6ec895e 5137 ixgbe_free_tx_resources(adapter->tx_ring[i]);
9a799d71
AK
5138}
5139
5140/**
b4617240 5141 * ixgbe_free_rx_resources - Free Rx Resources
9a799d71
AK
5142 * @rx_ring: ring to clean the resources from
5143 *
5144 * Free all receive software resources
5145 **/
b6ec895e 5146void ixgbe_free_rx_resources(struct ixgbe_ring *rx_ring)
9a799d71 5147{
b6ec895e 5148 ixgbe_clean_rx_ring(rx_ring);
9a799d71
AK
5149
5150 vfree(rx_ring->rx_buffer_info);
5151 rx_ring->rx_buffer_info = NULL;
5152
b6ec895e
AD
5153 /* if not set, then don't free */
5154 if (!rx_ring->desc)
5155 return;
5156
5157 dma_free_coherent(rx_ring->dev, rx_ring->size,
5158 rx_ring->desc, rx_ring->dma);
9a799d71
AK
5159
5160 rx_ring->desc = NULL;
5161}
5162
5163/**
5164 * ixgbe_free_all_rx_resources - Free Rx Resources for All Queues
5165 * @adapter: board private structure
5166 *
5167 * Free all receive software resources
5168 **/
5169static void ixgbe_free_all_rx_resources(struct ixgbe_adapter *adapter)
5170{
5171 int i;
5172
5173 for (i = 0; i < adapter->num_rx_queues; i++)
4a0b9ca0 5174 if (adapter->rx_ring[i]->desc)
b6ec895e 5175 ixgbe_free_rx_resources(adapter->rx_ring[i]);
9a799d71
AK
5176}
5177
9a799d71
AK
5178/**
5179 * ixgbe_change_mtu - Change the Maximum Transfer Unit
5180 * @netdev: network interface device structure
5181 * @new_mtu: new value for maximum frame size
5182 *
5183 * Returns 0 on success, negative on failure
5184 **/
5185static int ixgbe_change_mtu(struct net_device *netdev, int new_mtu)
5186{
5187 struct ixgbe_adapter *adapter = netdev_priv(netdev);
16b61beb 5188 struct ixgbe_hw *hw = &adapter->hw;
9a799d71
AK
5189 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
5190
42c783c5
JB
5191 /* MTU < 68 is an error and causes problems on some kernels */
5192 if ((new_mtu < 68) || (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE))
9a799d71
AK
5193 return -EINVAL;
5194
396e799c 5195 e_info(probe, "changing MTU from %d to %d\n", netdev->mtu, new_mtu);
021230d4 5196 /* must set new MTU before calling down or up */
9a799d71
AK
5197 netdev->mtu = new_mtu;
5198
16b61beb
JF
5199 hw->fc.high_water = FC_HIGH_WATER(max_frame);
5200 hw->fc.low_water = FC_LOW_WATER(max_frame);
5201
d4f80882
AV
5202 if (netif_running(netdev))
5203 ixgbe_reinit_locked(adapter);
9a799d71
AK
5204
5205 return 0;
5206}
5207
5208/**
5209 * ixgbe_open - Called when a network interface is made active
5210 * @netdev: network interface device structure
5211 *
5212 * Returns 0 on success, negative value on failure
5213 *
5214 * The open entry point is called when a network interface is made
5215 * active by the system (IFF_UP). At this point all resources needed
5216 * for transmit and receive operations are allocated, the interrupt
5217 * handler is registered with the OS, the watchdog timer is started,
5218 * and the stack is notified that the interface is ready.
5219 **/
5220static int ixgbe_open(struct net_device *netdev)
5221{
5222 struct ixgbe_adapter *adapter = netdev_priv(netdev);
5223 int err;
4bebfaa5
AK
5224
5225 /* disallow open during test */
5226 if (test_bit(__IXGBE_TESTING, &adapter->state))
5227 return -EBUSY;
9a799d71 5228
54386467
JB
5229 netif_carrier_off(netdev);
5230
9a799d71
AK
5231 /* allocate transmit descriptors */
5232 err = ixgbe_setup_all_tx_resources(adapter);
5233 if (err)
5234 goto err_setup_tx;
5235
9a799d71
AK
5236 /* allocate receive descriptors */
5237 err = ixgbe_setup_all_rx_resources(adapter);
5238 if (err)
5239 goto err_setup_rx;
5240
5241 ixgbe_configure(adapter);
5242
021230d4 5243 err = ixgbe_request_irq(adapter);
9a799d71
AK
5244 if (err)
5245 goto err_req_irq;
5246
9a799d71
AK
5247 err = ixgbe_up_complete(adapter);
5248 if (err)
5249 goto err_up;
5250
d55b53ff
JK
5251 netif_tx_start_all_queues(netdev);
5252
9a799d71
AK
5253 return 0;
5254
5255err_up:
5eba3699 5256 ixgbe_release_hw_control(adapter);
9a799d71
AK
5257 ixgbe_free_irq(adapter);
5258err_req_irq:
9a799d71 5259err_setup_rx:
a20a1199 5260 ixgbe_free_all_rx_resources(adapter);
9a799d71 5261err_setup_tx:
a20a1199 5262 ixgbe_free_all_tx_resources(adapter);
9a799d71
AK
5263 ixgbe_reset(adapter);
5264
5265 return err;
5266}
5267
5268/**
5269 * ixgbe_close - Disables a network interface
5270 * @netdev: network interface device structure
5271 *
5272 * Returns 0, this is not allowed to fail
5273 *
5274 * The close entry point is called when an interface is de-activated
5275 * by the OS. The hardware is still under the drivers control, but
5276 * needs to be disabled. A global MAC reset is issued to stop the
5277 * hardware, and all transmit and receive resources are freed.
5278 **/
5279static int ixgbe_close(struct net_device *netdev)
5280{
5281 struct ixgbe_adapter *adapter = netdev_priv(netdev);
9a799d71
AK
5282
5283 ixgbe_down(adapter);
5284 ixgbe_free_irq(adapter);
5285
5286 ixgbe_free_all_tx_resources(adapter);
5287 ixgbe_free_all_rx_resources(adapter);
5288
5eba3699 5289 ixgbe_release_hw_control(adapter);
9a799d71
AK
5290
5291 return 0;
5292}
5293
b3c8b4ba
AD
5294#ifdef CONFIG_PM
5295static int ixgbe_resume(struct pci_dev *pdev)
5296{
c60fbb00
AD
5297 struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
5298 struct net_device *netdev = adapter->netdev;
b3c8b4ba
AD
5299 u32 err;
5300
5301 pci_set_power_state(pdev, PCI_D0);
5302 pci_restore_state(pdev);
656ab817
DS
5303 /*
5304 * pci_restore_state clears dev->state_saved so call
5305 * pci_save_state to restore it.
5306 */
5307 pci_save_state(pdev);
9ce77666 5308
5309 err = pci_enable_device_mem(pdev);
b3c8b4ba 5310 if (err) {
849c4542 5311 e_dev_err("Cannot enable PCI device from suspend\n");
b3c8b4ba
AD
5312 return err;
5313 }
5314 pci_set_master(pdev);
5315
dd4d8ca6 5316 pci_wake_from_d3(pdev, false);
b3c8b4ba
AD
5317
5318 err = ixgbe_init_interrupt_scheme(adapter);
5319 if (err) {
849c4542 5320 e_dev_err("Cannot initialize interrupts for device\n");
b3c8b4ba
AD
5321 return err;
5322 }
5323
b3c8b4ba
AD
5324 ixgbe_reset(adapter);
5325
495dce12
WJP
5326 IXGBE_WRITE_REG(&adapter->hw, IXGBE_WUS, ~0);
5327
b3c8b4ba 5328 if (netif_running(netdev)) {
c60fbb00 5329 err = ixgbe_open(netdev);
b3c8b4ba
AD
5330 if (err)
5331 return err;
5332 }
5333
5334 netif_device_attach(netdev);
5335
5336 return 0;
5337}
b3c8b4ba 5338#endif /* CONFIG_PM */
9d8d05ae
RW
5339
5340static int __ixgbe_shutdown(struct pci_dev *pdev, bool *enable_wake)
b3c8b4ba 5341{
c60fbb00
AD
5342 struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
5343 struct net_device *netdev = adapter->netdev;
e8e26350
PW
5344 struct ixgbe_hw *hw = &adapter->hw;
5345 u32 ctrl, fctrl;
5346 u32 wufc = adapter->wol;
b3c8b4ba
AD
5347#ifdef CONFIG_PM
5348 int retval = 0;
5349#endif
5350
5351 netif_device_detach(netdev);
5352
5353 if (netif_running(netdev)) {
5354 ixgbe_down(adapter);
5355 ixgbe_free_irq(adapter);
5356 ixgbe_free_all_tx_resources(adapter);
5357 ixgbe_free_all_rx_resources(adapter);
5358 }
b3c8b4ba 5359
5f5ae6fc
AD
5360 ixgbe_clear_interrupt_scheme(adapter);
5361
b3c8b4ba
AD
5362#ifdef CONFIG_PM
5363 retval = pci_save_state(pdev);
5364 if (retval)
5365 return retval;
4df10466 5366
b3c8b4ba 5367#endif
e8e26350
PW
5368 if (wufc) {
5369 ixgbe_set_rx_mode(netdev);
b3c8b4ba 5370
e8e26350
PW
5371 /* turn on all-multi mode if wake on multicast is enabled */
5372 if (wufc & IXGBE_WUFC_MC) {
5373 fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
5374 fctrl |= IXGBE_FCTRL_MPE;
5375 IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
5376 }
5377
5378 ctrl = IXGBE_READ_REG(hw, IXGBE_CTRL);
5379 ctrl |= IXGBE_CTRL_GIO_DIS;
5380 IXGBE_WRITE_REG(hw, IXGBE_CTRL, ctrl);
5381
5382 IXGBE_WRITE_REG(hw, IXGBE_WUFC, wufc);
5383 } else {
5384 IXGBE_WRITE_REG(hw, IXGBE_WUC, 0);
5385 IXGBE_WRITE_REG(hw, IXGBE_WUFC, 0);
5386 }
5387
dd4d8ca6
DS
5388 if (wufc && hw->mac.type == ixgbe_mac_82599EB)
5389 pci_wake_from_d3(pdev, true);
5390 else
5391 pci_wake_from_d3(pdev, false);
b3c8b4ba 5392
9d8d05ae
RW
5393 *enable_wake = !!wufc;
5394
b3c8b4ba
AD
5395 ixgbe_release_hw_control(adapter);
5396
5397 pci_disable_device(pdev);
5398
9d8d05ae
RW
5399 return 0;
5400}
5401
5402#ifdef CONFIG_PM
5403static int ixgbe_suspend(struct pci_dev *pdev, pm_message_t state)
5404{
5405 int retval;
5406 bool wake;
5407
5408 retval = __ixgbe_shutdown(pdev, &wake);
5409 if (retval)
5410 return retval;
5411
5412 if (wake) {
5413 pci_prepare_to_sleep(pdev);
5414 } else {
5415 pci_wake_from_d3(pdev, false);
5416 pci_set_power_state(pdev, PCI_D3hot);
5417 }
b3c8b4ba
AD
5418
5419 return 0;
5420}
9d8d05ae 5421#endif /* CONFIG_PM */
b3c8b4ba
AD
5422
5423static void ixgbe_shutdown(struct pci_dev *pdev)
5424{
9d8d05ae
RW
5425 bool wake;
5426
5427 __ixgbe_shutdown(pdev, &wake);
5428
5429 if (system_state == SYSTEM_POWER_OFF) {
5430 pci_wake_from_d3(pdev, wake);
5431 pci_set_power_state(pdev, PCI_D3hot);
5432 }
b3c8b4ba
AD
5433}
5434
9a799d71
AK
5435/**
5436 * ixgbe_update_stats - Update the board statistics counters.
5437 * @adapter: board private structure
5438 **/
5439void ixgbe_update_stats(struct ixgbe_adapter *adapter)
5440{
2d86f139 5441 struct net_device *netdev = adapter->netdev;
9a799d71 5442 struct ixgbe_hw *hw = &adapter->hw;
5b7da515 5443 struct ixgbe_hw_stats *hwstats = &adapter->stats;
6f11eef7
AV
5444 u64 total_mpc = 0;
5445 u32 i, missed_rx = 0, mpc, bprc, lxon, lxoff, xon_off_tot;
5b7da515
AD
5446 u64 non_eop_descs = 0, restart_queue = 0, tx_busy = 0;
5447 u64 alloc_rx_page_failed = 0, alloc_rx_buff_failed = 0;
5448 u64 bytes = 0, packets = 0;
9a799d71 5449
d08935c2
DS
5450 if (test_bit(__IXGBE_DOWN, &adapter->state) ||
5451 test_bit(__IXGBE_RESETTING, &adapter->state))
5452 return;
5453
94b982b2 5454 if (adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED) {
f8212f97 5455 u64 rsc_count = 0;
94b982b2 5456 u64 rsc_flush = 0;
d51019a4
PW
5457 for (i = 0; i < 16; i++)
5458 adapter->hw_rx_no_dma_resources +=
7ca647bd 5459 IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
94b982b2 5460 for (i = 0; i < adapter->num_rx_queues; i++) {
5b7da515
AD
5461 rsc_count += adapter->rx_ring[i]->rx_stats.rsc_count;
5462 rsc_flush += adapter->rx_ring[i]->rx_stats.rsc_flush;
94b982b2
MC
5463 }
5464 adapter->rsc_total_count = rsc_count;
5465 adapter->rsc_total_flush = rsc_flush;
d51019a4
PW
5466 }
5467
5b7da515
AD
5468 for (i = 0; i < adapter->num_rx_queues; i++) {
5469 struct ixgbe_ring *rx_ring = adapter->rx_ring[i];
5470 non_eop_descs += rx_ring->rx_stats.non_eop_descs;
5471 alloc_rx_page_failed += rx_ring->rx_stats.alloc_rx_page_failed;
5472 alloc_rx_buff_failed += rx_ring->rx_stats.alloc_rx_buff_failed;
5473 bytes += rx_ring->stats.bytes;
5474 packets += rx_ring->stats.packets;
5475 }
5476 adapter->non_eop_descs = non_eop_descs;
5477 adapter->alloc_rx_page_failed = alloc_rx_page_failed;
5478 adapter->alloc_rx_buff_failed = alloc_rx_buff_failed;
5479 netdev->stats.rx_bytes = bytes;
5480 netdev->stats.rx_packets = packets;
5481
5482 bytes = 0;
5483 packets = 0;
7ca3bc58 5484 /* gather some stats to the adapter struct that are per queue */
5b7da515
AD
5485 for (i = 0; i < adapter->num_tx_queues; i++) {
5486 struct ixgbe_ring *tx_ring = adapter->tx_ring[i];
5487 restart_queue += tx_ring->tx_stats.restart_queue;
5488 tx_busy += tx_ring->tx_stats.tx_busy;
5489 bytes += tx_ring->stats.bytes;
5490 packets += tx_ring->stats.packets;
5491 }
eb985f09 5492 adapter->restart_queue = restart_queue;
5b7da515
AD
5493 adapter->tx_busy = tx_busy;
5494 netdev->stats.tx_bytes = bytes;
5495 netdev->stats.tx_packets = packets;
7ca3bc58 5496
7ca647bd 5497 hwstats->crcerrs += IXGBE_READ_REG(hw, IXGBE_CRCERRS);
6f11eef7
AV
5498 for (i = 0; i < 8; i++) {
5499 /* for packet buffers not used, the register should read 0 */
5500 mpc = IXGBE_READ_REG(hw, IXGBE_MPC(i));
5501 missed_rx += mpc;
7ca647bd
JP
5502 hwstats->mpc[i] += mpc;
5503 total_mpc += hwstats->mpc[i];
e8e26350 5504 if (hw->mac.type == ixgbe_mac_82598EB)
7ca647bd
JP
5505 hwstats->rnbc[i] += IXGBE_READ_REG(hw, IXGBE_RNBC(i));
5506 hwstats->qptc[i] += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
5507 hwstats->qbtc[i] += IXGBE_READ_REG(hw, IXGBE_QBTC(i));
5508 hwstats->qprc[i] += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
5509 hwstats->qbrc[i] += IXGBE_READ_REG(hw, IXGBE_QBRC(i));
e8e26350 5510 if (hw->mac.type == ixgbe_mac_82599EB) {
7ca647bd
JP
5511 hwstats->pxonrxc[i] +=
5512 IXGBE_READ_REG(hw, IXGBE_PXONRXCNT(i));
5513 hwstats->pxoffrxc[i] +=
5514 IXGBE_READ_REG(hw, IXGBE_PXOFFRXCNT(i));
5515 hwstats->qprdc[i] += IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
e8e26350 5516 } else {
7ca647bd
JP
5517 hwstats->pxonrxc[i] +=
5518 IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
5519 hwstats->pxoffrxc[i] +=
5520 IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
e8e26350 5521 }
7ca647bd
JP
5522 hwstats->pxontxc[i] += IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
5523 hwstats->pxofftxc[i] += IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
6f11eef7 5524 }
7ca647bd 5525 hwstats->gprc += IXGBE_READ_REG(hw, IXGBE_GPRC);
6f11eef7 5526 /* work around hardware counting issue */
7ca647bd 5527 hwstats->gprc -= missed_rx;
6f11eef7
AV
5528
5529 /* 82598 hardware only has a 32 bit counter in the high register */
e8e26350 5530 if (hw->mac.type == ixgbe_mac_82599EB) {
aad71918 5531 u64 tmp;
7ca647bd 5532 hwstats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCL);
e8e9f696
JP
5533 tmp = IXGBE_READ_REG(hw, IXGBE_GORCH) & 0xF;
5534 /* 4 high bits of GORC */
7ca647bd
JP
5535 hwstats->gorc += (tmp << 32);
5536 hwstats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCL);
e8e9f696
JP
5537 tmp = IXGBE_READ_REG(hw, IXGBE_GOTCH) & 0xF;
5538 /* 4 high bits of GOTC */
7ca647bd
JP
5539 hwstats->gotc += (tmp << 32);
5540 hwstats->tor += IXGBE_READ_REG(hw, IXGBE_TORL);
e8e9f696 5541 IXGBE_READ_REG(hw, IXGBE_TORH); /* to clear */
7ca647bd
JP
5542 hwstats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
5543 hwstats->lxoffrxc += IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
5544 hwstats->fdirmatch += IXGBE_READ_REG(hw, IXGBE_FDIRMATCH);
5545 hwstats->fdirmiss += IXGBE_READ_REG(hw, IXGBE_FDIRMISS);
6d45522c 5546#ifdef IXGBE_FCOE
7ca647bd
JP
5547 hwstats->fccrc += IXGBE_READ_REG(hw, IXGBE_FCCRC);
5548 hwstats->fcoerpdc += IXGBE_READ_REG(hw, IXGBE_FCOERPDC);
5549 hwstats->fcoeprc += IXGBE_READ_REG(hw, IXGBE_FCOEPRC);
5550 hwstats->fcoeptc += IXGBE_READ_REG(hw, IXGBE_FCOEPTC);
5551 hwstats->fcoedwrc += IXGBE_READ_REG(hw, IXGBE_FCOEDWRC);
5552 hwstats->fcoedwtc += IXGBE_READ_REG(hw, IXGBE_FCOEDWTC);
6d45522c 5553#endif /* IXGBE_FCOE */
e8e26350 5554 } else {
7ca647bd
JP
5555 hwstats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
5556 hwstats->lxoffrxc += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
5557 hwstats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCH);
5558 hwstats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCH);
5559 hwstats->tor += IXGBE_READ_REG(hw, IXGBE_TORH);
e8e26350 5560 }
9a799d71 5561 bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
7ca647bd
JP
5562 hwstats->bprc += bprc;
5563 hwstats->mprc += IXGBE_READ_REG(hw, IXGBE_MPRC);
e8e26350 5564 if (hw->mac.type == ixgbe_mac_82598EB)
7ca647bd
JP
5565 hwstats->mprc -= bprc;
5566 hwstats->roc += IXGBE_READ_REG(hw, IXGBE_ROC);
5567 hwstats->prc64 += IXGBE_READ_REG(hw, IXGBE_PRC64);
5568 hwstats->prc127 += IXGBE_READ_REG(hw, IXGBE_PRC127);
5569 hwstats->prc255 += IXGBE_READ_REG(hw, IXGBE_PRC255);
5570 hwstats->prc511 += IXGBE_READ_REG(hw, IXGBE_PRC511);
5571 hwstats->prc1023 += IXGBE_READ_REG(hw, IXGBE_PRC1023);
5572 hwstats->prc1522 += IXGBE_READ_REG(hw, IXGBE_PRC1522);
5573 hwstats->rlec += IXGBE_READ_REG(hw, IXGBE_RLEC);
6f11eef7 5574 lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
7ca647bd 5575 hwstats->lxontxc += lxon;
6f11eef7 5576 lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
7ca647bd
JP
5577 hwstats->lxofftxc += lxoff;
5578 hwstats->ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
5579 hwstats->gptc += IXGBE_READ_REG(hw, IXGBE_GPTC);
5580 hwstats->mptc += IXGBE_READ_REG(hw, IXGBE_MPTC);
6f11eef7
AV
5581 /*
5582 * 82598 errata - tx of flow control packets is included in tx counters
5583 */
5584 xon_off_tot = lxon + lxoff;
7ca647bd
JP
5585 hwstats->gptc -= xon_off_tot;
5586 hwstats->mptc -= xon_off_tot;
5587 hwstats->gotc -= (xon_off_tot * (ETH_ZLEN + ETH_FCS_LEN));
5588 hwstats->ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
5589 hwstats->rfc += IXGBE_READ_REG(hw, IXGBE_RFC);
5590 hwstats->rjc += IXGBE_READ_REG(hw, IXGBE_RJC);
5591 hwstats->tpr += IXGBE_READ_REG(hw, IXGBE_TPR);
5592 hwstats->ptc64 += IXGBE_READ_REG(hw, IXGBE_PTC64);
5593 hwstats->ptc64 -= xon_off_tot;
5594 hwstats->ptc127 += IXGBE_READ_REG(hw, IXGBE_PTC127);
5595 hwstats->ptc255 += IXGBE_READ_REG(hw, IXGBE_PTC255);
5596 hwstats->ptc511 += IXGBE_READ_REG(hw, IXGBE_PTC511);
5597 hwstats->ptc1023 += IXGBE_READ_REG(hw, IXGBE_PTC1023);
5598 hwstats->ptc1522 += IXGBE_READ_REG(hw, IXGBE_PTC1522);
5599 hwstats->bptc += IXGBE_READ_REG(hw, IXGBE_BPTC);
9a799d71
AK
5600
5601 /* Fill out the OS statistics structure */
7ca647bd 5602 netdev->stats.multicast = hwstats->mprc;
9a799d71
AK
5603
5604 /* Rx Errors */
7ca647bd 5605 netdev->stats.rx_errors = hwstats->crcerrs + hwstats->rlec;
2d86f139 5606 netdev->stats.rx_dropped = 0;
7ca647bd
JP
5607 netdev->stats.rx_length_errors = hwstats->rlec;
5608 netdev->stats.rx_crc_errors = hwstats->crcerrs;
2d86f139 5609 netdev->stats.rx_missed_errors = total_mpc;
9a799d71
AK
5610}
5611
5612/**
5613 * ixgbe_watchdog - Timer Call-back
5614 * @data: pointer to adapter cast into an unsigned long
5615 **/
5616static void ixgbe_watchdog(unsigned long data)
5617{
5618 struct ixgbe_adapter *adapter = (struct ixgbe_adapter *)data;
cf8280ee 5619 struct ixgbe_hw *hw = &adapter->hw;
fe49f04a
AD
5620 u64 eics = 0;
5621 int i;
cf8280ee 5622
fe49f04a
AD
5623 /*
5624 * Do the watchdog outside of interrupt context due to the lovely
5625 * delays that some of the newer hardware requires
5626 */
22d5a71b 5627
fe49f04a
AD
5628 if (test_bit(__IXGBE_DOWN, &adapter->state))
5629 goto watchdog_short_circuit;
22d5a71b 5630
fe49f04a
AD
5631 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED)) {
5632 /*
5633 * for legacy and MSI interrupts don't set any bits
5634 * that are enabled for EIAM, because this operation
5635 * would set *both* EIMS and EICS for any bit in EIAM
5636 */
5637 IXGBE_WRITE_REG(hw, IXGBE_EICS,
5638 (IXGBE_EICS_TCP_TIMER | IXGBE_EICS_OTHER));
5639 goto watchdog_reschedule;
5640 }
5641
5642 /* get one bit for every active tx/rx interrupt vector */
5643 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
5644 struct ixgbe_q_vector *qv = adapter->q_vector[i];
5645 if (qv->rxr_count || qv->txr_count)
5646 eics |= ((u64)1 << i);
cf8280ee 5647 }
9a799d71 5648
fe49f04a
AD
5649 /* Cause software interrupt to ensure rx rings are cleaned */
5650 ixgbe_irq_rearm_queues(adapter, eics);
5651
5652watchdog_reschedule:
5653 /* Reset the timer */
5654 mod_timer(&adapter->watchdog_timer, round_jiffies(jiffies + 2 * HZ));
5655
5656watchdog_short_circuit:
cf8280ee
JB
5657 schedule_work(&adapter->watchdog_task);
5658}
5659
e8e26350
PW
5660/**
5661 * ixgbe_multispeed_fiber_task - worker thread to configure multispeed fiber
5662 * @work: pointer to work_struct containing our data
5663 **/
5664static void ixgbe_multispeed_fiber_task(struct work_struct *work)
5665{
5666 struct ixgbe_adapter *adapter = container_of(work,
e8e9f696
JP
5667 struct ixgbe_adapter,
5668 multispeed_fiber_task);
e8e26350
PW
5669 struct ixgbe_hw *hw = &adapter->hw;
5670 u32 autoneg;
8620a103 5671 bool negotiation;
e8e26350
PW
5672
5673 adapter->flags |= IXGBE_FLAG_IN_SFP_LINK_TASK;
a1f25324
MC
5674 autoneg = hw->phy.autoneg_advertised;
5675 if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
8620a103 5676 hw->mac.ops.get_link_capabilities(hw, &autoneg, &negotiation);
1097cd17 5677 hw->mac.autotry_restart = false;
8620a103
MC
5678 if (hw->mac.ops.setup_link)
5679 hw->mac.ops.setup_link(hw, autoneg, negotiation, true);
e8e26350
PW
5680 adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
5681 adapter->flags &= ~IXGBE_FLAG_IN_SFP_LINK_TASK;
5682}
5683
5684/**
5685 * ixgbe_sfp_config_module_task - worker thread to configure a new SFP+ module
5686 * @work: pointer to work_struct containing our data
5687 **/
5688static void ixgbe_sfp_config_module_task(struct work_struct *work)
5689{
5690 struct ixgbe_adapter *adapter = container_of(work,
e8e9f696
JP
5691 struct ixgbe_adapter,
5692 sfp_config_module_task);
e8e26350
PW
5693 struct ixgbe_hw *hw = &adapter->hw;
5694 u32 err;
5695
5696 adapter->flags |= IXGBE_FLAG_IN_SFP_MOD_TASK;
63d6e1d8
DS
5697
5698 /* Time for electrical oscillations to settle down */
5699 msleep(100);
e8e26350 5700 err = hw->phy.ops.identify_sfp(hw);
63d6e1d8 5701
e8e26350 5702 if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
849c4542
ET
5703 e_dev_err("failed to initialize because an unsupported SFP+ "
5704 "module type was detected.\n");
5705 e_dev_err("Reload the driver after installing a supported "
5706 "module.\n");
63d6e1d8 5707 unregister_netdev(adapter->netdev);
e8e26350
PW
5708 return;
5709 }
5710 hw->mac.ops.setup_sfp(hw);
5711
8d1c3c07 5712 if (!(adapter->flags & IXGBE_FLAG_IN_SFP_LINK_TASK))
e8e26350
PW
5713 /* This will also work for DA Twinax connections */
5714 schedule_work(&adapter->multispeed_fiber_task);
5715 adapter->flags &= ~IXGBE_FLAG_IN_SFP_MOD_TASK;
5716}
5717
c4cf55e5
PWJ
5718/**
5719 * ixgbe_fdir_reinit_task - worker thread to reinit FDIR filter table
5720 * @work: pointer to work_struct containing our data
5721 **/
5722static void ixgbe_fdir_reinit_task(struct work_struct *work)
5723{
5724 struct ixgbe_adapter *adapter = container_of(work,
e8e9f696
JP
5725 struct ixgbe_adapter,
5726 fdir_reinit_task);
c4cf55e5
PWJ
5727 struct ixgbe_hw *hw = &adapter->hw;
5728 int i;
5729
5730 if (ixgbe_reinit_fdir_tables_82599(hw) == 0) {
5731 for (i = 0; i < adapter->num_tx_queues; i++)
7d637bcc
AD
5732 set_bit(__IXGBE_TX_FDIR_INIT_DONE,
5733 &(adapter->tx_ring[i]->state));
c4cf55e5 5734 } else {
396e799c 5735 e_err(probe, "failed to finish FDIR re-initialization, "
849c4542 5736 "ignored adding FDIR ATR filters\n");
c4cf55e5
PWJ
5737 }
5738 /* Done FDIR Re-initialization, enable transmits */
5739 netif_tx_start_all_queues(adapter->netdev);
5740}
5741
10eec955
JF
5742static DEFINE_MUTEX(ixgbe_watchdog_lock);
5743
cf8280ee 5744/**
69888674
AD
5745 * ixgbe_watchdog_task - worker thread to bring link up
5746 * @work: pointer to work_struct containing our data
cf8280ee
JB
5747 **/
5748static void ixgbe_watchdog_task(struct work_struct *work)
5749{
5750 struct ixgbe_adapter *adapter = container_of(work,
e8e9f696
JP
5751 struct ixgbe_adapter,
5752 watchdog_task);
cf8280ee
JB
5753 struct net_device *netdev = adapter->netdev;
5754 struct ixgbe_hw *hw = &adapter->hw;
10eec955
JF
5755 u32 link_speed;
5756 bool link_up;
bc59fcda
NS
5757 int i;
5758 struct ixgbe_ring *tx_ring;
5759 int some_tx_pending = 0;
cf8280ee 5760
10eec955
JF
5761 mutex_lock(&ixgbe_watchdog_lock);
5762
5763 link_up = adapter->link_up;
5764 link_speed = adapter->link_speed;
cf8280ee
JB
5765
5766 if (adapter->flags & IXGBE_FLAG_NEED_LINK_UPDATE) {
5767 hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
264857b8
PWJ
5768 if (link_up) {
5769#ifdef CONFIG_DCB
5770 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
5771 for (i = 0; i < MAX_TRAFFIC_CLASS; i++)
620fa036 5772 hw->mac.ops.fc_enable(hw, i);
264857b8 5773 } else {
620fa036 5774 hw->mac.ops.fc_enable(hw, 0);
264857b8
PWJ
5775 }
5776#else
620fa036 5777 hw->mac.ops.fc_enable(hw, 0);
264857b8
PWJ
5778#endif
5779 }
5780
cf8280ee
JB
5781 if (link_up ||
5782 time_after(jiffies, (adapter->link_check_timeout +
e8e9f696 5783 IXGBE_TRY_LINK_TIMEOUT))) {
cf8280ee 5784 adapter->flags &= ~IXGBE_FLAG_NEED_LINK_UPDATE;
264857b8 5785 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMC_LSC);
cf8280ee
JB
5786 }
5787 adapter->link_up = link_up;
5788 adapter->link_speed = link_speed;
5789 }
9a799d71
AK
5790
5791 if (link_up) {
5792 if (!netif_carrier_ok(netdev)) {
e8e26350
PW
5793 bool flow_rx, flow_tx;
5794
5795 if (hw->mac.type == ixgbe_mac_82599EB) {
5796 u32 mflcn = IXGBE_READ_REG(hw, IXGBE_MFLCN);
5797 u32 fccfg = IXGBE_READ_REG(hw, IXGBE_FCCFG);
078788b6
PWJ
5798 flow_rx = !!(mflcn & IXGBE_MFLCN_RFCE);
5799 flow_tx = !!(fccfg & IXGBE_FCCFG_TFCE_802_3X);
e8e26350
PW
5800 } else {
5801 u32 frctl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
5802 u32 rmcs = IXGBE_READ_REG(hw, IXGBE_RMCS);
078788b6
PWJ
5803 flow_rx = !!(frctl & IXGBE_FCTRL_RFCE);
5804 flow_tx = !!(rmcs & IXGBE_RMCS_TFCE_802_3X);
e8e26350
PW
5805 }
5806
396e799c 5807 e_info(drv, "NIC Link is Up %s, Flow Control: %s\n",
a46e534b 5808 (link_speed == IXGBE_LINK_SPEED_10GB_FULL ?
849c4542
ET
5809 "10 Gbps" :
5810 (link_speed == IXGBE_LINK_SPEED_1GB_FULL ?
5811 "1 Gbps" : "unknown speed")),
e8e26350 5812 ((flow_rx && flow_tx) ? "RX/TX" :
849c4542
ET
5813 (flow_rx ? "RX" :
5814 (flow_tx ? "TX" : "None"))));
9a799d71
AK
5815
5816 netif_carrier_on(netdev);
9a799d71
AK
5817 } else {
5818 /* Force detection of hung controller */
7d637bcc
AD
5819 for (i = 0; i < adapter->num_tx_queues; i++) {
5820 tx_ring = adapter->tx_ring[i];
5821 set_check_for_tx_hang(tx_ring);
5822 }
9a799d71
AK
5823 }
5824 } else {
cf8280ee
JB
5825 adapter->link_up = false;
5826 adapter->link_speed = 0;
9a799d71 5827 if (netif_carrier_ok(netdev)) {
396e799c 5828 e_info(drv, "NIC Link is Down\n");
9a799d71 5829 netif_carrier_off(netdev);
9a799d71
AK
5830 }
5831 }
5832
bc59fcda
NS
5833 if (!netif_carrier_ok(netdev)) {
5834 for (i = 0; i < adapter->num_tx_queues; i++) {
4a0b9ca0 5835 tx_ring = adapter->tx_ring[i];
bc59fcda
NS
5836 if (tx_ring->next_to_use != tx_ring->next_to_clean) {
5837 some_tx_pending = 1;
5838 break;
5839 }
5840 }
5841
5842 if (some_tx_pending) {
5843 /* We've lost link, so the controller stops DMA,
5844 * but we've got queued Tx work that's never going
5845 * to get done, so reset controller to flush Tx.
5846 * (Do the reset outside of interrupt context).
5847 */
5848 schedule_work(&adapter->reset_task);
5849 }
5850 }
5851
9a799d71 5852 ixgbe_update_stats(adapter);
10eec955 5853 mutex_unlock(&ixgbe_watchdog_lock);
9a799d71
AK
5854}
5855
9a799d71 5856static int ixgbe_tso(struct ixgbe_adapter *adapter,
e8e9f696 5857 struct ixgbe_ring *tx_ring, struct sk_buff *skb,
5e09a105 5858 u32 tx_flags, u8 *hdr_len, __be16 protocol)
9a799d71
AK
5859{
5860 struct ixgbe_adv_tx_context_desc *context_desc;
5861 unsigned int i;
5862 int err;
5863 struct ixgbe_tx_buffer *tx_buffer_info;
9f8cdf4f
JB
5864 u32 vlan_macip_lens = 0, type_tucmd_mlhl;
5865 u32 mss_l4len_idx, l4len;
9a799d71
AK
5866
5867 if (skb_is_gso(skb)) {
5868 if (skb_header_cloned(skb)) {
5869 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5870 if (err)
5871 return err;
5872 }
5873 l4len = tcp_hdrlen(skb);
5874 *hdr_len += l4len;
5875
5e09a105 5876 if (protocol == htons(ETH_P_IP)) {
9a799d71
AK
5877 struct iphdr *iph = ip_hdr(skb);
5878 iph->tot_len = 0;
5879 iph->check = 0;
5880 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
e8e9f696
JP
5881 iph->daddr, 0,
5882 IPPROTO_TCP,
5883 0);
8e1e8a47 5884 } else if (skb_is_gso_v6(skb)) {
9a799d71
AK
5885 ipv6_hdr(skb)->payload_len = 0;
5886 tcp_hdr(skb)->check =
5887 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
e8e9f696
JP
5888 &ipv6_hdr(skb)->daddr,
5889 0, IPPROTO_TCP, 0);
9a799d71
AK
5890 }
5891
5892 i = tx_ring->next_to_use;
5893
5894 tx_buffer_info = &tx_ring->tx_buffer_info[i];
31f05a2d 5895 context_desc = IXGBE_TX_CTXTDESC_ADV(tx_ring, i);
9a799d71
AK
5896
5897 /* VLAN MACLEN IPLEN */
5898 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
5899 vlan_macip_lens |=
5900 (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
5901 vlan_macip_lens |= ((skb_network_offset(skb)) <<
e8e9f696 5902 IXGBE_ADVTXD_MACLEN_SHIFT);
9a799d71
AK
5903 *hdr_len += skb_network_offset(skb);
5904 vlan_macip_lens |=
5905 (skb_transport_header(skb) - skb_network_header(skb));
5906 *hdr_len +=
5907 (skb_transport_header(skb) - skb_network_header(skb));
5908 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
5909 context_desc->seqnum_seed = 0;
5910
5911 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
9f8cdf4f 5912 type_tucmd_mlhl = (IXGBE_TXD_CMD_DEXT |
e8e9f696 5913 IXGBE_ADVTXD_DTYP_CTXT);
9a799d71 5914
5e09a105 5915 if (protocol == htons(ETH_P_IP))
9a799d71
AK
5916 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
5917 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
5918 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
5919
5920 /* MSS L4LEN IDX */
9f8cdf4f 5921 mss_l4len_idx =
9a799d71
AK
5922 (skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT);
5923 mss_l4len_idx |= (l4len << IXGBE_ADVTXD_L4LEN_SHIFT);
4eeae6fd
PW
5924 /* use index 1 for TSO */
5925 mss_l4len_idx |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
9a799d71
AK
5926 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
5927
5928 tx_buffer_info->time_stamp = jiffies;
5929 tx_buffer_info->next_to_watch = i;
5930
5931 i++;
5932 if (i == tx_ring->count)
5933 i = 0;
5934 tx_ring->next_to_use = i;
5935
5936 return true;
5937 }
5938 return false;
5939}
5940
5e09a105
HZ
5941static u32 ixgbe_psum(struct ixgbe_adapter *adapter, struct sk_buff *skb,
5942 __be16 protocol)
7ca647bd
JP
5943{
5944 u32 rtn = 0;
7ca647bd
JP
5945
5946 switch (protocol) {
5947 case cpu_to_be16(ETH_P_IP):
5948 rtn |= IXGBE_ADVTXD_TUCMD_IPV4;
5949 switch (ip_hdr(skb)->protocol) {
5950 case IPPROTO_TCP:
5951 rtn |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
5952 break;
5953 case IPPROTO_SCTP:
5954 rtn |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
5955 break;
5956 }
5957 break;
5958 case cpu_to_be16(ETH_P_IPV6):
5959 /* XXX what about other V6 headers?? */
5960 switch (ipv6_hdr(skb)->nexthdr) {
5961 case IPPROTO_TCP:
5962 rtn |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
5963 break;
5964 case IPPROTO_SCTP:
5965 rtn |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
5966 break;
5967 }
5968 break;
5969 default:
5970 if (unlikely(net_ratelimit()))
5971 e_warn(probe, "partial checksum but proto=%x!\n",
5e09a105 5972 protocol);
7ca647bd
JP
5973 break;
5974 }
5975
5976 return rtn;
5977}
5978
9a799d71 5979static bool ixgbe_tx_csum(struct ixgbe_adapter *adapter,
e8e9f696 5980 struct ixgbe_ring *tx_ring,
5e09a105
HZ
5981 struct sk_buff *skb, u32 tx_flags,
5982 __be16 protocol)
9a799d71
AK
5983{
5984 struct ixgbe_adv_tx_context_desc *context_desc;
5985 unsigned int i;
5986 struct ixgbe_tx_buffer *tx_buffer_info;
5987 u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
5988
5989 if (skb->ip_summed == CHECKSUM_PARTIAL ||
5990 (tx_flags & IXGBE_TX_FLAGS_VLAN)) {
5991 i = tx_ring->next_to_use;
5992 tx_buffer_info = &tx_ring->tx_buffer_info[i];
31f05a2d 5993 context_desc = IXGBE_TX_CTXTDESC_ADV(tx_ring, i);
9a799d71
AK
5994
5995 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
5996 vlan_macip_lens |=
5997 (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
5998 vlan_macip_lens |= (skb_network_offset(skb) <<
e8e9f696 5999 IXGBE_ADVTXD_MACLEN_SHIFT);
9a799d71
AK
6000 if (skb->ip_summed == CHECKSUM_PARTIAL)
6001 vlan_macip_lens |= (skb_transport_header(skb) -
e8e9f696 6002 skb_network_header(skb));
9a799d71
AK
6003
6004 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
6005 context_desc->seqnum_seed = 0;
6006
6007 type_tucmd_mlhl |= (IXGBE_TXD_CMD_DEXT |
e8e9f696 6008 IXGBE_ADVTXD_DTYP_CTXT);
9a799d71 6009
7ca647bd 6010 if (skb->ip_summed == CHECKSUM_PARTIAL)
5e09a105 6011 type_tucmd_mlhl |= ixgbe_psum(adapter, skb, protocol);
9a799d71
AK
6012
6013 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
4eeae6fd 6014 /* use index zero for tx checksum offload */
9a799d71
AK
6015 context_desc->mss_l4len_idx = 0;
6016
6017 tx_buffer_info->time_stamp = jiffies;
6018 tx_buffer_info->next_to_watch = i;
9f8cdf4f 6019
9a799d71
AK
6020 i++;
6021 if (i == tx_ring->count)
6022 i = 0;
6023 tx_ring->next_to_use = i;
6024
6025 return true;
6026 }
9f8cdf4f 6027
9a799d71
AK
6028 return false;
6029}
6030
6031static int ixgbe_tx_map(struct ixgbe_adapter *adapter,
e8e9f696
JP
6032 struct ixgbe_ring *tx_ring,
6033 struct sk_buff *skb, u32 tx_flags,
8ad494b0 6034 unsigned int first, const u8 hdr_len)
9a799d71 6035{
b6ec895e 6036 struct device *dev = tx_ring->dev;
9a799d71 6037 struct ixgbe_tx_buffer *tx_buffer_info;
eacd73f7
YZ
6038 unsigned int len;
6039 unsigned int total = skb->len;
9a799d71
AK
6040 unsigned int offset = 0, size, count = 0, i;
6041 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
6042 unsigned int f;
8ad494b0
AD
6043 unsigned int bytecount = skb->len;
6044 u16 gso_segs = 1;
9a799d71
AK
6045
6046 i = tx_ring->next_to_use;
6047
eacd73f7
YZ
6048 if (tx_flags & IXGBE_TX_FLAGS_FCOE)
6049 /* excluding fcoe_crc_eof for FCoE */
6050 total -= sizeof(struct fcoe_crc_eof);
6051
6052 len = min(skb_headlen(skb), total);
9a799d71
AK
6053 while (len) {
6054 tx_buffer_info = &tx_ring->tx_buffer_info[i];
6055 size = min(len, (uint)IXGBE_MAX_DATA_PER_TXD);
6056
6057 tx_buffer_info->length = size;
e5a43549 6058 tx_buffer_info->mapped_as_page = false;
b6ec895e 6059 tx_buffer_info->dma = dma_map_single(dev,
e5a43549 6060 skb->data + offset,
1b507730 6061 size, DMA_TO_DEVICE);
b6ec895e 6062 if (dma_mapping_error(dev, tx_buffer_info->dma))
e5a43549 6063 goto dma_error;
9a799d71
AK
6064 tx_buffer_info->time_stamp = jiffies;
6065 tx_buffer_info->next_to_watch = i;
6066
6067 len -= size;
eacd73f7 6068 total -= size;
9a799d71
AK
6069 offset += size;
6070 count++;
44df32c5
AD
6071
6072 if (len) {
6073 i++;
6074 if (i == tx_ring->count)
6075 i = 0;
6076 }
9a799d71
AK
6077 }
6078
6079 for (f = 0; f < nr_frags; f++) {
6080 struct skb_frag_struct *frag;
6081
6082 frag = &skb_shinfo(skb)->frags[f];
eacd73f7 6083 len = min((unsigned int)frag->size, total);
e5a43549 6084 offset = frag->page_offset;
9a799d71
AK
6085
6086 while (len) {
44df32c5
AD
6087 i++;
6088 if (i == tx_ring->count)
6089 i = 0;
6090
9a799d71
AK
6091 tx_buffer_info = &tx_ring->tx_buffer_info[i];
6092 size = min(len, (uint)IXGBE_MAX_DATA_PER_TXD);
6093
6094 tx_buffer_info->length = size;
b6ec895e 6095 tx_buffer_info->dma = dma_map_page(dev,
e5a43549
AD
6096 frag->page,
6097 offset, size,
1b507730 6098 DMA_TO_DEVICE);
e5a43549 6099 tx_buffer_info->mapped_as_page = true;
b6ec895e 6100 if (dma_mapping_error(dev, tx_buffer_info->dma))
e5a43549 6101 goto dma_error;
9a799d71
AK
6102 tx_buffer_info->time_stamp = jiffies;
6103 tx_buffer_info->next_to_watch = i;
6104
6105 len -= size;
eacd73f7 6106 total -= size;
9a799d71
AK
6107 offset += size;
6108 count++;
9a799d71 6109 }
eacd73f7
YZ
6110 if (total == 0)
6111 break;
9a799d71 6112 }
44df32c5 6113
8ad494b0
AD
6114 if (tx_flags & IXGBE_TX_FLAGS_TSO)
6115 gso_segs = skb_shinfo(skb)->gso_segs;
6116#ifdef IXGBE_FCOE
6117 /* adjust for FCoE Sequence Offload */
6118 else if (tx_flags & IXGBE_TX_FLAGS_FSO)
6119 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
6120 skb_shinfo(skb)->gso_size);
6121#endif /* IXGBE_FCOE */
6122 bytecount += (gso_segs - 1) * hdr_len;
6123
6124 /* multiply data chunks by size of headers */
6125 tx_ring->tx_buffer_info[i].bytecount = bytecount;
6126 tx_ring->tx_buffer_info[i].gso_segs = gso_segs;
9a799d71
AK
6127 tx_ring->tx_buffer_info[i].skb = skb;
6128 tx_ring->tx_buffer_info[first].next_to_watch = i;
6129
e5a43549
AD
6130 return count;
6131
6132dma_error:
849c4542 6133 e_dev_err("TX DMA map failed\n");
e5a43549
AD
6134
6135 /* clear timestamp and dma mappings for failed tx_buffer_info map */
6136 tx_buffer_info->dma = 0;
6137 tx_buffer_info->time_stamp = 0;
6138 tx_buffer_info->next_to_watch = 0;
c1fa347f
RK
6139 if (count)
6140 count--;
e5a43549
AD
6141
6142 /* clear timestamp and dma mappings for remaining portion of packet */
c1fa347f 6143 while (count--) {
e8e9f696 6144 if (i == 0)
e5a43549 6145 i += tx_ring->count;
c1fa347f 6146 i--;
e5a43549 6147 tx_buffer_info = &tx_ring->tx_buffer_info[i];
b6ec895e 6148 ixgbe_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
e5a43549
AD
6149 }
6150
e44d38e1 6151 return 0;
9a799d71
AK
6152}
6153
84ea2591 6154static void ixgbe_tx_queue(struct ixgbe_ring *tx_ring,
e8e9f696 6155 int tx_flags, int count, u32 paylen, u8 hdr_len)
9a799d71
AK
6156{
6157 union ixgbe_adv_tx_desc *tx_desc = NULL;
6158 struct ixgbe_tx_buffer *tx_buffer_info;
6159 u32 olinfo_status = 0, cmd_type_len = 0;
6160 unsigned int i;
6161 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
6162
6163 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
6164
6165 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
6166
6167 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
6168 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
6169
6170 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
6171 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
6172
6173 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
e8e9f696 6174 IXGBE_ADVTXD_POPTS_SHIFT;
9a799d71 6175
4eeae6fd
PW
6176 /* use index 1 context for tso */
6177 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
9a799d71
AK
6178 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
6179 olinfo_status |= IXGBE_TXD_POPTS_IXSM <<
e8e9f696 6180 IXGBE_ADVTXD_POPTS_SHIFT;
9a799d71
AK
6181
6182 } else if (tx_flags & IXGBE_TX_FLAGS_CSUM)
6183 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
e8e9f696 6184 IXGBE_ADVTXD_POPTS_SHIFT;
9a799d71 6185
eacd73f7
YZ
6186 if (tx_flags & IXGBE_TX_FLAGS_FCOE) {
6187 olinfo_status |= IXGBE_ADVTXD_CC;
6188 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
6189 if (tx_flags & IXGBE_TX_FLAGS_FSO)
6190 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
6191 }
6192
9a799d71
AK
6193 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
6194
6195 i = tx_ring->next_to_use;
6196 while (count--) {
6197 tx_buffer_info = &tx_ring->tx_buffer_info[i];
31f05a2d 6198 tx_desc = IXGBE_TX_DESC_ADV(tx_ring, i);
9a799d71
AK
6199 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
6200 tx_desc->read.cmd_type_len =
e8e9f696 6201 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
9a799d71 6202 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
9a799d71
AK
6203 i++;
6204 if (i == tx_ring->count)
6205 i = 0;
6206 }
6207
6208 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
6209
6210 /*
6211 * Force memory writes to complete before letting h/w
6212 * know there are new descriptors to fetch. (Only
6213 * applicable for weak-ordered memory model archs,
6214 * such as IA-64).
6215 */
6216 wmb();
6217
6218 tx_ring->next_to_use = i;
84ea2591 6219 writel(i, tx_ring->tail);
9a799d71
AK
6220}
6221
c4cf55e5 6222static void ixgbe_atr(struct ixgbe_adapter *adapter, struct sk_buff *skb,
5e09a105 6223 int queue, u32 tx_flags, __be16 protocol)
c4cf55e5 6224{
c4cf55e5
PWJ
6225 struct ixgbe_atr_input atr_input;
6226 struct tcphdr *th;
c4cf55e5
PWJ
6227 struct iphdr *iph = ip_hdr(skb);
6228 struct ethhdr *eth = (struct ethhdr *)skb->data;
6229 u16 vlan_id, src_port, dst_port, flex_bytes;
6230 u32 src_ipv4_addr, dst_ipv4_addr;
6231 u8 l4type = 0;
6232
d3ead241 6233 /* Right now, we support IPv4 only */
5e09a105 6234 if (protocol != htons(ETH_P_IP))
d3ead241 6235 return;
c4cf55e5
PWJ
6236 /* check if we're UDP or TCP */
6237 if (iph->protocol == IPPROTO_TCP) {
6238 th = tcp_hdr(skb);
6239 src_port = th->source;
6240 dst_port = th->dest;
6241 l4type |= IXGBE_ATR_L4TYPE_TCP;
6242 /* l4type IPv4 type is 0, no need to assign */
c4cf55e5
PWJ
6243 } else {
6244 /* Unsupported L4 header, just bail here */
6245 return;
6246 }
6247
6248 memset(&atr_input, 0, sizeof(struct ixgbe_atr_input));
6249
6250 vlan_id = (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK) >>
e8e9f696 6251 IXGBE_TX_FLAGS_VLAN_SHIFT;
c4cf55e5
PWJ
6252 src_ipv4_addr = iph->saddr;
6253 dst_ipv4_addr = iph->daddr;
6254 flex_bytes = eth->h_proto;
6255
6256 ixgbe_atr_set_vlan_id_82599(&atr_input, vlan_id);
6257 ixgbe_atr_set_src_port_82599(&atr_input, dst_port);
6258 ixgbe_atr_set_dst_port_82599(&atr_input, src_port);
6259 ixgbe_atr_set_flex_byte_82599(&atr_input, flex_bytes);
6260 ixgbe_atr_set_l4type_82599(&atr_input, l4type);
6261 /* src and dst are inverted, think how the receiver sees them */
6262 ixgbe_atr_set_src_ipv4_82599(&atr_input, dst_ipv4_addr);
6263 ixgbe_atr_set_dst_ipv4_82599(&atr_input, src_ipv4_addr);
6264
6265 /* This assumes the Rx queue and Tx queue are bound to the same CPU */
6266 ixgbe_fdir_add_signature_filter_82599(&adapter->hw, &atr_input, queue);
6267}
6268
fc77dc3c 6269static int __ixgbe_maybe_stop_tx(struct ixgbe_ring *tx_ring, int size)
e092be60 6270{
fc77dc3c 6271 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
e092be60
AV
6272 /* Herbert's original patch had:
6273 * smp_mb__after_netif_stop_queue();
6274 * but since that doesn't exist yet, just open code it. */
6275 smp_mb();
6276
6277 /* We need to check again in a case another CPU has just
6278 * made room available. */
6279 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
6280 return -EBUSY;
6281
6282 /* A reprieve! - use start_queue because it doesn't call schedule */
fc77dc3c 6283 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
5b7da515 6284 ++tx_ring->tx_stats.restart_queue;
e092be60
AV
6285 return 0;
6286}
6287
fc77dc3c 6288static int ixgbe_maybe_stop_tx(struct ixgbe_ring *tx_ring, int size)
e092be60
AV
6289{
6290 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
6291 return 0;
fc77dc3c 6292 return __ixgbe_maybe_stop_tx(tx_ring, size);
e092be60
AV
6293}
6294
09a3b1f8
SH
6295static u16 ixgbe_select_queue(struct net_device *dev, struct sk_buff *skb)
6296{
6297 struct ixgbe_adapter *adapter = netdev_priv(dev);
5f715823 6298 int txq = smp_processor_id();
56075a98 6299#ifdef IXGBE_FCOE
5e09a105
HZ
6300 __be16 protocol;
6301
6302 protocol = vlan_get_protocol(skb);
6303
6304 if ((protocol == htons(ETH_P_FCOE)) ||
6305 (protocol == htons(ETH_P_FIP))) {
56075a98
JF
6306 if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED) {
6307 txq &= (adapter->ring_feature[RING_F_FCOE].indices - 1);
6308 txq += adapter->ring_feature[RING_F_FCOE].mask;
6309 return txq;
4bc091d8 6310#ifdef CONFIG_IXGBE_DCB
56075a98
JF
6311 } else if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
6312 txq = adapter->fcoe.up;
6313 return txq;
4bc091d8 6314#endif
56075a98
JF
6315 }
6316 }
6317#endif
6318
fdd3d631
KK
6319 if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE) {
6320 while (unlikely(txq >= dev->real_num_tx_queues))
6321 txq -= dev->real_num_tx_queues;
5f715823 6322 return txq;
fdd3d631 6323 }
c4cf55e5 6324
2ea186ae
JF
6325 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
6326 if (skb->priority == TC_PRIO_CONTROL)
6327 txq = adapter->ring_feature[RING_F_DCB].indices-1;
6328 else
6329 txq = (skb->vlan_tci & IXGBE_TX_FLAGS_VLAN_PRIO_MASK)
6330 >> 13;
6331 return txq;
6332 }
09a3b1f8
SH
6333
6334 return skb_tx_hash(dev, skb);
6335}
6336
fc77dc3c 6337netdev_tx_t ixgbe_xmit_frame_ring(struct sk_buff *skb,
84418e3b
AD
6338 struct ixgbe_adapter *adapter,
6339 struct ixgbe_ring *tx_ring)
9a799d71 6340{
fc77dc3c 6341 struct net_device *netdev = tx_ring->netdev;
60d51134 6342 struct netdev_queue *txq;
9a799d71
AK
6343 unsigned int first;
6344 unsigned int tx_flags = 0;
30eba97a 6345 u8 hdr_len = 0;
5f715823 6346 int tso;
9a799d71
AK
6347 int count = 0;
6348 unsigned int f;
5e09a105
HZ
6349 __be16 protocol;
6350
6351 protocol = vlan_get_protocol(skb);
9f8cdf4f 6352
eab6d18d 6353 if (vlan_tx_tag_present(skb)) {
9f8cdf4f 6354 tx_flags |= vlan_tx_tag_get(skb);
2f90b865
AD
6355 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
6356 tx_flags &= ~IXGBE_TX_FLAGS_VLAN_PRIO_MASK;
5f715823 6357 tx_flags |= ((skb->queue_mapping & 0x7) << 13);
2f90b865
AD
6358 }
6359 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
6360 tx_flags |= IXGBE_TX_FLAGS_VLAN;
33c66bd1
JF
6361 } else if (adapter->flags & IXGBE_FLAG_DCB_ENABLED &&
6362 skb->priority != TC_PRIO_CONTROL) {
2ea186ae
JF
6363 tx_flags |= ((skb->queue_mapping & 0x7) << 13);
6364 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
6365 tx_flags |= IXGBE_TX_FLAGS_VLAN;
9a799d71 6366 }
eacd73f7 6367
09ad1cc0 6368#ifdef IXGBE_FCOE
56075a98
JF
6369 /* for FCoE with DCB, we force the priority to what
6370 * was specified by the switch */
6371 if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED &&
5e09a105
HZ
6372 (protocol == htons(ETH_P_FCOE) ||
6373 protocol == htons(ETH_P_FIP))) {
4bc091d8
JF
6374#ifdef CONFIG_IXGBE_DCB
6375 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
6376 tx_flags &= ~(IXGBE_TX_FLAGS_VLAN_PRIO_MASK
6377 << IXGBE_TX_FLAGS_VLAN_SHIFT);
6378 tx_flags |= ((adapter->fcoe.up << 13)
6379 << IXGBE_TX_FLAGS_VLAN_SHIFT);
6380 }
6381#endif
ca77cd59 6382 /* flag for FCoE offloads */
5e09a105 6383 if (protocol == htons(ETH_P_FCOE))
ca77cd59 6384 tx_flags |= IXGBE_TX_FLAGS_FCOE;
09ad1cc0 6385 }
ca77cd59
RL
6386#endif
6387
eacd73f7 6388 /* four things can cause us to need a context descriptor */
9f8cdf4f
JB
6389 if (skb_is_gso(skb) ||
6390 (skb->ip_summed == CHECKSUM_PARTIAL) ||
eacd73f7
YZ
6391 (tx_flags & IXGBE_TX_FLAGS_VLAN) ||
6392 (tx_flags & IXGBE_TX_FLAGS_FCOE))
9a799d71
AK
6393 count++;
6394
9f8cdf4f
JB
6395 count += TXD_USE_COUNT(skb_headlen(skb));
6396 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
9a799d71
AK
6397 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
6398
fc77dc3c 6399 if (ixgbe_maybe_stop_tx(tx_ring, count)) {
5b7da515 6400 tx_ring->tx_stats.tx_busy++;
9a799d71
AK
6401 return NETDEV_TX_BUSY;
6402 }
9a799d71 6403
9a799d71 6404 first = tx_ring->next_to_use;
eacd73f7
YZ
6405 if (tx_flags & IXGBE_TX_FLAGS_FCOE) {
6406#ifdef IXGBE_FCOE
6407 /* setup tx offload for FCoE */
6408 tso = ixgbe_fso(adapter, tx_ring, skb, tx_flags, &hdr_len);
6409 if (tso < 0) {
6410 dev_kfree_skb_any(skb);
6411 return NETDEV_TX_OK;
6412 }
6413 if (tso)
6414 tx_flags |= IXGBE_TX_FLAGS_FSO;
6415#endif /* IXGBE_FCOE */
6416 } else {
5e09a105 6417 if (protocol == htons(ETH_P_IP))
eacd73f7 6418 tx_flags |= IXGBE_TX_FLAGS_IPV4;
5e09a105
HZ
6419 tso = ixgbe_tso(adapter, tx_ring, skb, tx_flags, &hdr_len,
6420 protocol);
eacd73f7
YZ
6421 if (tso < 0) {
6422 dev_kfree_skb_any(skb);
6423 return NETDEV_TX_OK;
6424 }
9a799d71 6425
eacd73f7
YZ
6426 if (tso)
6427 tx_flags |= IXGBE_TX_FLAGS_TSO;
5e09a105
HZ
6428 else if (ixgbe_tx_csum(adapter, tx_ring, skb, tx_flags,
6429 protocol) &&
eacd73f7
YZ
6430 (skb->ip_summed == CHECKSUM_PARTIAL))
6431 tx_flags |= IXGBE_TX_FLAGS_CSUM;
6432 }
9a799d71 6433
8ad494b0 6434 count = ixgbe_tx_map(adapter, tx_ring, skb, tx_flags, first, hdr_len);
44df32c5 6435 if (count) {
c4cf55e5
PWJ
6436 /* add the ATR filter if ATR is on */
6437 if (tx_ring->atr_sample_rate) {
6438 ++tx_ring->atr_count;
6439 if ((tx_ring->atr_count >= tx_ring->atr_sample_rate) &&
7d637bcc
AD
6440 test_bit(__IXGBE_TX_FDIR_INIT_DONE,
6441 &tx_ring->state)) {
c4cf55e5 6442 ixgbe_atr(adapter, skb, tx_ring->queue_index,
5e09a105 6443 tx_flags, protocol);
c4cf55e5
PWJ
6444 tx_ring->atr_count = 0;
6445 }
6446 }
60d51134
ED
6447 txq = netdev_get_tx_queue(netdev, tx_ring->queue_index);
6448 txq->tx_bytes += skb->len;
6449 txq->tx_packets++;
84ea2591 6450 ixgbe_tx_queue(tx_ring, tx_flags, count, skb->len, hdr_len);
fc77dc3c 6451 ixgbe_maybe_stop_tx(tx_ring, DESC_NEEDED);
9a799d71 6452
44df32c5
AD
6453 } else {
6454 dev_kfree_skb_any(skb);
6455 tx_ring->tx_buffer_info[first].time_stamp = 0;
6456 tx_ring->next_to_use = first;
6457 }
9a799d71
AK
6458
6459 return NETDEV_TX_OK;
6460}
6461
84418e3b
AD
6462static netdev_tx_t ixgbe_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
6463{
6464 struct ixgbe_adapter *adapter = netdev_priv(netdev);
6465 struct ixgbe_ring *tx_ring;
6466
6467 tx_ring = adapter->tx_ring[skb->queue_mapping];
fc77dc3c 6468 return ixgbe_xmit_frame_ring(skb, adapter, tx_ring);
84418e3b
AD
6469}
6470
9a799d71
AK
6471/**
6472 * ixgbe_set_mac - Change the Ethernet Address of the NIC
6473 * @netdev: network interface device structure
6474 * @p: pointer to an address structure
6475 *
6476 * Returns 0 on success, negative on failure
6477 **/
6478static int ixgbe_set_mac(struct net_device *netdev, void *p)
6479{
6480 struct ixgbe_adapter *adapter = netdev_priv(netdev);
b4617240 6481 struct ixgbe_hw *hw = &adapter->hw;
9a799d71
AK
6482 struct sockaddr *addr = p;
6483
6484 if (!is_valid_ether_addr(addr->sa_data))
6485 return -EADDRNOTAVAIL;
6486
6487 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
b4617240 6488 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
9a799d71 6489
1cdd1ec8
GR
6490 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, adapter->num_vfs,
6491 IXGBE_RAH_AV);
9a799d71
AK
6492
6493 return 0;
6494}
6495
6b73e10d
BH
6496static int
6497ixgbe_mdio_read(struct net_device *netdev, int prtad, int devad, u16 addr)
6498{
6499 struct ixgbe_adapter *adapter = netdev_priv(netdev);
6500 struct ixgbe_hw *hw = &adapter->hw;
6501 u16 value;
6502 int rc;
6503
6504 if (prtad != hw->phy.mdio.prtad)
6505 return -EINVAL;
6506 rc = hw->phy.ops.read_reg(hw, addr, devad, &value);
6507 if (!rc)
6508 rc = value;
6509 return rc;
6510}
6511
6512static int ixgbe_mdio_write(struct net_device *netdev, int prtad, int devad,
6513 u16 addr, u16 value)
6514{
6515 struct ixgbe_adapter *adapter = netdev_priv(netdev);
6516 struct ixgbe_hw *hw = &adapter->hw;
6517
6518 if (prtad != hw->phy.mdio.prtad)
6519 return -EINVAL;
6520 return hw->phy.ops.write_reg(hw, addr, devad, value);
6521}
6522
6523static int ixgbe_ioctl(struct net_device *netdev, struct ifreq *req, int cmd)
6524{
6525 struct ixgbe_adapter *adapter = netdev_priv(netdev);
6526
6527 return mdio_mii_ioctl(&adapter->hw.phy.mdio, if_mii(req), cmd);
6528}
6529
0365e6e4
PW
6530/**
6531 * ixgbe_add_sanmac_netdev - Add the SAN MAC address to the corresponding
31278e71 6532 * netdev->dev_addrs
0365e6e4
PW
6533 * @netdev: network interface device structure
6534 *
6535 * Returns non-zero on failure
6536 **/
6537static int ixgbe_add_sanmac_netdev(struct net_device *dev)
6538{
6539 int err = 0;
6540 struct ixgbe_adapter *adapter = netdev_priv(dev);
6541 struct ixgbe_mac_info *mac = &adapter->hw.mac;
6542
6543 if (is_valid_ether_addr(mac->san_addr)) {
6544 rtnl_lock();
6545 err = dev_addr_add(dev, mac->san_addr, NETDEV_HW_ADDR_T_SAN);
6546 rtnl_unlock();
6547 }
6548 return err;
6549}
6550
6551/**
6552 * ixgbe_del_sanmac_netdev - Removes the SAN MAC address to the corresponding
31278e71 6553 * netdev->dev_addrs
0365e6e4
PW
6554 * @netdev: network interface device structure
6555 *
6556 * Returns non-zero on failure
6557 **/
6558static int ixgbe_del_sanmac_netdev(struct net_device *dev)
6559{
6560 int err = 0;
6561 struct ixgbe_adapter *adapter = netdev_priv(dev);
6562 struct ixgbe_mac_info *mac = &adapter->hw.mac;
6563
6564 if (is_valid_ether_addr(mac->san_addr)) {
6565 rtnl_lock();
6566 err = dev_addr_del(dev, mac->san_addr, NETDEV_HW_ADDR_T_SAN);
6567 rtnl_unlock();
6568 }
6569 return err;
6570}
6571
9a799d71
AK
6572#ifdef CONFIG_NET_POLL_CONTROLLER
6573/*
6574 * Polling 'interrupt' - used by things like netconsole to send skbs
6575 * without having to re-enable interrupts. It's not called while
6576 * the interrupt routine is executing.
6577 */
6578static void ixgbe_netpoll(struct net_device *netdev)
6579{
6580 struct ixgbe_adapter *adapter = netdev_priv(netdev);
8f9a7167 6581 int i;
9a799d71 6582
1a647bd2
AD
6583 /* if interface is down do nothing */
6584 if (test_bit(__IXGBE_DOWN, &adapter->state))
6585 return;
6586
9a799d71 6587 adapter->flags |= IXGBE_FLAG_IN_NETPOLL;
8f9a7167
PWJ
6588 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
6589 int num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
6590 for (i = 0; i < num_q_vectors; i++) {
6591 struct ixgbe_q_vector *q_vector = adapter->q_vector[i];
6592 ixgbe_msix_clean_many(0, q_vector);
6593 }
6594 } else {
6595 ixgbe_intr(adapter->pdev->irq, netdev);
6596 }
9a799d71 6597 adapter->flags &= ~IXGBE_FLAG_IN_NETPOLL;
9a799d71
AK
6598}
6599#endif
6600
de1036b1
ED
6601static struct rtnl_link_stats64 *ixgbe_get_stats64(struct net_device *netdev,
6602 struct rtnl_link_stats64 *stats)
6603{
6604 struct ixgbe_adapter *adapter = netdev_priv(netdev);
6605 int i;
6606
6607 /* accurate rx/tx bytes/packets stats */
6608 dev_txq_stats_fold(netdev, stats);
1a51502b 6609 rcu_read_lock();
de1036b1 6610 for (i = 0; i < adapter->num_rx_queues; i++) {
1a51502b 6611 struct ixgbe_ring *ring = ACCESS_ONCE(adapter->rx_ring[i]);
de1036b1
ED
6612 u64 bytes, packets;
6613 unsigned int start;
6614
1a51502b
ED
6615 if (ring) {
6616 do {
6617 start = u64_stats_fetch_begin_bh(&ring->syncp);
6618 packets = ring->stats.packets;
6619 bytes = ring->stats.bytes;
6620 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
6621 stats->rx_packets += packets;
6622 stats->rx_bytes += bytes;
6623 }
de1036b1 6624 }
1a51502b 6625 rcu_read_unlock();
de1036b1
ED
6626 /* following stats updated by ixgbe_watchdog_task() */
6627 stats->multicast = netdev->stats.multicast;
6628 stats->rx_errors = netdev->stats.rx_errors;
6629 stats->rx_length_errors = netdev->stats.rx_length_errors;
6630 stats->rx_crc_errors = netdev->stats.rx_crc_errors;
6631 stats->rx_missed_errors = netdev->stats.rx_missed_errors;
6632 return stats;
6633}
6634
6635
0edc3527 6636static const struct net_device_ops ixgbe_netdev_ops = {
e8e9f696 6637 .ndo_open = ixgbe_open,
0edc3527 6638 .ndo_stop = ixgbe_close,
00829823 6639 .ndo_start_xmit = ixgbe_xmit_frame,
09a3b1f8 6640 .ndo_select_queue = ixgbe_select_queue,
e90d400c 6641 .ndo_set_rx_mode = ixgbe_set_rx_mode,
0edc3527
SH
6642 .ndo_set_multicast_list = ixgbe_set_rx_mode,
6643 .ndo_validate_addr = eth_validate_addr,
6644 .ndo_set_mac_address = ixgbe_set_mac,
6645 .ndo_change_mtu = ixgbe_change_mtu,
6646 .ndo_tx_timeout = ixgbe_tx_timeout,
0edc3527
SH
6647 .ndo_vlan_rx_add_vid = ixgbe_vlan_rx_add_vid,
6648 .ndo_vlan_rx_kill_vid = ixgbe_vlan_rx_kill_vid,
6b73e10d 6649 .ndo_do_ioctl = ixgbe_ioctl,
7f01648a
GR
6650 .ndo_set_vf_mac = ixgbe_ndo_set_vf_mac,
6651 .ndo_set_vf_vlan = ixgbe_ndo_set_vf_vlan,
6652 .ndo_set_vf_tx_rate = ixgbe_ndo_set_vf_bw,
6653 .ndo_get_vf_config = ixgbe_ndo_get_vf_config,
de1036b1 6654 .ndo_get_stats64 = ixgbe_get_stats64,
0edc3527
SH
6655#ifdef CONFIG_NET_POLL_CONTROLLER
6656 .ndo_poll_controller = ixgbe_netpoll,
6657#endif
332d4a7d
YZ
6658#ifdef IXGBE_FCOE
6659 .ndo_fcoe_ddp_setup = ixgbe_fcoe_ddp_get,
6660 .ndo_fcoe_ddp_done = ixgbe_fcoe_ddp_put,
8450ff8c
YZ
6661 .ndo_fcoe_enable = ixgbe_fcoe_enable,
6662 .ndo_fcoe_disable = ixgbe_fcoe_disable,
61a1fa10 6663 .ndo_fcoe_get_wwn = ixgbe_fcoe_get_wwn,
332d4a7d 6664#endif /* IXGBE_FCOE */
0edc3527
SH
6665};
6666
1cdd1ec8
GR
6667static void __devinit ixgbe_probe_vf(struct ixgbe_adapter *adapter,
6668 const struct ixgbe_info *ii)
6669{
6670#ifdef CONFIG_PCI_IOV
6671 struct ixgbe_hw *hw = &adapter->hw;
6672 int err;
6673
6674 if (hw->mac.type != ixgbe_mac_82599EB || !max_vfs)
6675 return;
6676
6677 /* The 82599 supports up to 64 VFs per physical function
6678 * but this implementation limits allocation to 63 so that
6679 * basic networking resources are still available to the
6680 * physical function
6681 */
6682 adapter->num_vfs = (max_vfs > 63) ? 63 : max_vfs;
6683 adapter->flags |= IXGBE_FLAG_SRIOV_ENABLED;
6684 err = pci_enable_sriov(adapter->pdev, adapter->num_vfs);
6685 if (err) {
396e799c 6686 e_err(probe, "Failed to enable PCI sriov: %d\n", err);
1cdd1ec8
GR
6687 goto err_novfs;
6688 }
6689 /* If call to enable VFs succeeded then allocate memory
6690 * for per VF control structures.
6691 */
6692 adapter->vfinfo =
6693 kcalloc(adapter->num_vfs,
6694 sizeof(struct vf_data_storage), GFP_KERNEL);
6695 if (adapter->vfinfo) {
6696 /* Now that we're sure SR-IOV is enabled
6697 * and memory allocated set up the mailbox parameters
6698 */
6699 ixgbe_init_mbx_params_pf(hw);
6700 memcpy(&hw->mbx.ops, ii->mbx_ops,
6701 sizeof(hw->mbx.ops));
6702
6703 /* Disable RSC when in SR-IOV mode */
6704 adapter->flags2 &= ~(IXGBE_FLAG2_RSC_CAPABLE |
6705 IXGBE_FLAG2_RSC_ENABLED);
6706 return;
6707 }
6708
6709 /* Oh oh */
396e799c
ET
6710 e_err(probe, "Unable to allocate memory for VF Data Storage - "
6711 "SRIOV disabled\n");
1cdd1ec8
GR
6712 pci_disable_sriov(adapter->pdev);
6713
6714err_novfs:
6715 adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
6716 adapter->num_vfs = 0;
6717#endif /* CONFIG_PCI_IOV */
6718}
6719
9a799d71
AK
6720/**
6721 * ixgbe_probe - Device Initialization Routine
6722 * @pdev: PCI device information struct
6723 * @ent: entry in ixgbe_pci_tbl
6724 *
6725 * Returns 0 on success, negative on failure
6726 *
6727 * ixgbe_probe initializes an adapter identified by a pci_dev structure.
6728 * The OS initialization, configuring of the adapter private structure,
6729 * and a hardware reset occur.
6730 **/
6731static int __devinit ixgbe_probe(struct pci_dev *pdev,
e8e9f696 6732 const struct pci_device_id *ent)
9a799d71
AK
6733{
6734 struct net_device *netdev;
6735 struct ixgbe_adapter *adapter = NULL;
6736 struct ixgbe_hw *hw;
6737 const struct ixgbe_info *ii = ixgbe_info_tbl[ent->driver_data];
9a799d71
AK
6738 static int cards_found;
6739 int i, err, pci_using_dac;
c85a2618 6740 unsigned int indices = num_possible_cpus();
eacd73f7
YZ
6741#ifdef IXGBE_FCOE
6742 u16 device_caps;
6743#endif
c44ade9e 6744 u32 part_num, eec;
9a799d71 6745
bded64a7
AG
6746 /* Catch broken hardware that put the wrong VF device ID in
6747 * the PCIe SR-IOV capability.
6748 */
6749 if (pdev->is_virtfn) {
6750 WARN(1, KERN_ERR "%s (%hx:%hx) should not be a VF!\n",
6751 pci_name(pdev), pdev->vendor, pdev->device);
6752 return -EINVAL;
6753 }
6754
9ce77666 6755 err = pci_enable_device_mem(pdev);
9a799d71
AK
6756 if (err)
6757 return err;
6758
1b507730
NN
6759 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
6760 !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
9a799d71
AK
6761 pci_using_dac = 1;
6762 } else {
1b507730 6763 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
9a799d71 6764 if (err) {
1b507730
NN
6765 err = dma_set_coherent_mask(&pdev->dev,
6766 DMA_BIT_MASK(32));
9a799d71 6767 if (err) {
b8bc0421
DC
6768 dev_err(&pdev->dev,
6769 "No usable DMA configuration, aborting\n");
9a799d71
AK
6770 goto err_dma;
6771 }
6772 }
6773 pci_using_dac = 0;
6774 }
6775
9ce77666 6776 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
e8e9f696 6777 IORESOURCE_MEM), ixgbe_driver_name);
9a799d71 6778 if (err) {
b8bc0421
DC
6779 dev_err(&pdev->dev,
6780 "pci_request_selected_regions failed 0x%x\n", err);
9a799d71
AK
6781 goto err_pci_reg;
6782 }
6783
19d5afd4 6784 pci_enable_pcie_error_reporting(pdev);
6fabd715 6785
9a799d71 6786 pci_set_master(pdev);
fb3b27bc 6787 pci_save_state(pdev);
9a799d71 6788
c85a2618
JF
6789 if (ii->mac == ixgbe_mac_82598EB)
6790 indices = min_t(unsigned int, indices, IXGBE_MAX_RSS_INDICES);
6791 else
6792 indices = min_t(unsigned int, indices, IXGBE_MAX_FDIR_INDICES);
6793
6794 indices = max_t(unsigned int, indices, IXGBE_MAX_DCB_INDICES);
6795#ifdef IXGBE_FCOE
6796 indices += min_t(unsigned int, num_possible_cpus(),
6797 IXGBE_MAX_FCOE_INDICES);
6798#endif
c85a2618 6799 netdev = alloc_etherdev_mq(sizeof(struct ixgbe_adapter), indices);
9a799d71
AK
6800 if (!netdev) {
6801 err = -ENOMEM;
6802 goto err_alloc_etherdev;
6803 }
6804
9a799d71
AK
6805 SET_NETDEV_DEV(netdev, &pdev->dev);
6806
9a799d71 6807 adapter = netdev_priv(netdev);
c60fbb00 6808 pci_set_drvdata(pdev, adapter);
9a799d71
AK
6809
6810 adapter->netdev = netdev;
6811 adapter->pdev = pdev;
6812 hw = &adapter->hw;
6813 hw->back = adapter;
6814 adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
6815
05857980 6816 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
e8e9f696 6817 pci_resource_len(pdev, 0));
9a799d71
AK
6818 if (!hw->hw_addr) {
6819 err = -EIO;
6820 goto err_ioremap;
6821 }
6822
6823 for (i = 1; i <= 5; i++) {
6824 if (pci_resource_len(pdev, i) == 0)
6825 continue;
6826 }
6827
0edc3527 6828 netdev->netdev_ops = &ixgbe_netdev_ops;
9a799d71 6829 ixgbe_set_ethtool_ops(netdev);
9a799d71 6830 netdev->watchdog_timeo = 5 * HZ;
9a799d71
AK
6831 strcpy(netdev->name, pci_name(pdev));
6832
9a799d71
AK
6833 adapter->bd_number = cards_found;
6834
9a799d71
AK
6835 /* Setup hw api */
6836 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
021230d4 6837 hw->mac.type = ii->mac;
9a799d71 6838
c44ade9e
JB
6839 /* EEPROM */
6840 memcpy(&hw->eeprom.ops, ii->eeprom_ops, sizeof(hw->eeprom.ops));
6841 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
6842 /* If EEPROM is valid (bit 8 = 1), use default otherwise use bit bang */
6843 if (!(eec & (1 << 8)))
6844 hw->eeprom.ops.read = &ixgbe_read_eeprom_bit_bang_generic;
6845
6846 /* PHY */
6847 memcpy(&hw->phy.ops, ii->phy_ops, sizeof(hw->phy.ops));
c4900be0 6848 hw->phy.sfp_type = ixgbe_sfp_type_unknown;
6b73e10d
BH
6849 /* ixgbe_identify_phy_generic will set prtad and mmds properly */
6850 hw->phy.mdio.prtad = MDIO_PRTAD_NONE;
6851 hw->phy.mdio.mmds = 0;
6852 hw->phy.mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
6853 hw->phy.mdio.dev = netdev;
6854 hw->phy.mdio.mdio_read = ixgbe_mdio_read;
6855 hw->phy.mdio.mdio_write = ixgbe_mdio_write;
c4900be0
DS
6856
6857 /* set up this timer and work struct before calling get_invariants
6858 * which might start the timer
6859 */
6860 init_timer(&adapter->sfp_timer);
c061b18d 6861 adapter->sfp_timer.function = ixgbe_sfp_timer;
c4900be0
DS
6862 adapter->sfp_timer.data = (unsigned long) adapter;
6863
6864 INIT_WORK(&adapter->sfp_task, ixgbe_sfp_task);
c44ade9e 6865
e8e26350
PW
6866 /* multispeed fiber has its own tasklet, called from GPI SDP1 context */
6867 INIT_WORK(&adapter->multispeed_fiber_task, ixgbe_multispeed_fiber_task);
6868
6869 /* a new SFP+ module arrival, called from GPI SDP2 context */
6870 INIT_WORK(&adapter->sfp_config_module_task,
e8e9f696 6871 ixgbe_sfp_config_module_task);
e8e26350 6872
8ca783ab 6873 ii->get_invariants(hw);
9a799d71
AK
6874
6875 /* setup the private structure */
6876 err = ixgbe_sw_init(adapter);
6877 if (err)
6878 goto err_sw_init;
6879
e86bff0e
DS
6880 /* Make it possible the adapter to be woken up via WOL */
6881 if (adapter->hw.mac.type == ixgbe_mac_82599EB)
6882 IXGBE_WRITE_REG(&adapter->hw, IXGBE_WUS, ~0);
6883
bf069c97
DS
6884 /*
6885 * If there is a fan on this device and it has failed log the
6886 * failure.
6887 */
6888 if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE) {
6889 u32 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
6890 if (esdp & IXGBE_ESDP_SDP1)
396e799c 6891 e_crit(probe, "Fan has stopped, replace the adapter\n");
bf069c97
DS
6892 }
6893
c44ade9e 6894 /* reset_hw fills in the perm_addr as well */
119fc60a 6895 hw->phy.reset_if_overtemp = true;
c44ade9e 6896 err = hw->mac.ops.reset_hw(hw);
119fc60a 6897 hw->phy.reset_if_overtemp = false;
8ca783ab
DS
6898 if (err == IXGBE_ERR_SFP_NOT_PRESENT &&
6899 hw->mac.type == ixgbe_mac_82598EB) {
6900 /*
6901 * Start a kernel thread to watch for a module to arrive.
6902 * Only do this for 82598, since 82599 will generate
6903 * interrupts on module arrival.
6904 */
6905 set_bit(__IXGBE_SFP_MODULE_NOT_FOUND, &adapter->state);
6906 mod_timer(&adapter->sfp_timer,
6907 round_jiffies(jiffies + (2 * HZ)));
6908 err = 0;
6909 } else if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
849c4542
ET
6910 e_dev_err("failed to initialize because an unsupported SFP+ "
6911 "module type was detected.\n");
6912 e_dev_err("Reload the driver after installing a supported "
6913 "module.\n");
04f165ef
PW
6914 goto err_sw_init;
6915 } else if (err) {
849c4542 6916 e_dev_err("HW Init failed: %d\n", err);
c44ade9e
JB
6917 goto err_sw_init;
6918 }
6919
1cdd1ec8
GR
6920 ixgbe_probe_vf(adapter, ii);
6921
396e799c 6922 netdev->features = NETIF_F_SG |
e8e9f696
JP
6923 NETIF_F_IP_CSUM |
6924 NETIF_F_HW_VLAN_TX |
6925 NETIF_F_HW_VLAN_RX |
6926 NETIF_F_HW_VLAN_FILTER;
9a799d71 6927
e9990a9c 6928 netdev->features |= NETIF_F_IPV6_CSUM;
9a799d71 6929 netdev->features |= NETIF_F_TSO;
9a799d71 6930 netdev->features |= NETIF_F_TSO6;
78b6f4ce 6931 netdev->features |= NETIF_F_GRO;
ad31c402 6932
45a5ead0
JB
6933 if (adapter->hw.mac.type == ixgbe_mac_82599EB)
6934 netdev->features |= NETIF_F_SCTP_CSUM;
6935
ad31c402
JK
6936 netdev->vlan_features |= NETIF_F_TSO;
6937 netdev->vlan_features |= NETIF_F_TSO6;
22f32b7a 6938 netdev->vlan_features |= NETIF_F_IP_CSUM;
cd1da503 6939 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
ad31c402
JK
6940 netdev->vlan_features |= NETIF_F_SG;
6941
1cdd1ec8
GR
6942 if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
6943 adapter->flags &= ~(IXGBE_FLAG_RSS_ENABLED |
6944 IXGBE_FLAG_DCB_ENABLED);
2f90b865
AD
6945 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED)
6946 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
6947
7a6b6f51 6948#ifdef CONFIG_IXGBE_DCB
2f90b865
AD
6949 netdev->dcbnl_ops = &dcbnl_ops;
6950#endif
6951
eacd73f7 6952#ifdef IXGBE_FCOE
0d551589 6953 if (adapter->flags & IXGBE_FLAG_FCOE_CAPABLE) {
eacd73f7
YZ
6954 if (hw->mac.ops.get_device_caps) {
6955 hw->mac.ops.get_device_caps(hw, &device_caps);
0d551589
YZ
6956 if (device_caps & IXGBE_DEVICE_CAPS_FCOE_OFFLOADS)
6957 adapter->flags &= ~IXGBE_FLAG_FCOE_CAPABLE;
eacd73f7
YZ
6958 }
6959 }
5e09d7f6
YZ
6960 if (adapter->flags & IXGBE_FLAG_FCOE_CAPABLE) {
6961 netdev->vlan_features |= NETIF_F_FCOE_CRC;
6962 netdev->vlan_features |= NETIF_F_FSO;
6963 netdev->vlan_features |= NETIF_F_FCOE_MTU;
6964 }
eacd73f7 6965#endif /* IXGBE_FCOE */
7b872a55 6966 if (pci_using_dac) {
9a799d71 6967 netdev->features |= NETIF_F_HIGHDMA;
7b872a55
YZ
6968 netdev->vlan_features |= NETIF_F_HIGHDMA;
6969 }
9a799d71 6970
0c19d6af 6971 if (adapter->flags2 & IXGBE_FLAG2_RSC_ENABLED)
f8212f97
AD
6972 netdev->features |= NETIF_F_LRO;
6973
9a799d71 6974 /* make sure the EEPROM is good */
c44ade9e 6975 if (hw->eeprom.ops.validate_checksum(hw, NULL) < 0) {
849c4542 6976 e_dev_err("The EEPROM Checksum Is Not Valid\n");
9a799d71
AK
6977 err = -EIO;
6978 goto err_eeprom;
6979 }
6980
6981 memcpy(netdev->dev_addr, hw->mac.perm_addr, netdev->addr_len);
6982 memcpy(netdev->perm_addr, hw->mac.perm_addr, netdev->addr_len);
6983
c44ade9e 6984 if (ixgbe_validate_mac_addr(netdev->perm_addr)) {
849c4542 6985 e_dev_err("invalid MAC address\n");
9a799d71
AK
6986 err = -EIO;
6987 goto err_eeprom;
6988 }
6989
61fac744
PW
6990 /* power down the optics */
6991 if (hw->phy.multispeed_fiber)
6992 hw->mac.ops.disable_tx_laser(hw);
6993
9a799d71 6994 init_timer(&adapter->watchdog_timer);
c061b18d 6995 adapter->watchdog_timer.function = ixgbe_watchdog;
9a799d71
AK
6996 adapter->watchdog_timer.data = (unsigned long)adapter;
6997
6998 INIT_WORK(&adapter->reset_task, ixgbe_reset_task);
cf8280ee 6999 INIT_WORK(&adapter->watchdog_task, ixgbe_watchdog_task);
9a799d71 7000
021230d4
AV
7001 err = ixgbe_init_interrupt_scheme(adapter);
7002 if (err)
7003 goto err_sw_init;
9a799d71 7004
e8e26350
PW
7005 switch (pdev->device) {
7006 case IXGBE_DEV_ID_82599_KX4:
495dce12 7007 adapter->wol = (IXGBE_WUFC_MAG | IXGBE_WUFC_EX |
e8e9f696 7008 IXGBE_WUFC_MC | IXGBE_WUFC_BC);
e8e26350
PW
7009 break;
7010 default:
7011 adapter->wol = 0;
7012 break;
7013 }
e8e26350
PW
7014 device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
7015
04f165ef
PW
7016 /* pick up the PCI bus settings for reporting later */
7017 hw->mac.ops.get_bus_info(hw);
7018
9a799d71 7019 /* print bus type/speed/width info */
849c4542 7020 e_dev_info("(PCI Express:%s:%s) %pM\n",
e8e9f696
JP
7021 (hw->bus.speed == ixgbe_bus_speed_5000 ? "5.0Gb/s" :
7022 hw->bus.speed == ixgbe_bus_speed_2500 ? "2.5Gb/s" :
7023 "Unknown"),
7024 (hw->bus.width == ixgbe_bus_width_pcie_x8 ? "Width x8" :
7025 hw->bus.width == ixgbe_bus_width_pcie_x4 ? "Width x4" :
7026 hw->bus.width == ixgbe_bus_width_pcie_x1 ? "Width x1" :
7027 "Unknown"),
7028 netdev->dev_addr);
c44ade9e 7029 ixgbe_read_pba_num_generic(hw, &part_num);
e8e26350 7030 if (ixgbe_is_sfp(hw) && hw->phy.sfp_type != ixgbe_sfp_type_not_present)
849c4542
ET
7031 e_dev_info("MAC: %d, PHY: %d, SFP+: %d, "
7032 "PBA No: %06x-%03x\n",
7033 hw->mac.type, hw->phy.type, hw->phy.sfp_type,
7034 (part_num >> 8), (part_num & 0xff));
e8e26350 7035 else
849c4542
ET
7036 e_dev_info("MAC: %d, PHY: %d, PBA No: %06x-%03x\n",
7037 hw->mac.type, hw->phy.type,
7038 (part_num >> 8), (part_num & 0xff));
9a799d71 7039
e8e26350 7040 if (hw->bus.width <= ixgbe_bus_width_pcie_x4) {
849c4542
ET
7041 e_dev_warn("PCI-Express bandwidth available for this card is "
7042 "not sufficient for optimal performance.\n");
7043 e_dev_warn("For optimal performance a x8 PCI-Express slot "
7044 "is required.\n");
0c254d86
AK
7045 }
7046
34b0368c
PWJ
7047 /* save off EEPROM version number */
7048 hw->eeprom.ops.read(hw, 0x29, &adapter->eeprom_version);
7049
9a799d71 7050 /* reset the hardware with the new settings */
794caeb2 7051 err = hw->mac.ops.start_hw(hw);
c44ade9e 7052
794caeb2
PWJ
7053 if (err == IXGBE_ERR_EEPROM_VERSION) {
7054 /* We are running on a pre-production device, log a warning */
849c4542
ET
7055 e_dev_warn("This device is a pre-production adapter/LOM. "
7056 "Please be aware there may be issues associated "
7057 "with your hardware. If you are experiencing "
7058 "problems please contact your Intel or hardware "
7059 "representative who provided you with this "
7060 "hardware.\n");
794caeb2 7061 }
9a799d71
AK
7062 strcpy(netdev->name, "eth%d");
7063 err = register_netdev(netdev);
7064 if (err)
7065 goto err_register;
7066
54386467
JB
7067 /* carrier off reporting is important to ethtool even BEFORE open */
7068 netif_carrier_off(netdev);
7069
c4cf55e5
PWJ
7070 if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE ||
7071 adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE)
7072 INIT_WORK(&adapter->fdir_reinit_task, ixgbe_fdir_reinit_task);
7073
119fc60a 7074 if (adapter->flags2 & IXGBE_FLAG2_TEMP_SENSOR_CAPABLE)
e8e9f696
JP
7075 INIT_WORK(&adapter->check_overtemp_task,
7076 ixgbe_check_overtemp_task);
5dd2d332 7077#ifdef CONFIG_IXGBE_DCA
652f093f 7078 if (dca_add_requester(&pdev->dev) == 0) {
bd0362dd 7079 adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
bd0362dd
JC
7080 ixgbe_setup_dca(adapter);
7081 }
7082#endif
1cdd1ec8 7083 if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) {
396e799c 7084 e_info(probe, "IOV is enabled with %d VFs\n", adapter->num_vfs);
1cdd1ec8
GR
7085 for (i = 0; i < adapter->num_vfs; i++)
7086 ixgbe_vf_configuration(pdev, (i | 0x10000000));
7087 }
7088
0365e6e4
PW
7089 /* add san mac addr to netdev */
7090 ixgbe_add_sanmac_netdev(netdev);
9a799d71 7091
849c4542 7092 e_dev_info("Intel(R) 10 Gigabit Network Connection\n");
9a799d71
AK
7093 cards_found++;
7094 return 0;
7095
7096err_register:
5eba3699 7097 ixgbe_release_hw_control(adapter);
7a921c93 7098 ixgbe_clear_interrupt_scheme(adapter);
9a799d71
AK
7099err_sw_init:
7100err_eeprom:
1cdd1ec8
GR
7101 if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
7102 ixgbe_disable_sriov(adapter);
c4900be0
DS
7103 clear_bit(__IXGBE_SFP_MODULE_NOT_FOUND, &adapter->state);
7104 del_timer_sync(&adapter->sfp_timer);
7105 cancel_work_sync(&adapter->sfp_task);
e8e26350
PW
7106 cancel_work_sync(&adapter->multispeed_fiber_task);
7107 cancel_work_sync(&adapter->sfp_config_module_task);
9a799d71
AK
7108 iounmap(hw->hw_addr);
7109err_ioremap:
7110 free_netdev(netdev);
7111err_alloc_etherdev:
e8e9f696
JP
7112 pci_release_selected_regions(pdev,
7113 pci_select_bars(pdev, IORESOURCE_MEM));
9a799d71
AK
7114err_pci_reg:
7115err_dma:
7116 pci_disable_device(pdev);
7117 return err;
7118}
7119
7120/**
7121 * ixgbe_remove - Device Removal Routine
7122 * @pdev: PCI device information struct
7123 *
7124 * ixgbe_remove is called by the PCI subsystem to alert the driver
7125 * that it should release a PCI device. The could be caused by a
7126 * Hot-Plug event, or because the driver is going to be removed from
7127 * memory.
7128 **/
7129static void __devexit ixgbe_remove(struct pci_dev *pdev)
7130{
c60fbb00
AD
7131 struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
7132 struct net_device *netdev = adapter->netdev;
9a799d71
AK
7133
7134 set_bit(__IXGBE_DOWN, &adapter->state);
c4900be0
DS
7135 /* clear the module not found bit to make sure the worker won't
7136 * reschedule
7137 */
7138 clear_bit(__IXGBE_SFP_MODULE_NOT_FOUND, &adapter->state);
9a799d71
AK
7139 del_timer_sync(&adapter->watchdog_timer);
7140
c4900be0
DS
7141 del_timer_sync(&adapter->sfp_timer);
7142 cancel_work_sync(&adapter->watchdog_task);
7143 cancel_work_sync(&adapter->sfp_task);
e8e26350
PW
7144 cancel_work_sync(&adapter->multispeed_fiber_task);
7145 cancel_work_sync(&adapter->sfp_config_module_task);
c4cf55e5
PWJ
7146 if (adapter->flags & IXGBE_FLAG_FDIR_HASH_CAPABLE ||
7147 adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE)
7148 cancel_work_sync(&adapter->fdir_reinit_task);
9a799d71
AK
7149 flush_scheduled_work();
7150
5dd2d332 7151#ifdef CONFIG_IXGBE_DCA
bd0362dd
JC
7152 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
7153 adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
7154 dca_remove_requester(&pdev->dev);
7155 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 1);
7156 }
7157
7158#endif
332d4a7d
YZ
7159#ifdef IXGBE_FCOE
7160 if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED)
7161 ixgbe_cleanup_fcoe(adapter);
7162
7163#endif /* IXGBE_FCOE */
0365e6e4
PW
7164
7165 /* remove the added san mac */
7166 ixgbe_del_sanmac_netdev(netdev);
7167
c4900be0
DS
7168 if (netdev->reg_state == NETREG_REGISTERED)
7169 unregister_netdev(netdev);
9a799d71 7170
1cdd1ec8
GR
7171 if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
7172 ixgbe_disable_sriov(adapter);
7173
7a921c93 7174 ixgbe_clear_interrupt_scheme(adapter);
5eba3699 7175
021230d4 7176 ixgbe_release_hw_control(adapter);
9a799d71
AK
7177
7178 iounmap(adapter->hw.hw_addr);
9ce77666 7179 pci_release_selected_regions(pdev, pci_select_bars(pdev,
e8e9f696 7180 IORESOURCE_MEM));
9a799d71 7181
849c4542 7182 e_dev_info("complete\n");
021230d4 7183
9a799d71
AK
7184 free_netdev(netdev);
7185
19d5afd4 7186 pci_disable_pcie_error_reporting(pdev);
6fabd715 7187
9a799d71
AK
7188 pci_disable_device(pdev);
7189}
7190
7191/**
7192 * ixgbe_io_error_detected - called when PCI error is detected
7193 * @pdev: Pointer to PCI device
7194 * @state: The current pci connection state
7195 *
7196 * This function is called after a PCI bus error affecting
7197 * this device has been detected.
7198 */
7199static pci_ers_result_t ixgbe_io_error_detected(struct pci_dev *pdev,
e8e9f696 7200 pci_channel_state_t state)
9a799d71 7201{
c60fbb00
AD
7202 struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
7203 struct net_device *netdev = adapter->netdev;
9a799d71
AK
7204
7205 netif_device_detach(netdev);
7206
3044b8d1
BL
7207 if (state == pci_channel_io_perm_failure)
7208 return PCI_ERS_RESULT_DISCONNECT;
7209
9a799d71
AK
7210 if (netif_running(netdev))
7211 ixgbe_down(adapter);
7212 pci_disable_device(pdev);
7213
b4617240 7214 /* Request a slot reset. */
9a799d71
AK
7215 return PCI_ERS_RESULT_NEED_RESET;
7216}
7217
7218/**
7219 * ixgbe_io_slot_reset - called after the pci bus has been reset.
7220 * @pdev: Pointer to PCI device
7221 *
7222 * Restart the card from scratch, as if from a cold-boot.
7223 */
7224static pci_ers_result_t ixgbe_io_slot_reset(struct pci_dev *pdev)
7225{
c60fbb00 7226 struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
6fabd715
PWJ
7227 pci_ers_result_t result;
7228 int err;
9a799d71 7229
9ce77666 7230 if (pci_enable_device_mem(pdev)) {
396e799c 7231 e_err(probe, "Cannot re-enable PCI device after reset.\n");
6fabd715
PWJ
7232 result = PCI_ERS_RESULT_DISCONNECT;
7233 } else {
7234 pci_set_master(pdev);
7235 pci_restore_state(pdev);
c0e1f68b 7236 pci_save_state(pdev);
9a799d71 7237
dd4d8ca6 7238 pci_wake_from_d3(pdev, false);
9a799d71 7239
6fabd715 7240 ixgbe_reset(adapter);
88512539 7241 IXGBE_WRITE_REG(&adapter->hw, IXGBE_WUS, ~0);
6fabd715
PWJ
7242 result = PCI_ERS_RESULT_RECOVERED;
7243 }
7244
7245 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7246 if (err) {
849c4542
ET
7247 e_dev_err("pci_cleanup_aer_uncorrect_error_status "
7248 "failed 0x%0x\n", err);
6fabd715
PWJ
7249 /* non-fatal, continue */
7250 }
9a799d71 7251
6fabd715 7252 return result;
9a799d71
AK
7253}
7254
7255/**
7256 * ixgbe_io_resume - called when traffic can start flowing again.
7257 * @pdev: Pointer to PCI device
7258 *
7259 * This callback is called when the error recovery driver tells us that
7260 * its OK to resume normal operation.
7261 */
7262static void ixgbe_io_resume(struct pci_dev *pdev)
7263{
c60fbb00
AD
7264 struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
7265 struct net_device *netdev = adapter->netdev;
9a799d71
AK
7266
7267 if (netif_running(netdev)) {
7268 if (ixgbe_up(adapter)) {
396e799c 7269 e_info(probe, "ixgbe_up failed after reset\n");
9a799d71
AK
7270 return;
7271 }
7272 }
7273
7274 netif_device_attach(netdev);
9a799d71
AK
7275}
7276
7277static struct pci_error_handlers ixgbe_err_handler = {
7278 .error_detected = ixgbe_io_error_detected,
7279 .slot_reset = ixgbe_io_slot_reset,
7280 .resume = ixgbe_io_resume,
7281};
7282
7283static struct pci_driver ixgbe_driver = {
7284 .name = ixgbe_driver_name,
7285 .id_table = ixgbe_pci_tbl,
7286 .probe = ixgbe_probe,
7287 .remove = __devexit_p(ixgbe_remove),
7288#ifdef CONFIG_PM
7289 .suspend = ixgbe_suspend,
7290 .resume = ixgbe_resume,
7291#endif
7292 .shutdown = ixgbe_shutdown,
7293 .err_handler = &ixgbe_err_handler
7294};
7295
7296/**
7297 * ixgbe_init_module - Driver Registration Routine
7298 *
7299 * ixgbe_init_module is the first routine called when the driver is
7300 * loaded. All it does is register with the PCI subsystem.
7301 **/
7302static int __init ixgbe_init_module(void)
7303{
7304 int ret;
c7689578 7305 pr_info("%s - version %s\n", ixgbe_driver_string, ixgbe_driver_version);
849c4542 7306 pr_info("%s\n", ixgbe_copyright);
9a799d71 7307
5dd2d332 7308#ifdef CONFIG_IXGBE_DCA
bd0362dd 7309 dca_register_notify(&dca_notifier);
bd0362dd 7310#endif
5dd2d332 7311
9a799d71
AK
7312 ret = pci_register_driver(&ixgbe_driver);
7313 return ret;
7314}
b4617240 7315
9a799d71
AK
7316module_init(ixgbe_init_module);
7317
7318/**
7319 * ixgbe_exit_module - Driver Exit Cleanup Routine
7320 *
7321 * ixgbe_exit_module is called just before the driver is removed
7322 * from memory.
7323 **/
7324static void __exit ixgbe_exit_module(void)
7325{
5dd2d332 7326#ifdef CONFIG_IXGBE_DCA
bd0362dd
JC
7327 dca_unregister_notify(&dca_notifier);
7328#endif
9a799d71 7329 pci_unregister_driver(&ixgbe_driver);
1a51502b 7330 rcu_barrier(); /* Wait for completion of call_rcu()'s */
9a799d71 7331}
bd0362dd 7332
5dd2d332 7333#ifdef CONFIG_IXGBE_DCA
bd0362dd 7334static int ixgbe_notify_dca(struct notifier_block *nb, unsigned long event,
e8e9f696 7335 void *p)
bd0362dd
JC
7336{
7337 int ret_val;
7338
7339 ret_val = driver_for_each_device(&ixgbe_driver.driver, NULL, &event,
e8e9f696 7340 __ixgbe_notify_dca);
bd0362dd
JC
7341
7342 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
7343}
b453368d 7344
5dd2d332 7345#endif /* CONFIG_IXGBE_DCA */
849c4542 7346
b453368d 7347/**
849c4542 7348 * ixgbe_get_hw_dev return device
b453368d
AD
7349 * used by hardware layer to print debugging information
7350 **/
849c4542 7351struct net_device *ixgbe_get_hw_dev(struct ixgbe_hw *hw)
b453368d
AD
7352{
7353 struct ixgbe_adapter *adapter = hw->back;
849c4542 7354 return adapter->netdev;
b453368d 7355}
bd0362dd 7356
9a799d71
AK
7357module_exit(ixgbe_exit_module);
7358
7359/* ixgbe_main.c */