]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/ixgbe/ixgbe_main.c
sctp: fix missing label when PROC_FS=n
[net-next-2.6.git] / drivers / net / ixgbe / ixgbe_main.c
CommitLineData
9a799d71
AK
1/*******************************************************************************
2
3 Intel 10 Gigabit PCI Express Linux driver
b4617240 4 Copyright(c) 1999 - 2008 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>
37#include <linux/ipv6.h>
38#include <net/checksum.h>
39#include <net/ip6_checksum.h>
40#include <linux/ethtool.h>
41#include <linux/if_vlan.h>
42
43#include "ixgbe.h"
44#include "ixgbe_common.h"
45
46char ixgbe_driver_name[] = "ixgbe";
9c8eb720 47static const char ixgbe_driver_string[] =
b4617240 48 "Intel(R) 10 Gigabit PCI Express Network Driver";
9a799d71 49
51ac6445 50#define DRV_VERSION "1.3.30-k2"
9c8eb720 51const char ixgbe_driver_version[] = DRV_VERSION;
b4617240 52static char ixgbe_copyright[] = "Copyright (c) 1999-2007 Intel Corporation.";
9a799d71
AK
53
54static const struct ixgbe_info *ixgbe_info_tbl[] = {
b4617240 55 [board_82598] = &ixgbe_82598_info,
9a799d71
AK
56};
57
58/* ixgbe_pci_tbl - PCI Device ID Table
59 *
60 * Wildcard entries (PCI_ANY_ID) should come last
61 * Last entry must be all 0s
62 *
63 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
64 * Class, Class Mask, private data (not used) }
65 */
66static struct pci_device_id ixgbe_pci_tbl[] = {
67 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_DUAL_PORT),
3957d63d 68 board_82598 },
9a799d71 69 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_SINGLE_PORT),
3957d63d 70 board_82598 },
0befdb3e
JB
71 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AT),
72 board_82598 },
9a799d71 73 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_CX4),
3957d63d 74 board_82598 },
8d792cd9
JB
75 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_CX4_DUAL_PORT),
76 board_82598 },
c4900be0
DS
77 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_DA_DUAL_PORT),
78 board_82598 },
79 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM),
80 board_82598 },
b95f5fcb
JB
81 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_XF_LR),
82 board_82598 },
c4900be0
DS
83 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_SFP_LOM),
84 board_82598 },
9a799d71
AK
85
86 /* required last entry */
87 {0, }
88};
89MODULE_DEVICE_TABLE(pci, ixgbe_pci_tbl);
90
5dd2d332 91#ifdef CONFIG_IXGBE_DCA
bd0362dd 92static int ixgbe_notify_dca(struct notifier_block *, unsigned long event,
b4617240 93 void *p);
bd0362dd
JC
94static struct notifier_block dca_notifier = {
95 .notifier_call = ixgbe_notify_dca,
96 .next = NULL,
97 .priority = 0
98};
99#endif
100
9a799d71
AK
101MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
102MODULE_DESCRIPTION("Intel(R) 10 Gigabit PCI Express Network Driver");
103MODULE_LICENSE("GPL");
104MODULE_VERSION(DRV_VERSION);
105
106#define DEFAULT_DEBUG_LEVEL_SHIFT 3
107
5eba3699
AV
108static void ixgbe_release_hw_control(struct ixgbe_adapter *adapter)
109{
110 u32 ctrl_ext;
111
112 /* Let firmware take over control of h/w */
113 ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
114 IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
b4617240 115 ctrl_ext & ~IXGBE_CTRL_EXT_DRV_LOAD);
5eba3699
AV
116}
117
118static void ixgbe_get_hw_control(struct ixgbe_adapter *adapter)
119{
120 u32 ctrl_ext;
121
122 /* Let firmware know the driver has taken over */
123 ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
124 IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
b4617240 125 ctrl_ext | IXGBE_CTRL_EXT_DRV_LOAD);
5eba3699 126}
9a799d71 127
9a799d71 128static void ixgbe_set_ivar(struct ixgbe_adapter *adapter, u16 int_alloc_entry,
b4617240 129 u8 msix_vector)
9a799d71
AK
130{
131 u32 ivar, index;
132
133 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
134 index = (int_alloc_entry >> 2) & 0x1F;
135 ivar = IXGBE_READ_REG(&adapter->hw, IXGBE_IVAR(index));
136 ivar &= ~(0xFF << (8 * (int_alloc_entry & 0x3)));
137 ivar |= (msix_vector << (8 * (int_alloc_entry & 0x3)));
138 IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR(index), ivar);
139}
140
141static void ixgbe_unmap_and_free_tx_resource(struct ixgbe_adapter *adapter,
b4617240
PW
142 struct ixgbe_tx_buffer
143 *tx_buffer_info)
9a799d71
AK
144{
145 if (tx_buffer_info->dma) {
e01c31a5 146 pci_unmap_page(adapter->pdev, tx_buffer_info->dma,
b4617240 147 tx_buffer_info->length, PCI_DMA_TODEVICE);
9a799d71
AK
148 tx_buffer_info->dma = 0;
149 }
150 if (tx_buffer_info->skb) {
151 dev_kfree_skb_any(tx_buffer_info->skb);
152 tx_buffer_info->skb = NULL;
153 }
154 /* tx_buffer_info must be completely set up in the transmit path */
155}
156
157static inline bool ixgbe_check_tx_hang(struct ixgbe_adapter *adapter,
b4617240
PW
158 struct ixgbe_ring *tx_ring,
159 unsigned int eop)
9a799d71 160{
e01c31a5
JB
161 struct ixgbe_hw *hw = &adapter->hw;
162 u32 head, tail;
163
9a799d71 164 /* Detect a transmit hang in hardware, this serializes the
e01c31a5
JB
165 * check with the clearing of time_stamp and movement of eop */
166 head = IXGBE_READ_REG(hw, tx_ring->head);
167 tail = IXGBE_READ_REG(hw, tx_ring->tail);
9a799d71 168 adapter->detect_tx_hung = false;
e01c31a5
JB
169 if ((head != tail) &&
170 tx_ring->tx_buffer_info[eop].time_stamp &&
9a799d71
AK
171 time_after(jiffies, tx_ring->tx_buffer_info[eop].time_stamp + HZ) &&
172 !(IXGBE_READ_REG(&adapter->hw, IXGBE_TFCS) & IXGBE_TFCS_TXOFF)) {
173 /* detected Tx unit hang */
e01c31a5
JB
174 union ixgbe_adv_tx_desc *tx_desc;
175 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, eop);
9a799d71 176 DPRINTK(DRV, ERR, "Detected Tx Unit Hang\n"
e01c31a5
JB
177 " Tx Queue <%d>\n"
178 " TDH, TDT <%x>, <%x>\n"
9a799d71
AK
179 " next_to_use <%x>\n"
180 " next_to_clean <%x>\n"
181 "tx_buffer_info[next_to_clean]\n"
182 " time_stamp <%lx>\n"
e01c31a5
JB
183 " jiffies <%lx>\n",
184 tx_ring->queue_index,
185 head, tail,
186 tx_ring->next_to_use, eop,
187 tx_ring->tx_buffer_info[eop].time_stamp, jiffies);
9a799d71
AK
188 return true;
189 }
190
191 return false;
192}
193
b4617240
PW
194#define IXGBE_MAX_TXD_PWR 14
195#define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
e092be60
AV
196
197/* Tx Descriptors needed, worst case */
198#define TXD_USE_COUNT(S) (((S) >> IXGBE_MAX_TXD_PWR) + \
199 (((S) & (IXGBE_MAX_DATA_PER_TXD - 1)) ? 1 : 0))
200#define DESC_NEEDED (TXD_USE_COUNT(IXGBE_MAX_DATA_PER_TXD) /* skb->data */ + \
b4617240 201 MAX_SKB_FRAGS * TXD_USE_COUNT(PAGE_SIZE) + 1) /* for context */
e092be60 202
e01c31a5
JB
203#define GET_TX_HEAD_FROM_RING(ring) (\
204 *(volatile u32 *) \
205 ((union ixgbe_adv_tx_desc *)(ring)->desc + (ring)->count))
206static void ixgbe_tx_timeout(struct net_device *netdev);
207
9a799d71
AK
208/**
209 * ixgbe_clean_tx_irq - Reclaim resources after transmit completes
210 * @adapter: board private structure
e01c31a5 211 * @tx_ring: tx ring to clean
9a799d71
AK
212 **/
213static bool ixgbe_clean_tx_irq(struct ixgbe_adapter *adapter,
e01c31a5 214 struct ixgbe_ring *tx_ring)
9a799d71 215{
e01c31a5 216 union ixgbe_adv_tx_desc *tx_desc;
9a799d71 217 struct ixgbe_tx_buffer *tx_buffer_info;
e01c31a5
JB
218 struct net_device *netdev = adapter->netdev;
219 struct sk_buff *skb;
220 unsigned int i;
221 u32 head, oldhead;
222 unsigned int count = 0;
223 unsigned int total_bytes = 0, total_packets = 0;
9a799d71 224
e01c31a5
JB
225 rmb();
226 head = GET_TX_HEAD_FROM_RING(tx_ring);
227 head = le32_to_cpu(head);
9a799d71 228 i = tx_ring->next_to_clean;
e01c31a5
JB
229 while (1) {
230 while (i != head) {
9a799d71
AK
231 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
232 tx_buffer_info = &tx_ring->tx_buffer_info[i];
e01c31a5 233 skb = tx_buffer_info->skb;
9a799d71 234
e01c31a5 235 if (skb) {
e092be60 236 unsigned int segs, bytecount;
e01c31a5
JB
237
238 /* gso_segs is currently only valid for tcp */
e092be60
AV
239 segs = skb_shinfo(skb)->gso_segs ?: 1;
240 /* multiply data chunks by size of headers */
241 bytecount = ((segs - 1) * skb_headlen(skb)) +
e01c31a5
JB
242 skb->len;
243 total_packets += segs;
244 total_bytes += bytecount;
e092be60 245 }
e01c31a5 246
9a799d71 247 ixgbe_unmap_and_free_tx_resource(adapter,
e01c31a5 248 tx_buffer_info);
9a799d71
AK
249
250 i++;
251 if (i == tx_ring->count)
252 i = 0;
9a799d71 253
e01c31a5
JB
254 count++;
255 if (count == tx_ring->count)
256 goto done_cleaning;
257 }
258 oldhead = head;
259 rmb();
260 head = GET_TX_HEAD_FROM_RING(tx_ring);
261 head = le32_to_cpu(head);
262 if (head == oldhead)
263 goto done_cleaning;
264 } /* while (1) */
265
266done_cleaning:
9a799d71
AK
267 tx_ring->next_to_clean = i;
268
e092be60 269#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
e01c31a5
JB
270 if (unlikely(count && netif_carrier_ok(netdev) &&
271 (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
e092be60
AV
272 /* Make sure that anybody stopping the queue after this
273 * sees the new next_to_clean.
274 */
275 smp_mb();
30eba97a
AV
276 if (__netif_subqueue_stopped(netdev, tx_ring->queue_index) &&
277 !test_bit(__IXGBE_DOWN, &adapter->state)) {
278 netif_wake_subqueue(netdev, tx_ring->queue_index);
e01c31a5 279 ++adapter->restart_queue;
30eba97a 280 }
e092be60 281 }
9a799d71 282
e01c31a5
JB
283 if (adapter->detect_tx_hung) {
284 if (ixgbe_check_tx_hang(adapter, tx_ring, i)) {
285 /* schedule immediate reset if we believe we hung */
286 DPRINTK(PROBE, INFO,
287 "tx hang %d detected, resetting adapter\n",
288 adapter->tx_timeout_count + 1);
289 ixgbe_tx_timeout(adapter->netdev);
290 }
291 }
9a799d71 292
e01c31a5
JB
293 /* re-arm the interrupt */
294 if ((total_packets >= tx_ring->work_limit) ||
295 (count == tx_ring->count))
296 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS, tx_ring->v_idx);
9a799d71 297
e01c31a5
JB
298 tx_ring->total_bytes += total_bytes;
299 tx_ring->total_packets += total_packets;
300 tx_ring->stats.bytes += total_bytes;
301 tx_ring->stats.packets += total_packets;
302 adapter->net_stats.tx_bytes += total_bytes;
303 adapter->net_stats.tx_packets += total_packets;
304 return (total_packets ? true : false);
9a799d71
AK
305}
306
5dd2d332 307#ifdef CONFIG_IXGBE_DCA
bd0362dd 308static void ixgbe_update_rx_dca(struct ixgbe_adapter *adapter,
b4617240 309 struct ixgbe_ring *rx_ring)
bd0362dd
JC
310{
311 u32 rxctrl;
312 int cpu = get_cpu();
3a581073 313 int q = rx_ring - adapter->rx_ring;
bd0362dd 314
3a581073 315 if (rx_ring->cpu != cpu) {
bd0362dd
JC
316 rxctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_DCA_RXCTRL(q));
317 rxctrl &= ~IXGBE_DCA_RXCTRL_CPUID_MASK;
96b0e0f6 318 rxctrl |= dca3_get_tag(&adapter->pdev->dev, cpu);
bd0362dd
JC
319 rxctrl |= IXGBE_DCA_RXCTRL_DESC_DCA_EN;
320 rxctrl |= IXGBE_DCA_RXCTRL_HEAD_DCA_EN;
321 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_RXCTRL(q), rxctrl);
3a581073 322 rx_ring->cpu = cpu;
bd0362dd
JC
323 }
324 put_cpu();
325}
326
327static void ixgbe_update_tx_dca(struct ixgbe_adapter *adapter,
b4617240 328 struct ixgbe_ring *tx_ring)
bd0362dd
JC
329{
330 u32 txctrl;
331 int cpu = get_cpu();
3a581073 332 int q = tx_ring - adapter->tx_ring;
bd0362dd 333
3a581073 334 if (tx_ring->cpu != cpu) {
bd0362dd
JC
335 txctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_DCA_TXCTRL(q));
336 txctrl &= ~IXGBE_DCA_TXCTRL_CPUID_MASK;
96b0e0f6 337 txctrl |= dca3_get_tag(&adapter->pdev->dev, cpu);
bd0362dd
JC
338 txctrl |= IXGBE_DCA_TXCTRL_DESC_DCA_EN;
339 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_TXCTRL(q), txctrl);
3a581073 340 tx_ring->cpu = cpu;
bd0362dd
JC
341 }
342 put_cpu();
343}
344
345static void ixgbe_setup_dca(struct ixgbe_adapter *adapter)
346{
347 int i;
348
349 if (!(adapter->flags & IXGBE_FLAG_DCA_ENABLED))
350 return;
351
352 for (i = 0; i < adapter->num_tx_queues; i++) {
353 adapter->tx_ring[i].cpu = -1;
354 ixgbe_update_tx_dca(adapter, &adapter->tx_ring[i]);
355 }
356 for (i = 0; i < adapter->num_rx_queues; i++) {
357 adapter->rx_ring[i].cpu = -1;
358 ixgbe_update_rx_dca(adapter, &adapter->rx_ring[i]);
359 }
360}
361
362static int __ixgbe_notify_dca(struct device *dev, void *data)
363{
364 struct net_device *netdev = dev_get_drvdata(dev);
365 struct ixgbe_adapter *adapter = netdev_priv(netdev);
366 unsigned long event = *(unsigned long *)data;
367
368 switch (event) {
369 case DCA_PROVIDER_ADD:
96b0e0f6
JB
370 /* if we're already enabled, don't do it again */
371 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
372 break;
bd0362dd
JC
373 /* Always use CB2 mode, difference is masked
374 * in the CB driver. */
375 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 2);
652f093f 376 if (dca_add_requester(dev) == 0) {
96b0e0f6 377 adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
bd0362dd
JC
378 ixgbe_setup_dca(adapter);
379 break;
380 }
381 /* Fall Through since DCA is disabled. */
382 case DCA_PROVIDER_REMOVE:
383 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
384 dca_remove_requester(dev);
385 adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
386 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 1);
387 }
388 break;
389 }
390
652f093f 391 return 0;
bd0362dd
JC
392}
393
5dd2d332 394#endif /* CONFIG_IXGBE_DCA */
9a799d71
AK
395/**
396 * ixgbe_receive_skb - Send a completed packet up the stack
397 * @adapter: board private structure
398 * @skb: packet to send up
177db6ff
MC
399 * @status: hardware indication of status of receive
400 * @rx_ring: rx descriptor ring (for a specific queue) to setup
401 * @rx_desc: rx descriptor
9a799d71
AK
402 **/
403static void ixgbe_receive_skb(struct ixgbe_adapter *adapter,
b4617240
PW
404 struct sk_buff *skb, u8 status,
405 struct ixgbe_ring *ring,
177db6ff 406 union ixgbe_adv_rx_desc *rx_desc)
9a799d71 407{
177db6ff
MC
408 bool is_vlan = (status & IXGBE_RXD_STAT_VP);
409 u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
9a799d71 410
177db6ff
MC
411 if (adapter->netdev->features & NETIF_F_LRO &&
412 skb->ip_summed == CHECKSUM_UNNECESSARY) {
2f90b865 413 if (adapter->vlgrp && is_vlan && (tag != 0))
177db6ff
MC
414 lro_vlan_hwaccel_receive_skb(&ring->lro_mgr, skb,
415 adapter->vlgrp, tag,
416 rx_desc);
9a799d71 417 else
177db6ff
MC
418 lro_receive_skb(&ring->lro_mgr, skb, rx_desc);
419 ring->lro_used = true;
420 } else {
421 if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL)) {
2f90b865 422 if (adapter->vlgrp && is_vlan && (tag != 0))
177db6ff
MC
423 vlan_hwaccel_receive_skb(skb, adapter->vlgrp, tag);
424 else
425 netif_receive_skb(skb);
426 } else {
2f90b865 427 if (adapter->vlgrp && is_vlan && (tag != 0))
177db6ff
MC
428 vlan_hwaccel_rx(skb, adapter->vlgrp, tag);
429 else
430 netif_rx(skb);
431 }
9a799d71
AK
432 }
433}
434
e59bd25d
AV
435/**
436 * ixgbe_rx_checksum - indicate in skb if hw indicated a good cksum
437 * @adapter: address of board private structure
438 * @status_err: hardware indication of status of receive
439 * @skb: skb currently being received and modified
440 **/
9a799d71 441static inline void ixgbe_rx_checksum(struct ixgbe_adapter *adapter,
712744be 442 u32 status_err, struct sk_buff *skb)
9a799d71
AK
443{
444 skb->ip_summed = CHECKSUM_NONE;
445
712744be
JB
446 /* Rx csum disabled */
447 if (!(adapter->flags & IXGBE_FLAG_RX_CSUM_ENABLED))
9a799d71 448 return;
e59bd25d
AV
449
450 /* if IP and error */
451 if ((status_err & IXGBE_RXD_STAT_IPCS) &&
452 (status_err & IXGBE_RXDADV_ERR_IPE)) {
9a799d71
AK
453 adapter->hw_csum_rx_error++;
454 return;
455 }
e59bd25d
AV
456
457 if (!(status_err & IXGBE_RXD_STAT_L4CS))
458 return;
459
460 if (status_err & IXGBE_RXDADV_ERR_TCPE) {
461 adapter->hw_csum_rx_error++;
462 return;
463 }
464
9a799d71 465 /* It must be a TCP or UDP packet with a valid checksum */
e59bd25d 466 skb->ip_summed = CHECKSUM_UNNECESSARY;
9a799d71
AK
467 adapter->hw_csum_rx_good++;
468}
469
470/**
471 * ixgbe_alloc_rx_buffers - Replace used receive buffers; packet split
472 * @adapter: address of board private structure
473 **/
474static void ixgbe_alloc_rx_buffers(struct ixgbe_adapter *adapter,
7c6e0a43
JB
475 struct ixgbe_ring *rx_ring,
476 int cleaned_count)
9a799d71 477{
9a799d71
AK
478 struct pci_dev *pdev = adapter->pdev;
479 union ixgbe_adv_rx_desc *rx_desc;
3a581073 480 struct ixgbe_rx_buffer *bi;
9a799d71 481 unsigned int i;
7c6e0a43 482 unsigned int bufsz = rx_ring->rx_buf_len + NET_IP_ALIGN;
9a799d71
AK
483
484 i = rx_ring->next_to_use;
3a581073 485 bi = &rx_ring->rx_buffer_info[i];
9a799d71
AK
486
487 while (cleaned_count--) {
488 rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
489
762f4c57 490 if (!bi->page_dma &&
3a581073 491 (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED)) {
3a581073 492 if (!bi->page) {
762f4c57
JB
493 bi->page = alloc_page(GFP_ATOMIC);
494 if (!bi->page) {
495 adapter->alloc_rx_page_failed++;
496 goto no_buffers;
497 }
498 bi->page_offset = 0;
499 } else {
500 /* use a half page if we're re-using */
501 bi->page_offset ^= (PAGE_SIZE / 2);
9a799d71 502 }
762f4c57
JB
503
504 bi->page_dma = pci_map_page(pdev, bi->page,
505 bi->page_offset,
506 (PAGE_SIZE / 2),
507 PCI_DMA_FROMDEVICE);
9a799d71
AK
508 }
509
3a581073 510 if (!bi->skb) {
74ce8dd2
JB
511 struct sk_buff *skb = netdev_alloc_skb(adapter->netdev,
512 bufsz);
9a799d71
AK
513
514 if (!skb) {
515 adapter->alloc_rx_buff_failed++;
516 goto no_buffers;
517 }
518
519 /*
520 * Make buffer alignment 2 beyond a 16 byte boundary
521 * this will result in a 16 byte aligned IP header after
522 * the 14 byte MAC header is removed
523 */
524 skb_reserve(skb, NET_IP_ALIGN);
525
3a581073
JB
526 bi->skb = skb;
527 bi->dma = pci_map_single(pdev, skb->data, bufsz,
528 PCI_DMA_FROMDEVICE);
9a799d71
AK
529 }
530 /* Refresh the desc even if buffer_addrs didn't change because
531 * each write-back erases this info. */
532 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
3a581073
JB
533 rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
534 rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
9a799d71 535 } else {
3a581073 536 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
9a799d71
AK
537 }
538
539 i++;
540 if (i == rx_ring->count)
541 i = 0;
3a581073 542 bi = &rx_ring->rx_buffer_info[i];
9a799d71 543 }
7c6e0a43 544
9a799d71
AK
545no_buffers:
546 if (rx_ring->next_to_use != i) {
547 rx_ring->next_to_use = i;
548 if (i-- == 0)
549 i = (rx_ring->count - 1);
550
551 /*
552 * Force memory writes to complete before letting h/w
553 * know there are new descriptors to fetch. (Only
554 * applicable for weak-ordered memory model archs,
555 * such as IA-64).
556 */
557 wmb();
558 writel(i, adapter->hw.hw_addr + rx_ring->tail);
559 }
560}
561
7c6e0a43
JB
562static inline u16 ixgbe_get_hdr_info(union ixgbe_adv_rx_desc *rx_desc)
563{
564 return rx_desc->wb.lower.lo_dword.hs_rss.hdr_info;
565}
566
567static inline u16 ixgbe_get_pkt_info(union ixgbe_adv_rx_desc *rx_desc)
568{
569 return rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
570}
571
9a799d71 572static bool ixgbe_clean_rx_irq(struct ixgbe_adapter *adapter,
b4617240
PW
573 struct ixgbe_ring *rx_ring,
574 int *work_done, int work_to_do)
9a799d71 575{
9a799d71
AK
576 struct pci_dev *pdev = adapter->pdev;
577 union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
578 struct ixgbe_rx_buffer *rx_buffer_info, *next_buffer;
579 struct sk_buff *skb;
580 unsigned int i;
7c6e0a43 581 u32 len, staterr;
177db6ff
MC
582 u16 hdr_info;
583 bool cleaned = false;
9a799d71 584 int cleaned_count = 0;
d2f4fbe2 585 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
9a799d71
AK
586
587 i = rx_ring->next_to_clean;
9a799d71
AK
588 rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
589 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
590 rx_buffer_info = &rx_ring->rx_buffer_info[i];
9a799d71
AK
591
592 while (staterr & IXGBE_RXD_STAT_DD) {
7c6e0a43 593 u32 upper_len = 0;
9a799d71
AK
594 if (*work_done >= work_to_do)
595 break;
596 (*work_done)++;
597
598 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
7c6e0a43
JB
599 hdr_info = le16_to_cpu(ixgbe_get_hdr_info(rx_desc));
600 len = (hdr_info & IXGBE_RXDADV_HDRBUFLEN_MASK) >>
762f4c57 601 IXGBE_RXDADV_HDRBUFLEN_SHIFT;
9a799d71
AK
602 if (hdr_info & IXGBE_RXDADV_SPH)
603 adapter->rx_hdr_split++;
604 if (len > IXGBE_RX_HDR_SIZE)
605 len = IXGBE_RX_HDR_SIZE;
606 upper_len = le16_to_cpu(rx_desc->wb.upper.length);
7c6e0a43 607 } else {
9a799d71 608 len = le16_to_cpu(rx_desc->wb.upper.length);
7c6e0a43 609 }
9a799d71
AK
610
611 cleaned = true;
612 skb = rx_buffer_info->skb;
613 prefetch(skb->data - NET_IP_ALIGN);
614 rx_buffer_info->skb = NULL;
615
616 if (len && !skb_shinfo(skb)->nr_frags) {
617 pci_unmap_single(pdev, rx_buffer_info->dma,
b4617240
PW
618 rx_ring->rx_buf_len + NET_IP_ALIGN,
619 PCI_DMA_FROMDEVICE);
9a799d71
AK
620 skb_put(skb, len);
621 }
622
623 if (upper_len) {
624 pci_unmap_page(pdev, rx_buffer_info->page_dma,
762f4c57 625 PAGE_SIZE / 2, PCI_DMA_FROMDEVICE);
9a799d71
AK
626 rx_buffer_info->page_dma = 0;
627 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
762f4c57
JB
628 rx_buffer_info->page,
629 rx_buffer_info->page_offset,
630 upper_len);
631
632 if ((rx_ring->rx_buf_len > (PAGE_SIZE / 2)) ||
633 (page_count(rx_buffer_info->page) != 1))
634 rx_buffer_info->page = NULL;
635 else
636 get_page(rx_buffer_info->page);
9a799d71
AK
637
638 skb->len += upper_len;
639 skb->data_len += upper_len;
640 skb->truesize += upper_len;
641 }
642
643 i++;
644 if (i == rx_ring->count)
645 i = 0;
646 next_buffer = &rx_ring->rx_buffer_info[i];
647
648 next_rxd = IXGBE_RX_DESC_ADV(*rx_ring, i);
649 prefetch(next_rxd);
650
651 cleaned_count++;
652 if (staterr & IXGBE_RXD_STAT_EOP) {
653 rx_ring->stats.packets++;
654 rx_ring->stats.bytes += skb->len;
655 } else {
656 rx_buffer_info->skb = next_buffer->skb;
657 rx_buffer_info->dma = next_buffer->dma;
658 next_buffer->skb = skb;
762f4c57 659 next_buffer->dma = 0;
9a799d71
AK
660 adapter->non_eop_descs++;
661 goto next_desc;
662 }
663
664 if (staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK) {
665 dev_kfree_skb_irq(skb);
666 goto next_desc;
667 }
668
669 ixgbe_rx_checksum(adapter, staterr, skb);
d2f4fbe2
AV
670
671 /* probably a little skewed due to removing CRC */
672 total_rx_bytes += skb->len;
673 total_rx_packets++;
674
74ce8dd2 675 skb->protocol = eth_type_trans(skb, adapter->netdev);
177db6ff 676 ixgbe_receive_skb(adapter, skb, staterr, rx_ring, rx_desc);
9a799d71
AK
677
678next_desc:
679 rx_desc->wb.upper.status_error = 0;
680
681 /* return some buffers to hardware, one at a time is too slow */
682 if (cleaned_count >= IXGBE_RX_BUFFER_WRITE) {
683 ixgbe_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
684 cleaned_count = 0;
685 }
686
687 /* use prefetched values */
688 rx_desc = next_rxd;
689 rx_buffer_info = next_buffer;
690
691 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
177db6ff
MC
692 }
693
694 if (rx_ring->lro_used) {
695 lro_flush_all(&rx_ring->lro_mgr);
696 rx_ring->lro_used = false;
9a799d71
AK
697 }
698
699 rx_ring->next_to_clean = i;
700 cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
701
702 if (cleaned_count)
703 ixgbe_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
704
f494e8fa
AV
705 rx_ring->total_packets += total_rx_packets;
706 rx_ring->total_bytes += total_rx_bytes;
707 adapter->net_stats.rx_bytes += total_rx_bytes;
708 adapter->net_stats.rx_packets += total_rx_packets;
709
9a799d71
AK
710 return cleaned;
711}
712
021230d4 713static int ixgbe_clean_rxonly(struct napi_struct *, int);
9a799d71
AK
714/**
715 * ixgbe_configure_msix - Configure MSI-X hardware
716 * @adapter: board private structure
717 *
718 * ixgbe_configure_msix sets up the hardware to properly generate MSI-X
719 * interrupts.
720 **/
721static void ixgbe_configure_msix(struct ixgbe_adapter *adapter)
722{
021230d4
AV
723 struct ixgbe_q_vector *q_vector;
724 int i, j, q_vectors, v_idx, r_idx;
725 u32 mask;
9a799d71 726
021230d4 727 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
9a799d71 728
021230d4
AV
729 /* Populate the IVAR table and set the ITR values to the
730 * corresponding register.
731 */
732 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
733 q_vector = &adapter->q_vector[v_idx];
734 /* XXX for_each_bit(...) */
735 r_idx = find_first_bit(q_vector->rxr_idx,
b4617240 736 adapter->num_rx_queues);
021230d4
AV
737
738 for (i = 0; i < q_vector->rxr_count; i++) {
739 j = adapter->rx_ring[r_idx].reg_idx;
740 ixgbe_set_ivar(adapter, IXGBE_IVAR_RX_QUEUE(j), v_idx);
741 r_idx = find_next_bit(q_vector->rxr_idx,
b4617240
PW
742 adapter->num_rx_queues,
743 r_idx + 1);
021230d4
AV
744 }
745 r_idx = find_first_bit(q_vector->txr_idx,
b4617240 746 adapter->num_tx_queues);
021230d4
AV
747
748 for (i = 0; i < q_vector->txr_count; i++) {
749 j = adapter->tx_ring[r_idx].reg_idx;
750 ixgbe_set_ivar(adapter, IXGBE_IVAR_TX_QUEUE(j), v_idx);
751 r_idx = find_next_bit(q_vector->txr_idx,
b4617240
PW
752 adapter->num_tx_queues,
753 r_idx + 1);
021230d4
AV
754 }
755
30efa5a3 756 /* if this is a tx only vector halve the interrupt rate */
021230d4 757 if (q_vector->txr_count && !q_vector->rxr_count)
30efa5a3 758 q_vector->eitr = (adapter->eitr_param >> 1);
021230d4 759 else
30efa5a3
JB
760 /* rx only */
761 q_vector->eitr = adapter->eitr_param;
021230d4
AV
762
763 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(v_idx),
b4617240 764 EITR_INTS_PER_SEC_TO_REG(q_vector->eitr));
9a799d71
AK
765 }
766
021230d4
AV
767 ixgbe_set_ivar(adapter, IXGBE_IVAR_OTHER_CAUSES_INDEX, v_idx);
768 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(v_idx), 1950);
769
41fb9248 770 /* set up to autoclear timer, and the vectors */
021230d4 771 mask = IXGBE_EIMS_ENABLE_MASK;
41fb9248 772 mask &= ~(IXGBE_EIMS_OTHER | IXGBE_EIMS_LSC);
021230d4 773 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC, mask);
9a799d71
AK
774}
775
f494e8fa
AV
776enum latency_range {
777 lowest_latency = 0,
778 low_latency = 1,
779 bulk_latency = 2,
780 latency_invalid = 255
781};
782
783/**
784 * ixgbe_update_itr - update the dynamic ITR value based on statistics
785 * @adapter: pointer to adapter
786 * @eitr: eitr setting (ints per sec) to give last timeslice
787 * @itr_setting: current throttle rate in ints/second
788 * @packets: the number of packets during this measurement interval
789 * @bytes: the number of bytes during this measurement interval
790 *
791 * Stores a new ITR value based on packets and byte
792 * counts during the last interrupt. The advantage of per interrupt
793 * computation is faster updates and more accurate ITR for the current
794 * traffic pattern. Constants in this function were computed
795 * based on theoretical maximum wire speed and thresholds were set based
796 * on testing data as well as attempting to minimize response time
797 * while increasing bulk throughput.
798 * this functionality is controlled by the InterruptThrottleRate module
799 * parameter (see ixgbe_param.c)
800 **/
801static u8 ixgbe_update_itr(struct ixgbe_adapter *adapter,
b4617240
PW
802 u32 eitr, u8 itr_setting,
803 int packets, int bytes)
f494e8fa
AV
804{
805 unsigned int retval = itr_setting;
806 u32 timepassed_us;
807 u64 bytes_perint;
808
809 if (packets == 0)
810 goto update_itr_done;
811
812
813 /* simple throttlerate management
814 * 0-20MB/s lowest (100000 ints/s)
815 * 20-100MB/s low (20000 ints/s)
816 * 100-1249MB/s bulk (8000 ints/s)
817 */
818 /* what was last interrupt timeslice? */
819 timepassed_us = 1000000/eitr;
820 bytes_perint = bytes / timepassed_us; /* bytes/usec */
821
822 switch (itr_setting) {
823 case lowest_latency:
824 if (bytes_perint > adapter->eitr_low)
825 retval = low_latency;
826 break;
827 case low_latency:
828 if (bytes_perint > adapter->eitr_high)
829 retval = bulk_latency;
830 else if (bytes_perint <= adapter->eitr_low)
831 retval = lowest_latency;
832 break;
833 case bulk_latency:
834 if (bytes_perint <= adapter->eitr_high)
835 retval = low_latency;
836 break;
837 }
838
839update_itr_done:
840 return retval;
841}
842
843static void ixgbe_set_itr_msix(struct ixgbe_q_vector *q_vector)
844{
845 struct ixgbe_adapter *adapter = q_vector->adapter;
846 struct ixgbe_hw *hw = &adapter->hw;
847 u32 new_itr;
848 u8 current_itr, ret_itr;
849 int i, r_idx, v_idx = ((void *)q_vector - (void *)(adapter->q_vector)) /
b4617240 850 sizeof(struct ixgbe_q_vector);
f494e8fa
AV
851 struct ixgbe_ring *rx_ring, *tx_ring;
852
853 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
854 for (i = 0; i < q_vector->txr_count; i++) {
855 tx_ring = &(adapter->tx_ring[r_idx]);
856 ret_itr = ixgbe_update_itr(adapter, q_vector->eitr,
b4617240
PW
857 q_vector->tx_itr,
858 tx_ring->total_packets,
859 tx_ring->total_bytes);
f494e8fa
AV
860 /* if the result for this queue would decrease interrupt
861 * rate for this vector then use that result */
30efa5a3 862 q_vector->tx_itr = ((q_vector->tx_itr > ret_itr) ?
b4617240 863 q_vector->tx_itr - 1 : ret_itr);
f494e8fa 864 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
b4617240 865 r_idx + 1);
f494e8fa
AV
866 }
867
868 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
869 for (i = 0; i < q_vector->rxr_count; i++) {
870 rx_ring = &(adapter->rx_ring[r_idx]);
871 ret_itr = ixgbe_update_itr(adapter, q_vector->eitr,
b4617240
PW
872 q_vector->rx_itr,
873 rx_ring->total_packets,
874 rx_ring->total_bytes);
f494e8fa
AV
875 /* if the result for this queue would decrease interrupt
876 * rate for this vector then use that result */
30efa5a3 877 q_vector->rx_itr = ((q_vector->rx_itr > ret_itr) ?
b4617240 878 q_vector->rx_itr - 1 : ret_itr);
f494e8fa 879 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
b4617240 880 r_idx + 1);
f494e8fa
AV
881 }
882
30efa5a3 883 current_itr = max(q_vector->rx_itr, q_vector->tx_itr);
f494e8fa
AV
884
885 switch (current_itr) {
886 /* counts and packets in update_itr are dependent on these numbers */
887 case lowest_latency:
888 new_itr = 100000;
889 break;
890 case low_latency:
891 new_itr = 20000; /* aka hwitr = ~200 */
892 break;
893 case bulk_latency:
894 default:
895 new_itr = 8000;
896 break;
897 }
898
899 if (new_itr != q_vector->eitr) {
900 u32 itr_reg;
901 /* do an exponential smoothing */
902 new_itr = ((q_vector->eitr * 90)/100) + ((new_itr * 10)/100);
903 q_vector->eitr = new_itr;
904 itr_reg = EITR_INTS_PER_SEC_TO_REG(new_itr);
905 /* must write high and low 16 bits to reset counter */
906 DPRINTK(TX_ERR, DEBUG, "writing eitr(%d): %08X\n", v_idx,
b4617240 907 itr_reg);
f494e8fa
AV
908 IXGBE_WRITE_REG(hw, IXGBE_EITR(v_idx), itr_reg | (itr_reg)<<16);
909 }
910
911 return;
912}
913
0befdb3e
JB
914static void ixgbe_check_fan_failure(struct ixgbe_adapter *adapter, u32 eicr)
915{
916 struct ixgbe_hw *hw = &adapter->hw;
917
918 if ((adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE) &&
919 (eicr & IXGBE_EICR_GPI_SDP1)) {
920 DPRINTK(PROBE, CRIT, "Fan has stopped, replace the adapter\n");
921 /* write to clear the interrupt */
922 IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1);
923 }
924}
cf8280ee
JB
925
926static void ixgbe_check_lsc(struct ixgbe_adapter *adapter)
927{
928 struct ixgbe_hw *hw = &adapter->hw;
929
930 adapter->lsc_int++;
931 adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
932 adapter->link_check_timeout = jiffies;
933 if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
934 IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
935 schedule_work(&adapter->watchdog_task);
936 }
937}
938
9a799d71
AK
939static irqreturn_t ixgbe_msix_lsc(int irq, void *data)
940{
941 struct net_device *netdev = data;
942 struct ixgbe_adapter *adapter = netdev_priv(netdev);
943 struct ixgbe_hw *hw = &adapter->hw;
944 u32 eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
945
cf8280ee
JB
946 if (eicr & IXGBE_EICR_LSC)
947 ixgbe_check_lsc(adapter);
d4f80882 948
0befdb3e
JB
949 ixgbe_check_fan_failure(adapter, eicr);
950
d4f80882
AV
951 if (!test_bit(__IXGBE_DOWN, &adapter->state))
952 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_OTHER);
9a799d71
AK
953
954 return IRQ_HANDLED;
955}
956
957static irqreturn_t ixgbe_msix_clean_tx(int irq, void *data)
958{
021230d4
AV
959 struct ixgbe_q_vector *q_vector = data;
960 struct ixgbe_adapter *adapter = q_vector->adapter;
3a581073 961 struct ixgbe_ring *tx_ring;
021230d4
AV
962 int i, r_idx;
963
964 if (!q_vector->txr_count)
965 return IRQ_HANDLED;
966
967 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
968 for (i = 0; i < q_vector->txr_count; i++) {
3a581073 969 tx_ring = &(adapter->tx_ring[r_idx]);
5dd2d332 970#ifdef CONFIG_IXGBE_DCA
bd0362dd 971 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
3a581073 972 ixgbe_update_tx_dca(adapter, tx_ring);
bd0362dd 973#endif
3a581073
JB
974 tx_ring->total_bytes = 0;
975 tx_ring->total_packets = 0;
976 ixgbe_clean_tx_irq(adapter, tx_ring);
021230d4 977 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
b4617240 978 r_idx + 1);
021230d4 979 }
9a799d71 980
9a799d71
AK
981 return IRQ_HANDLED;
982}
983
021230d4
AV
984/**
985 * ixgbe_msix_clean_rx - single unshared vector rx clean (all queues)
986 * @irq: unused
987 * @data: pointer to our q_vector struct for this interrupt vector
988 **/
9a799d71
AK
989static irqreturn_t ixgbe_msix_clean_rx(int irq, void *data)
990{
021230d4
AV
991 struct ixgbe_q_vector *q_vector = data;
992 struct ixgbe_adapter *adapter = q_vector->adapter;
3a581073 993 struct ixgbe_ring *rx_ring;
021230d4 994 int r_idx;
30efa5a3 995 int i;
021230d4
AV
996
997 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
30efa5a3
JB
998 for (i = 0; i < q_vector->rxr_count; i++) {
999 rx_ring = &(adapter->rx_ring[r_idx]);
1000 rx_ring->total_bytes = 0;
1001 rx_ring->total_packets = 0;
1002 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
1003 r_idx + 1);
1004 }
1005
021230d4
AV
1006 if (!q_vector->rxr_count)
1007 return IRQ_HANDLED;
1008
30efa5a3 1009 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
3a581073 1010 rx_ring = &(adapter->rx_ring[r_idx]);
021230d4 1011 /* disable interrupts on this vector only */
3a581073 1012 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, rx_ring->v_idx);
021230d4
AV
1013 netif_rx_schedule(adapter->netdev, &q_vector->napi);
1014
1015 return IRQ_HANDLED;
1016}
1017
1018static irqreturn_t ixgbe_msix_clean_many(int irq, void *data)
1019{
1020 ixgbe_msix_clean_rx(irq, data);
1021 ixgbe_msix_clean_tx(irq, data);
9a799d71 1022
9a799d71
AK
1023 return IRQ_HANDLED;
1024}
1025
021230d4
AV
1026/**
1027 * ixgbe_clean_rxonly - msix (aka one shot) rx clean routine
1028 * @napi: napi struct with our devices info in it
1029 * @budget: amount of work driver is allowed to do this pass, in packets
1030 *
f0848276
JB
1031 * This function is optimized for cleaning one queue only on a single
1032 * q_vector!!!
021230d4 1033 **/
9a799d71
AK
1034static int ixgbe_clean_rxonly(struct napi_struct *napi, int budget)
1035{
021230d4 1036 struct ixgbe_q_vector *q_vector =
b4617240 1037 container_of(napi, struct ixgbe_q_vector, napi);
021230d4 1038 struct ixgbe_adapter *adapter = q_vector->adapter;
f0848276 1039 struct ixgbe_ring *rx_ring = NULL;
9a799d71 1040 int work_done = 0;
021230d4 1041 long r_idx;
9a799d71 1042
021230d4 1043 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
3a581073 1044 rx_ring = &(adapter->rx_ring[r_idx]);
5dd2d332 1045#ifdef CONFIG_IXGBE_DCA
bd0362dd 1046 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
3a581073 1047 ixgbe_update_rx_dca(adapter, rx_ring);
bd0362dd 1048#endif
9a799d71 1049
3a581073 1050 ixgbe_clean_rx_irq(adapter, rx_ring, &work_done, budget);
9a799d71 1051
021230d4
AV
1052 /* If all Rx work done, exit the polling mode */
1053 if (work_done < budget) {
1054 netif_rx_complete(adapter->netdev, napi);
30efa5a3 1055 if (adapter->itr_setting & 3)
f494e8fa 1056 ixgbe_set_itr_msix(q_vector);
9a799d71 1057 if (!test_bit(__IXGBE_DOWN, &adapter->state))
3a581073 1058 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, rx_ring->v_idx);
9a799d71
AK
1059 }
1060
1061 return work_done;
1062}
1063
f0848276
JB
1064/**
1065 * ixgbe_clean_rxonly_many - msix (aka one shot) rx clean routine
1066 * @napi: napi struct with our devices info in it
1067 * @budget: amount of work driver is allowed to do this pass, in packets
1068 *
1069 * This function will clean more than one rx queue associated with a
1070 * q_vector.
1071 **/
1072static int ixgbe_clean_rxonly_many(struct napi_struct *napi, int budget)
1073{
1074 struct ixgbe_q_vector *q_vector =
1075 container_of(napi, struct ixgbe_q_vector, napi);
1076 struct ixgbe_adapter *adapter = q_vector->adapter;
f0848276
JB
1077 struct ixgbe_ring *rx_ring = NULL;
1078 int work_done = 0, i;
1079 long r_idx;
1080 u16 enable_mask = 0;
1081
1082 /* attempt to distribute budget to each queue fairly, but don't allow
1083 * the budget to go below 1 because we'll exit polling */
1084 budget /= (q_vector->rxr_count ?: 1);
1085 budget = max(budget, 1);
1086 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
1087 for (i = 0; i < q_vector->rxr_count; i++) {
1088 rx_ring = &(adapter->rx_ring[r_idx]);
5dd2d332 1089#ifdef CONFIG_IXGBE_DCA
f0848276
JB
1090 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
1091 ixgbe_update_rx_dca(adapter, rx_ring);
1092#endif
1093 ixgbe_clean_rx_irq(adapter, rx_ring, &work_done, budget);
1094 enable_mask |= rx_ring->v_idx;
1095 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
1096 r_idx + 1);
1097 }
1098
1099 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
1100 rx_ring = &(adapter->rx_ring[r_idx]);
1101 /* If all Rx work done, exit the polling mode */
7f821875 1102 if (work_done < budget) {
74ce8dd2 1103 netif_rx_complete(adapter->netdev, napi);
f0848276
JB
1104 if (adapter->itr_setting & 3)
1105 ixgbe_set_itr_msix(q_vector);
1106 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1107 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, enable_mask);
1108 return 0;
1109 }
1110
1111 return work_done;
1112}
021230d4 1113static inline void map_vector_to_rxq(struct ixgbe_adapter *a, int v_idx,
b4617240 1114 int r_idx)
021230d4
AV
1115{
1116 a->q_vector[v_idx].adapter = a;
1117 set_bit(r_idx, a->q_vector[v_idx].rxr_idx);
1118 a->q_vector[v_idx].rxr_count++;
1119 a->rx_ring[r_idx].v_idx = 1 << v_idx;
1120}
1121
1122static inline void map_vector_to_txq(struct ixgbe_adapter *a, int v_idx,
b4617240 1123 int r_idx)
021230d4
AV
1124{
1125 a->q_vector[v_idx].adapter = a;
1126 set_bit(r_idx, a->q_vector[v_idx].txr_idx);
1127 a->q_vector[v_idx].txr_count++;
1128 a->tx_ring[r_idx].v_idx = 1 << v_idx;
1129}
1130
9a799d71 1131/**
021230d4
AV
1132 * ixgbe_map_rings_to_vectors - Maps descriptor rings to vectors
1133 * @adapter: board private structure to initialize
1134 * @vectors: allotted vector count for descriptor rings
9a799d71 1135 *
021230d4
AV
1136 * This function maps descriptor rings to the queue-specific vectors
1137 * we were allotted through the MSI-X enabling code. Ideally, we'd have
1138 * one vector per ring/queue, but on a constrained vector budget, we
1139 * group the rings as "efficiently" as possible. You would add new
1140 * mapping configurations in here.
9a799d71 1141 **/
021230d4 1142static int ixgbe_map_rings_to_vectors(struct ixgbe_adapter *adapter,
b4617240 1143 int vectors)
021230d4
AV
1144{
1145 int v_start = 0;
1146 int rxr_idx = 0, txr_idx = 0;
1147 int rxr_remaining = adapter->num_rx_queues;
1148 int txr_remaining = adapter->num_tx_queues;
1149 int i, j;
1150 int rqpv, tqpv;
1151 int err = 0;
1152
1153 /* No mapping required if MSI-X is disabled. */
1154 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
1155 goto out;
9a799d71 1156
021230d4
AV
1157 /*
1158 * The ideal configuration...
1159 * We have enough vectors to map one per queue.
1160 */
1161 if (vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
1162 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
1163 map_vector_to_rxq(adapter, v_start, rxr_idx);
9a799d71 1164
021230d4
AV
1165 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
1166 map_vector_to_txq(adapter, v_start, txr_idx);
9a799d71 1167
9a799d71 1168 goto out;
021230d4 1169 }
9a799d71 1170
021230d4
AV
1171 /*
1172 * If we don't have enough vectors for a 1-to-1
1173 * mapping, we'll have to group them so there are
1174 * multiple queues per vector.
1175 */
1176 /* Re-adjusting *qpv takes care of the remainder. */
1177 for (i = v_start; i < vectors; i++) {
1178 rqpv = DIV_ROUND_UP(rxr_remaining, vectors - i);
1179 for (j = 0; j < rqpv; j++) {
1180 map_vector_to_rxq(adapter, i, rxr_idx);
1181 rxr_idx++;
1182 rxr_remaining--;
1183 }
1184 }
1185 for (i = v_start; i < vectors; i++) {
1186 tqpv = DIV_ROUND_UP(txr_remaining, vectors - i);
1187 for (j = 0; j < tqpv; j++) {
1188 map_vector_to_txq(adapter, i, txr_idx);
1189 txr_idx++;
1190 txr_remaining--;
9a799d71 1191 }
9a799d71
AK
1192 }
1193
021230d4
AV
1194out:
1195 return err;
1196}
1197
1198/**
1199 * ixgbe_request_msix_irqs - Initialize MSI-X interrupts
1200 * @adapter: board private structure
1201 *
1202 * ixgbe_request_msix_irqs allocates MSI-X vectors and requests
1203 * interrupts from the kernel.
1204 **/
1205static int ixgbe_request_msix_irqs(struct ixgbe_adapter *adapter)
1206{
1207 struct net_device *netdev = adapter->netdev;
1208 irqreturn_t (*handler)(int, void *);
1209 int i, vector, q_vectors, err;
cb13fc20 1210 int ri=0, ti=0;
021230d4
AV
1211
1212 /* Decrement for Other and TCP Timer vectors */
1213 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1214
1215 /* Map the Tx/Rx rings to the vectors we were allotted. */
1216 err = ixgbe_map_rings_to_vectors(adapter, q_vectors);
1217 if (err)
1218 goto out;
1219
1220#define SET_HANDLER(_v) ((!(_v)->rxr_count) ? &ixgbe_msix_clean_tx : \
b4617240
PW
1221 (!(_v)->txr_count) ? &ixgbe_msix_clean_rx : \
1222 &ixgbe_msix_clean_many)
021230d4
AV
1223 for (vector = 0; vector < q_vectors; vector++) {
1224 handler = SET_HANDLER(&adapter->q_vector[vector]);
cb13fc20
RO
1225
1226 if(handler == &ixgbe_msix_clean_rx) {
1227 sprintf(adapter->name[vector], "%s-%s-%d",
1228 netdev->name, "rx", ri++);
1229 }
1230 else if(handler == &ixgbe_msix_clean_tx) {
1231 sprintf(adapter->name[vector], "%s-%s-%d",
1232 netdev->name, "tx", ti++);
1233 }
1234 else
1235 sprintf(adapter->name[vector], "%s-%s-%d",
1236 netdev->name, "TxRx", vector);
1237
021230d4 1238 err = request_irq(adapter->msix_entries[vector].vector,
b4617240
PW
1239 handler, 0, adapter->name[vector],
1240 &(adapter->q_vector[vector]));
9a799d71
AK
1241 if (err) {
1242 DPRINTK(PROBE, ERR,
b4617240
PW
1243 "request_irq failed for MSIX interrupt "
1244 "Error: %d\n", err);
021230d4 1245 goto free_queue_irqs;
9a799d71 1246 }
9a799d71
AK
1247 }
1248
021230d4
AV
1249 sprintf(adapter->name[vector], "%s:lsc", netdev->name);
1250 err = request_irq(adapter->msix_entries[vector].vector,
b4617240 1251 &ixgbe_msix_lsc, 0, adapter->name[vector], netdev);
9a799d71
AK
1252 if (err) {
1253 DPRINTK(PROBE, ERR,
1254 "request_irq for msix_lsc failed: %d\n", err);
021230d4 1255 goto free_queue_irqs;
9a799d71
AK
1256 }
1257
9a799d71
AK
1258 return 0;
1259
021230d4
AV
1260free_queue_irqs:
1261 for (i = vector - 1; i >= 0; i--)
1262 free_irq(adapter->msix_entries[--vector].vector,
b4617240 1263 &(adapter->q_vector[i]));
021230d4
AV
1264 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
1265 pci_disable_msix(adapter->pdev);
9a799d71
AK
1266 kfree(adapter->msix_entries);
1267 adapter->msix_entries = NULL;
021230d4 1268out:
9a799d71
AK
1269 return err;
1270}
1271
f494e8fa
AV
1272static void ixgbe_set_itr(struct ixgbe_adapter *adapter)
1273{
1274 struct ixgbe_hw *hw = &adapter->hw;
1275 struct ixgbe_q_vector *q_vector = adapter->q_vector;
1276 u8 current_itr;
1277 u32 new_itr = q_vector->eitr;
1278 struct ixgbe_ring *rx_ring = &adapter->rx_ring[0];
1279 struct ixgbe_ring *tx_ring = &adapter->tx_ring[0];
1280
30efa5a3 1281 q_vector->tx_itr = ixgbe_update_itr(adapter, new_itr,
b4617240
PW
1282 q_vector->tx_itr,
1283 tx_ring->total_packets,
1284 tx_ring->total_bytes);
30efa5a3 1285 q_vector->rx_itr = ixgbe_update_itr(adapter, new_itr,
b4617240
PW
1286 q_vector->rx_itr,
1287 rx_ring->total_packets,
1288 rx_ring->total_bytes);
f494e8fa 1289
30efa5a3 1290 current_itr = max(q_vector->rx_itr, q_vector->tx_itr);
f494e8fa
AV
1291
1292 switch (current_itr) {
1293 /* counts and packets in update_itr are dependent on these numbers */
1294 case lowest_latency:
1295 new_itr = 100000;
1296 break;
1297 case low_latency:
1298 new_itr = 20000; /* aka hwitr = ~200 */
1299 break;
1300 case bulk_latency:
1301 new_itr = 8000;
1302 break;
1303 default:
1304 break;
1305 }
1306
1307 if (new_itr != q_vector->eitr) {
1308 u32 itr_reg;
1309 /* do an exponential smoothing */
1310 new_itr = ((q_vector->eitr * 90)/100) + ((new_itr * 10)/100);
1311 q_vector->eitr = new_itr;
1312 itr_reg = EITR_INTS_PER_SEC_TO_REG(new_itr);
1313 /* must write high and low 16 bits to reset counter */
1314 IXGBE_WRITE_REG(hw, IXGBE_EITR(0), itr_reg | (itr_reg)<<16);
1315 }
1316
1317 return;
1318}
1319
79aefa45
AD
1320/**
1321 * ixgbe_irq_disable - Mask off interrupt generation on the NIC
1322 * @adapter: board private structure
1323 **/
1324static inline void ixgbe_irq_disable(struct ixgbe_adapter *adapter)
1325{
1326 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~0);
1327 IXGBE_WRITE_FLUSH(&adapter->hw);
1328 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
1329 int i;
1330 for (i = 0; i < adapter->num_msix_vectors; i++)
1331 synchronize_irq(adapter->msix_entries[i].vector);
1332 } else {
1333 synchronize_irq(adapter->pdev->irq);
1334 }
1335}
1336
1337/**
1338 * ixgbe_irq_enable - Enable default interrupt generation settings
1339 * @adapter: board private structure
1340 **/
1341static inline void ixgbe_irq_enable(struct ixgbe_adapter *adapter)
1342{
1343 u32 mask;
1344 mask = IXGBE_EIMS_ENABLE_MASK;
6ab33d51
DM
1345 if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE)
1346 mask |= IXGBE_EIMS_GPI_SDP1;
79aefa45
AD
1347 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, mask);
1348 IXGBE_WRITE_FLUSH(&adapter->hw);
1349}
021230d4 1350
9a799d71 1351/**
021230d4 1352 * ixgbe_intr - legacy mode Interrupt Handler
9a799d71
AK
1353 * @irq: interrupt number
1354 * @data: pointer to a network interface device structure
9a799d71
AK
1355 **/
1356static irqreturn_t ixgbe_intr(int irq, void *data)
1357{
1358 struct net_device *netdev = data;
1359 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1360 struct ixgbe_hw *hw = &adapter->hw;
1361 u32 eicr;
1362
021230d4
AV
1363 /* for NAPI, using EIAM to auto-mask tx/rx interrupt bits on read
1364 * therefore no explict interrupt disable is necessary */
1365 eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
f47cf66e
JB
1366 if (!eicr) {
1367 /* shared interrupt alert!
1368 * make sure interrupts are enabled because the read will
1369 * have disabled interrupts due to EIAM */
1370 ixgbe_irq_enable(adapter);
9a799d71 1371 return IRQ_NONE; /* Not our interrupt */
f47cf66e 1372 }
9a799d71 1373
cf8280ee
JB
1374 if (eicr & IXGBE_EICR_LSC)
1375 ixgbe_check_lsc(adapter);
021230d4 1376
0befdb3e
JB
1377 ixgbe_check_fan_failure(adapter, eicr);
1378
021230d4 1379 if (netif_rx_schedule_prep(netdev, &adapter->q_vector[0].napi)) {
f494e8fa
AV
1380 adapter->tx_ring[0].total_packets = 0;
1381 adapter->tx_ring[0].total_bytes = 0;
1382 adapter->rx_ring[0].total_packets = 0;
1383 adapter->rx_ring[0].total_bytes = 0;
021230d4
AV
1384 /* would disable interrupts here but EIAM disabled it */
1385 __netif_rx_schedule(netdev, &adapter->q_vector[0].napi);
9a799d71
AK
1386 }
1387
1388 return IRQ_HANDLED;
1389}
1390
021230d4
AV
1391static inline void ixgbe_reset_q_vectors(struct ixgbe_adapter *adapter)
1392{
1393 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1394
1395 for (i = 0; i < q_vectors; i++) {
1396 struct ixgbe_q_vector *q_vector = &adapter->q_vector[i];
1397 bitmap_zero(q_vector->rxr_idx, MAX_RX_QUEUES);
1398 bitmap_zero(q_vector->txr_idx, MAX_TX_QUEUES);
1399 q_vector->rxr_count = 0;
1400 q_vector->txr_count = 0;
1401 }
1402}
1403
9a799d71
AK
1404/**
1405 * ixgbe_request_irq - initialize interrupts
1406 * @adapter: board private structure
1407 *
1408 * Attempts to configure interrupts using the best available
1409 * capabilities of the hardware and kernel.
1410 **/
021230d4 1411static int ixgbe_request_irq(struct ixgbe_adapter *adapter)
9a799d71
AK
1412{
1413 struct net_device *netdev = adapter->netdev;
021230d4 1414 int err;
9a799d71 1415
021230d4
AV
1416 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
1417 err = ixgbe_request_msix_irqs(adapter);
1418 } else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED) {
1419 err = request_irq(adapter->pdev->irq, &ixgbe_intr, 0,
b4617240 1420 netdev->name, netdev);
021230d4
AV
1421 } else {
1422 err = request_irq(adapter->pdev->irq, &ixgbe_intr, IRQF_SHARED,
b4617240 1423 netdev->name, netdev);
9a799d71
AK
1424 }
1425
9a799d71
AK
1426 if (err)
1427 DPRINTK(PROBE, ERR, "request_irq failed, Error %d\n", err);
1428
9a799d71
AK
1429 return err;
1430}
1431
1432static void ixgbe_free_irq(struct ixgbe_adapter *adapter)
1433{
1434 struct net_device *netdev = adapter->netdev;
1435
1436 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
021230d4 1437 int i, q_vectors;
9a799d71 1438
021230d4
AV
1439 q_vectors = adapter->num_msix_vectors;
1440
1441 i = q_vectors - 1;
9a799d71 1442 free_irq(adapter->msix_entries[i].vector, netdev);
9a799d71 1443
021230d4
AV
1444 i--;
1445 for (; i >= 0; i--) {
1446 free_irq(adapter->msix_entries[i].vector,
b4617240 1447 &(adapter->q_vector[i]));
021230d4
AV
1448 }
1449
1450 ixgbe_reset_q_vectors(adapter);
1451 } else {
1452 free_irq(adapter->pdev->irq, netdev);
9a799d71
AK
1453 }
1454}
1455
9a799d71
AK
1456/**
1457 * ixgbe_configure_msi_and_legacy - Initialize PIN (INTA...) and MSI interrupts
1458 *
1459 **/
1460static void ixgbe_configure_msi_and_legacy(struct ixgbe_adapter *adapter)
1461{
9a799d71
AK
1462 struct ixgbe_hw *hw = &adapter->hw;
1463
021230d4 1464 IXGBE_WRITE_REG(hw, IXGBE_EITR(0),
30efa5a3 1465 EITR_INTS_PER_SEC_TO_REG(adapter->eitr_param));
9a799d71
AK
1466
1467 ixgbe_set_ivar(adapter, IXGBE_IVAR_RX_QUEUE(0), 0);
021230d4
AV
1468 ixgbe_set_ivar(adapter, IXGBE_IVAR_TX_QUEUE(0), 0);
1469
1470 map_vector_to_rxq(adapter, 0, 0);
1471 map_vector_to_txq(adapter, 0, 0);
1472
1473 DPRINTK(HW, INFO, "Legacy interrupt IVAR setup done\n");
9a799d71
AK
1474}
1475
1476/**
3a581073 1477 * ixgbe_configure_tx - Configure 8259x Transmit Unit after Reset
9a799d71
AK
1478 * @adapter: board private structure
1479 *
1480 * Configure the Tx unit of the MAC after a reset.
1481 **/
1482static void ixgbe_configure_tx(struct ixgbe_adapter *adapter)
1483{
e01c31a5 1484 u64 tdba, tdwba;
9a799d71 1485 struct ixgbe_hw *hw = &adapter->hw;
021230d4 1486 u32 i, j, tdlen, txctrl;
9a799d71
AK
1487
1488 /* Setup the HW Tx Head and Tail descriptor pointers */
1489 for (i = 0; i < adapter->num_tx_queues; i++) {
e01c31a5
JB
1490 struct ixgbe_ring *ring = &adapter->tx_ring[i];
1491 j = ring->reg_idx;
1492 tdba = ring->dma;
1493 tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
021230d4 1494 IXGBE_WRITE_REG(hw, IXGBE_TDBAL(j),
e01c31a5 1495 (tdba & DMA_32BIT_MASK));
021230d4 1496 IXGBE_WRITE_REG(hw, IXGBE_TDBAH(j), (tdba >> 32));
e01c31a5
JB
1497 tdwba = ring->dma +
1498 (ring->count * sizeof(union ixgbe_adv_tx_desc));
1499 tdwba |= IXGBE_TDWBAL_HEAD_WB_ENABLE;
1500 IXGBE_WRITE_REG(hw, IXGBE_TDWBAL(j), tdwba & DMA_32BIT_MASK);
1501 IXGBE_WRITE_REG(hw, IXGBE_TDWBAH(j), (tdwba >> 32));
021230d4
AV
1502 IXGBE_WRITE_REG(hw, IXGBE_TDLEN(j), tdlen);
1503 IXGBE_WRITE_REG(hw, IXGBE_TDH(j), 0);
1504 IXGBE_WRITE_REG(hw, IXGBE_TDT(j), 0);
1505 adapter->tx_ring[i].head = IXGBE_TDH(j);
1506 adapter->tx_ring[i].tail = IXGBE_TDT(j);
1507 /* Disable Tx Head Writeback RO bit, since this hoses
1508 * bookkeeping if things aren't delivered in order.
1509 */
e01c31a5 1510 txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(j));
021230d4 1511 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
e01c31a5 1512 IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(j), txctrl);
9a799d71 1513 }
9a799d71
AK
1514}
1515
cc41ac7c
JB
1516#define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1517
1518static void ixgbe_configure_srrctl(struct ixgbe_adapter *adapter, int index)
1519{
1520 struct ixgbe_ring *rx_ring;
1521 u32 srrctl;
1522 int queue0;
3be1adfb
AD
1523 unsigned long mask;
1524
1525 /* program one srrctl register per VMDq index */
1526 if (adapter->flags & IXGBE_FLAG_VMDQ_ENABLED) {
1527 long shift, len;
1528 mask = (unsigned long) adapter->ring_feature[RING_F_RSS].mask;
1529 len = sizeof(adapter->ring_feature[RING_F_VMDQ].mask) * 8;
1530 shift = find_first_bit(&mask, len);
1531 queue0 = index & mask;
1532 index = (index & mask) >> shift;
1533 /* program one srrctl per RSS queue since RDRXCTL.MVMEN is enabled */
cc41ac7c 1534 } else {
3be1adfb
AD
1535 mask = (unsigned long) adapter->ring_feature[RING_F_RSS].mask;
1536 queue0 = index & mask;
1537 index = index & mask;
cc41ac7c 1538 }
3be1adfb 1539
cc41ac7c
JB
1540 rx_ring = &adapter->rx_ring[queue0];
1541
1542 srrctl = IXGBE_READ_REG(&adapter->hw, IXGBE_SRRCTL(index));
1543
1544 srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
1545 srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
1546
1547 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
1548 srrctl |= IXGBE_RXBUFFER_2048 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1549 srrctl |= IXGBE_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
1550 srrctl |= ((IXGBE_RX_HDR_SIZE <<
b4617240
PW
1551 IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT) &
1552 IXGBE_SRRCTL_BSIZEHDR_MASK);
cc41ac7c
JB
1553 } else {
1554 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1555
1556 if (rx_ring->rx_buf_len == MAXIMUM_ETHERNET_VLAN_SIZE)
1557 srrctl |= IXGBE_RXBUFFER_2048 >>
1558 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1559 else
1560 srrctl |= rx_ring->rx_buf_len >>
1561 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1562 }
1563 IXGBE_WRITE_REG(&adapter->hw, IXGBE_SRRCTL(index), srrctl);
1564}
9a799d71 1565
177db6ff
MC
1566/**
1567 * ixgbe_get_skb_hdr - helper function for LRO header processing
1568 * @skb: pointer to sk_buff to be added to LRO packet
b4617240 1569 * @iphdr: pointer to ip header structure
177db6ff
MC
1570 * @tcph: pointer to tcp header structure
1571 * @hdr_flags: pointer to header flags
1572 * @priv: private data
1573 **/
1574static int ixgbe_get_skb_hdr(struct sk_buff *skb, void **iphdr, void **tcph,
1575 u64 *hdr_flags, void *priv)
1576{
1577 union ixgbe_adv_rx_desc *rx_desc = priv;
1578
1579 /* Verify that this is a valid IPv4 TCP packet */
e9990a9c
JB
1580 if (!((ixgbe_get_pkt_info(rx_desc) & IXGBE_RXDADV_PKTTYPE_IPV4) &&
1581 (ixgbe_get_pkt_info(rx_desc) & IXGBE_RXDADV_PKTTYPE_TCP)))
177db6ff
MC
1582 return -1;
1583
1584 /* Set network headers */
1585 skb_reset_network_header(skb);
1586 skb_set_transport_header(skb, ip_hdrlen(skb));
1587 *iphdr = ip_hdr(skb);
1588 *tcph = tcp_hdr(skb);
1589 *hdr_flags = LRO_IPV4 | LRO_TCP;
1590 return 0;
1591}
1592
cc41ac7c 1593#define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
b4617240 1594 (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
cc41ac7c 1595
9a799d71 1596/**
3a581073 1597 * ixgbe_configure_rx - Configure 8259x Receive Unit after Reset
9a799d71
AK
1598 * @adapter: board private structure
1599 *
1600 * Configure the Rx unit of the MAC after a reset.
1601 **/
1602static void ixgbe_configure_rx(struct ixgbe_adapter *adapter)
1603{
1604 u64 rdba;
1605 struct ixgbe_hw *hw = &adapter->hw;
1606 struct net_device *netdev = adapter->netdev;
1607 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
021230d4 1608 int i, j;
9a799d71 1609 u32 rdlen, rxctrl, rxcsum;
7c6e0a43
JB
1610 static const u32 seed[10] = { 0xE291D73D, 0x1805EC6C, 0x2A94B30D,
1611 0xA54F2BEC, 0xEA49AF7C, 0xE214AD3D, 0xB855AABE,
1612 0x6A3E67EA, 0x14364D17, 0x3BED200D};
9a799d71 1613 u32 fctrl, hlreg0;
9a799d71 1614 u32 pages;
cc41ac7c
JB
1615 u32 reta = 0, mrqc;
1616 u32 rdrxctl;
7c6e0a43 1617 int rx_buf_len;
9a799d71
AK
1618
1619 /* Decide whether to use packet split mode or not */
762f4c57 1620 adapter->flags |= IXGBE_FLAG_RX_PS_ENABLED;
9a799d71
AK
1621
1622 /* Set the RX buffer length according to the mode */
1623 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
7c6e0a43 1624 rx_buf_len = IXGBE_RX_HDR_SIZE;
9a799d71
AK
1625 } else {
1626 if (netdev->mtu <= ETH_DATA_LEN)
7c6e0a43 1627 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
9a799d71 1628 else
7c6e0a43 1629 rx_buf_len = ALIGN(max_frame, 1024);
9a799d71
AK
1630 }
1631
1632 fctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
1633 fctrl |= IXGBE_FCTRL_BAM;
021230d4 1634 fctrl |= IXGBE_FCTRL_DPF; /* discard pause frames when FC enabled */
9a799d71
AK
1635 IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, fctrl);
1636
1637 hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
1638 if (adapter->netdev->mtu <= ETH_DATA_LEN)
1639 hlreg0 &= ~IXGBE_HLREG0_JUMBOEN;
1640 else
1641 hlreg0 |= IXGBE_HLREG0_JUMBOEN;
1642 IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
1643
1644 pages = PAGE_USE_COUNT(adapter->netdev->mtu);
1645
9a799d71
AK
1646 rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1647 /* disable receives while setting up the descriptors */
1648 rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
1649 IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl & ~IXGBE_RXCTRL_RXEN);
1650
1651 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1652 * the Base and Length of the Rx Descriptor Ring */
1653 for (i = 0; i < adapter->num_rx_queues; i++) {
1654 rdba = adapter->rx_ring[i].dma;
7c6e0a43
JB
1655 j = adapter->rx_ring[i].reg_idx;
1656 IXGBE_WRITE_REG(hw, IXGBE_RDBAL(j), (rdba & DMA_32BIT_MASK));
1657 IXGBE_WRITE_REG(hw, IXGBE_RDBAH(j), (rdba >> 32));
1658 IXGBE_WRITE_REG(hw, IXGBE_RDLEN(j), rdlen);
1659 IXGBE_WRITE_REG(hw, IXGBE_RDH(j), 0);
1660 IXGBE_WRITE_REG(hw, IXGBE_RDT(j), 0);
1661 adapter->rx_ring[i].head = IXGBE_RDH(j);
1662 adapter->rx_ring[i].tail = IXGBE_RDT(j);
1663 adapter->rx_ring[i].rx_buf_len = rx_buf_len;
e9990a9c
JB
1664 /* Intitial LRO Settings */
1665 adapter->rx_ring[i].lro_mgr.max_aggr = IXGBE_MAX_LRO_AGGREGATE;
1666 adapter->rx_ring[i].lro_mgr.max_desc = IXGBE_MAX_LRO_DESCRIPTORS;
1667 adapter->rx_ring[i].lro_mgr.get_skb_header = ixgbe_get_skb_hdr;
1668 adapter->rx_ring[i].lro_mgr.features = LRO_F_EXTRACT_VLAN_ID;
1669 if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL))
1670 adapter->rx_ring[i].lro_mgr.features |= LRO_F_NAPI;
1671 adapter->rx_ring[i].lro_mgr.dev = adapter->netdev;
1672 adapter->rx_ring[i].lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1673 adapter->rx_ring[i].lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
cc41ac7c
JB
1674
1675 ixgbe_configure_srrctl(adapter, j);
9a799d71
AK
1676 }
1677
cc41ac7c
JB
1678 /*
1679 * For VMDq support of different descriptor types or
1680 * buffer sizes through the use of multiple SRRCTL
1681 * registers, RDRXCTL.MVMEN must be set to 1
1682 *
1683 * also, the manual doesn't mention it clearly but DCA hints
1684 * will only use queue 0's tags unless this bit is set. Side
1685 * effects of setting this bit are only that SRRCTL must be
1686 * fully programmed [0..15]
1687 */
2f90b865
AD
1688 if (adapter->flags &
1689 (IXGBE_FLAG_RSS_ENABLED | IXGBE_FLAG_VMDQ_ENABLED)) {
1690 rdrxctl = IXGBE_READ_REG(hw, IXGBE_RDRXCTL);
1691 rdrxctl |= IXGBE_RDRXCTL_MVMEN;
1692 IXGBE_WRITE_REG(hw, IXGBE_RDRXCTL, rdrxctl);
1693 }
177db6ff 1694
021230d4 1695 if (adapter->flags & IXGBE_FLAG_RSS_ENABLED) {
9a799d71 1696 /* Fill out redirection table */
021230d4
AV
1697 for (i = 0, j = 0; i < 128; i++, j++) {
1698 if (j == adapter->ring_feature[RING_F_RSS].indices)
1699 j = 0;
1700 /* reta = 4-byte sliding window of
1701 * 0x00..(indices-1)(indices-1)00..etc. */
1702 reta = (reta << 8) | (j * 0x11);
1703 if ((i & 3) == 3)
1704 IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
9a799d71
AK
1705 }
1706
1707 /* Fill out hash function seeds */
1708 for (i = 0; i < 10; i++)
7c6e0a43 1709 IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), seed[i]);
9a799d71
AK
1710
1711 mrqc = IXGBE_MRQC_RSSEN
1712 /* Perform hash on these packet types */
7c6e0a43
JB
1713 | IXGBE_MRQC_RSS_FIELD_IPV4
1714 | IXGBE_MRQC_RSS_FIELD_IPV4_TCP
1715 | IXGBE_MRQC_RSS_FIELD_IPV4_UDP
1716 | IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP
1717 | IXGBE_MRQC_RSS_FIELD_IPV6_EX
1718 | IXGBE_MRQC_RSS_FIELD_IPV6
1719 | IXGBE_MRQC_RSS_FIELD_IPV6_TCP
1720 | IXGBE_MRQC_RSS_FIELD_IPV6_UDP
1721 | IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP;
9a799d71 1722 IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
021230d4 1723 }
9a799d71 1724
021230d4
AV
1725 rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
1726
1727 if (adapter->flags & IXGBE_FLAG_RSS_ENABLED ||
1728 adapter->flags & IXGBE_FLAG_RX_CSUM_ENABLED) {
1729 /* Disable indicating checksum in descriptor, enables
1730 * RSS hash */
9a799d71 1731 rxcsum |= IXGBE_RXCSUM_PCSD;
9a799d71 1732 }
021230d4
AV
1733 if (!(rxcsum & IXGBE_RXCSUM_PCSD)) {
1734 /* Enable IPv4 payload checksum for UDP fragments
1735 * if PCSD is not set */
1736 rxcsum |= IXGBE_RXCSUM_IPPCSE;
1737 }
1738
1739 IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
9a799d71
AK
1740}
1741
1742static void ixgbe_vlan_rx_register(struct net_device *netdev,
b4617240 1743 struct vlan_group *grp)
9a799d71
AK
1744{
1745 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1746 u32 ctrl;
1747
d4f80882
AV
1748 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1749 ixgbe_irq_disable(adapter);
9a799d71
AK
1750 adapter->vlgrp = grp;
1751
2f90b865
AD
1752 /*
1753 * For a DCB driver, always enable VLAN tag stripping so we can
1754 * still receive traffic from a DCB-enabled host even if we're
1755 * not in DCB mode.
1756 */
1757 ctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_VLNCTRL);
1758 ctrl |= IXGBE_VLNCTRL_VME;
1759 ctrl &= ~IXGBE_VLNCTRL_CFIEN;
1760 IXGBE_WRITE_REG(&adapter->hw, IXGBE_VLNCTRL, ctrl);
1761
9a799d71
AK
1762 if (grp) {
1763 /* enable VLAN tag insert/strip */
1764 ctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_VLNCTRL);
746b9f02 1765 ctrl |= IXGBE_VLNCTRL_VME;
9a799d71
AK
1766 ctrl &= ~IXGBE_VLNCTRL_CFIEN;
1767 IXGBE_WRITE_REG(&adapter->hw, IXGBE_VLNCTRL, ctrl);
1768 }
1769
d4f80882
AV
1770 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1771 ixgbe_irq_enable(adapter);
9a799d71
AK
1772}
1773
1774static void ixgbe_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1775{
1776 struct ixgbe_adapter *adapter = netdev_priv(netdev);
c44ade9e 1777 struct ixgbe_hw *hw = &adapter->hw;
9a799d71
AK
1778
1779 /* add VID to filter table */
c44ade9e 1780 hw->mac.ops.set_vfta(&adapter->hw, vid, 0, true);
9a799d71
AK
1781}
1782
1783static void ixgbe_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1784{
1785 struct ixgbe_adapter *adapter = netdev_priv(netdev);
c44ade9e 1786 struct ixgbe_hw *hw = &adapter->hw;
9a799d71 1787
d4f80882
AV
1788 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1789 ixgbe_irq_disable(adapter);
1790
9a799d71 1791 vlan_group_set_device(adapter->vlgrp, vid, NULL);
d4f80882
AV
1792
1793 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1794 ixgbe_irq_enable(adapter);
9a799d71
AK
1795
1796 /* remove VID from filter table */
c44ade9e 1797 hw->mac.ops.set_vfta(&adapter->hw, vid, 0, false);
9a799d71
AK
1798}
1799
1800static void ixgbe_restore_vlan(struct ixgbe_adapter *adapter)
1801{
1802 ixgbe_vlan_rx_register(adapter->netdev, adapter->vlgrp);
1803
1804 if (adapter->vlgrp) {
1805 u16 vid;
1806 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
1807 if (!vlan_group_get_device(adapter->vlgrp, vid))
1808 continue;
1809 ixgbe_vlan_rx_add_vid(adapter->netdev, vid);
1810 }
1811 }
1812}
1813
2c5645cf
CL
1814static u8 *ixgbe_addr_list_itr(struct ixgbe_hw *hw, u8 **mc_addr_ptr, u32 *vmdq)
1815{
1816 struct dev_mc_list *mc_ptr;
1817 u8 *addr = *mc_addr_ptr;
1818 *vmdq = 0;
1819
1820 mc_ptr = container_of(addr, struct dev_mc_list, dmi_addr[0]);
1821 if (mc_ptr->next)
1822 *mc_addr_ptr = mc_ptr->next->dmi_addr;
1823 else
1824 *mc_addr_ptr = NULL;
1825
1826 return addr;
1827}
1828
9a799d71 1829/**
2c5645cf 1830 * ixgbe_set_rx_mode - Unicast, Multicast and Promiscuous mode set
9a799d71
AK
1831 * @netdev: network interface device structure
1832 *
2c5645cf
CL
1833 * The set_rx_method entry point is called whenever the unicast/multicast
1834 * address list or the network interface flags are updated. This routine is
1835 * responsible for configuring the hardware for proper unicast, multicast and
1836 * promiscuous mode.
9a799d71 1837 **/
2c5645cf 1838static void ixgbe_set_rx_mode(struct net_device *netdev)
9a799d71
AK
1839{
1840 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1841 struct ixgbe_hw *hw = &adapter->hw;
3d01625a 1842 u32 fctrl, vlnctrl;
2c5645cf
CL
1843 u8 *addr_list = NULL;
1844 int addr_count = 0;
9a799d71
AK
1845
1846 /* Check for Promiscuous and All Multicast modes */
1847
1848 fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
3d01625a 1849 vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
9a799d71
AK
1850
1851 if (netdev->flags & IFF_PROMISC) {
2c5645cf 1852 hw->addr_ctrl.user_set_promisc = 1;
9a799d71 1853 fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
3d01625a 1854 vlnctrl &= ~IXGBE_VLNCTRL_VFE;
9a799d71 1855 } else {
746b9f02
PM
1856 if (netdev->flags & IFF_ALLMULTI) {
1857 fctrl |= IXGBE_FCTRL_MPE;
1858 fctrl &= ~IXGBE_FCTRL_UPE;
1859 } else {
1860 fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
1861 }
3d01625a 1862 vlnctrl |= IXGBE_VLNCTRL_VFE;
2c5645cf 1863 hw->addr_ctrl.user_set_promisc = 0;
9a799d71
AK
1864 }
1865
1866 IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
3d01625a 1867 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
9a799d71 1868
2c5645cf
CL
1869 /* reprogram secondary unicast list */
1870 addr_count = netdev->uc_count;
1871 if (addr_count)
1872 addr_list = netdev->uc_list->dmi_addr;
c44ade9e
JB
1873 hw->mac.ops.update_uc_addr_list(hw, addr_list, addr_count,
1874 ixgbe_addr_list_itr);
9a799d71 1875
2c5645cf
CL
1876 /* reprogram multicast list */
1877 addr_count = netdev->mc_count;
1878 if (addr_count)
1879 addr_list = netdev->mc_list->dmi_addr;
c44ade9e
JB
1880 hw->mac.ops.update_mc_addr_list(hw, addr_list, addr_count,
1881 ixgbe_addr_list_itr);
9a799d71
AK
1882}
1883
021230d4
AV
1884static void ixgbe_napi_enable_all(struct ixgbe_adapter *adapter)
1885{
1886 int q_idx;
1887 struct ixgbe_q_vector *q_vector;
1888 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1889
1890 /* legacy and MSI only use one vector */
1891 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
1892 q_vectors = 1;
1893
1894 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
f0848276 1895 struct napi_struct *napi;
021230d4
AV
1896 q_vector = &adapter->q_vector[q_idx];
1897 if (!q_vector->rxr_count)
1898 continue;
f0848276
JB
1899 napi = &q_vector->napi;
1900 if ((adapter->flags & IXGBE_FLAG_MSIX_ENABLED) &&
1901 (q_vector->rxr_count > 1))
1902 napi->poll = &ixgbe_clean_rxonly_many;
1903
1904 napi_enable(napi);
021230d4
AV
1905 }
1906}
1907
1908static void ixgbe_napi_disable_all(struct ixgbe_adapter *adapter)
1909{
1910 int q_idx;
1911 struct ixgbe_q_vector *q_vector;
1912 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1913
1914 /* legacy and MSI only use one vector */
1915 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
1916 q_vectors = 1;
1917
1918 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1919 q_vector = &adapter->q_vector[q_idx];
1920 if (!q_vector->rxr_count)
1921 continue;
1922 napi_disable(&q_vector->napi);
1923 }
1924}
1925
7a6b6f51 1926#ifdef CONFIG_IXGBE_DCB
2f90b865
AD
1927/*
1928 * ixgbe_configure_dcb - Configure DCB hardware
1929 * @adapter: ixgbe adapter struct
1930 *
1931 * This is called by the driver on open to configure the DCB hardware.
1932 * This is also called by the gennetlink interface when reconfiguring
1933 * the DCB state.
1934 */
1935static void ixgbe_configure_dcb(struct ixgbe_adapter *adapter)
1936{
1937 struct ixgbe_hw *hw = &adapter->hw;
1938 u32 txdctl, vlnctrl;
1939 int i, j;
1940
1941 ixgbe_dcb_check_config(&adapter->dcb_cfg);
1942 ixgbe_dcb_calculate_tc_credits(&adapter->dcb_cfg, DCB_TX_CONFIG);
1943 ixgbe_dcb_calculate_tc_credits(&adapter->dcb_cfg, DCB_RX_CONFIG);
1944
1945 /* reconfigure the hardware */
1946 ixgbe_dcb_hw_config(&adapter->hw, &adapter->dcb_cfg);
1947
1948 for (i = 0; i < adapter->num_tx_queues; i++) {
1949 j = adapter->tx_ring[i].reg_idx;
1950 txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(j));
1951 /* PThresh workaround for Tx hang with DFP enabled. */
1952 txdctl |= 32;
1953 IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(j), txdctl);
1954 }
1955 /* Enable VLAN tag insert/strip */
1956 vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1957 vlnctrl |= IXGBE_VLNCTRL_VME | IXGBE_VLNCTRL_VFE;
1958 vlnctrl &= ~IXGBE_VLNCTRL_CFIEN;
1959 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
1960 hw->mac.ops.set_vfta(&adapter->hw, 0, 0, true);
1961}
1962
1963#endif
9a799d71
AK
1964static void ixgbe_configure(struct ixgbe_adapter *adapter)
1965{
1966 struct net_device *netdev = adapter->netdev;
1967 int i;
1968
2c5645cf 1969 ixgbe_set_rx_mode(netdev);
9a799d71
AK
1970
1971 ixgbe_restore_vlan(adapter);
7a6b6f51 1972#ifdef CONFIG_IXGBE_DCB
2f90b865
AD
1973 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
1974 netif_set_gso_max_size(netdev, 32768);
1975 ixgbe_configure_dcb(adapter);
1976 } else {
1977 netif_set_gso_max_size(netdev, 65536);
1978 }
1979#else
1980 netif_set_gso_max_size(netdev, 65536);
1981#endif
9a799d71
AK
1982
1983 ixgbe_configure_tx(adapter);
1984 ixgbe_configure_rx(adapter);
1985 for (i = 0; i < adapter->num_rx_queues; i++)
1986 ixgbe_alloc_rx_buffers(adapter, &adapter->rx_ring[i],
b4617240 1987 (adapter->rx_ring[i].count - 1));
9a799d71
AK
1988}
1989
1990static int ixgbe_up_complete(struct ixgbe_adapter *adapter)
1991{
1992 struct net_device *netdev = adapter->netdev;
9a799d71 1993 struct ixgbe_hw *hw = &adapter->hw;
021230d4 1994 int i, j = 0;
9a799d71 1995 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
021230d4
AV
1996 u32 txdctl, rxdctl, mhadd;
1997 u32 gpie;
9a799d71 1998
5eba3699
AV
1999 ixgbe_get_hw_control(adapter);
2000
021230d4
AV
2001 if ((adapter->flags & IXGBE_FLAG_MSIX_ENABLED) ||
2002 (adapter->flags & IXGBE_FLAG_MSI_ENABLED)) {
9a799d71
AK
2003 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
2004 gpie = (IXGBE_GPIE_MSIX_MODE | IXGBE_GPIE_EIAME |
b4617240 2005 IXGBE_GPIE_PBA_SUPPORT | IXGBE_GPIE_OCD);
9a799d71
AK
2006 } else {
2007 /* MSI only */
021230d4 2008 gpie = 0;
9a799d71 2009 }
021230d4
AV
2010 /* XXX: to interrupt immediately for EICS writes, enable this */
2011 /* gpie |= IXGBE_GPIE_EIMEN; */
2012 IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
9a799d71
AK
2013 }
2014
021230d4
AV
2015 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED)) {
2016 /* legacy interrupts, use EIAM to auto-mask when reading EICR,
2017 * specifically only auto mask tx and rx interrupts */
2018 IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
2019 }
9a799d71 2020
0befdb3e
JB
2021 /* Enable fan failure interrupt if media type is copper */
2022 if (adapter->flags & IXGBE_FLAG_FAN_FAIL_CAPABLE) {
2023 gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
2024 gpie |= IXGBE_SDP1_GPIEN;
2025 IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
2026 }
2027
021230d4 2028 mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
9a799d71
AK
2029 if (max_frame != (mhadd >> IXGBE_MHADD_MFS_SHIFT)) {
2030 mhadd &= ~IXGBE_MHADD_MFS_MASK;
2031 mhadd |= max_frame << IXGBE_MHADD_MFS_SHIFT;
2032
2033 IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
2034 }
2035
2036 for (i = 0; i < adapter->num_tx_queues; i++) {
021230d4
AV
2037 j = adapter->tx_ring[i].reg_idx;
2038 txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(j));
e01c31a5
JB
2039 /* enable WTHRESH=8 descriptors, to encourage burst writeback */
2040 txdctl |= (8 << 16);
9a799d71 2041 txdctl |= IXGBE_TXDCTL_ENABLE;
021230d4 2042 IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(j), txdctl);
9a799d71
AK
2043 }
2044
2045 for (i = 0; i < adapter->num_rx_queues; i++) {
021230d4
AV
2046 j = adapter->rx_ring[i].reg_idx;
2047 rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(j));
2048 /* enable PTHRESH=32 descriptors (half the internal cache)
2049 * and HTHRESH=0 descriptors (to minimize latency on fetch),
2050 * this also removes a pesky rx_no_buffer_count increment */
2051 rxdctl |= 0x0020;
9a799d71 2052 rxdctl |= IXGBE_RXDCTL_ENABLE;
021230d4 2053 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(j), rxdctl);
9a799d71
AK
2054 }
2055 /* enable all receives */
2056 rxdctl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
2057 rxdctl |= (IXGBE_RXCTRL_DMBYPS | IXGBE_RXCTRL_RXEN);
2058 IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxdctl);
2059
2060 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
2061 ixgbe_configure_msix(adapter);
2062 else
2063 ixgbe_configure_msi_and_legacy(adapter);
2064
2065 clear_bit(__IXGBE_DOWN, &adapter->state);
021230d4
AV
2066 ixgbe_napi_enable_all(adapter);
2067
2068 /* clear any pending interrupts, may auto mask */
2069 IXGBE_READ_REG(hw, IXGBE_EICR);
2070
9a799d71
AK
2071 ixgbe_irq_enable(adapter);
2072
2073 /* bring the link up in the watchdog, this could race with our first
2074 * link up interrupt but shouldn't be a problem */
cf8280ee
JB
2075 adapter->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
2076 adapter->link_check_timeout = jiffies;
9a799d71
AK
2077 mod_timer(&adapter->watchdog_timer, jiffies);
2078 return 0;
2079}
2080
d4f80882
AV
2081void ixgbe_reinit_locked(struct ixgbe_adapter *adapter)
2082{
2083 WARN_ON(in_interrupt());
2084 while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
2085 msleep(1);
2086 ixgbe_down(adapter);
2087 ixgbe_up(adapter);
2088 clear_bit(__IXGBE_RESETTING, &adapter->state);
2089}
2090
9a799d71
AK
2091int ixgbe_up(struct ixgbe_adapter *adapter)
2092{
2093 /* hardware has been reset, we need to reload some things */
2094 ixgbe_configure(adapter);
2095
2096 return ixgbe_up_complete(adapter);
2097}
2098
2099void ixgbe_reset(struct ixgbe_adapter *adapter)
2100{
c44ade9e
JB
2101 struct ixgbe_hw *hw = &adapter->hw;
2102 if (hw->mac.ops.init_hw(hw))
2103 dev_err(&adapter->pdev->dev, "Hardware Error\n");
9a799d71
AK
2104
2105 /* reprogram the RAR[0] in case user changed it. */
c44ade9e 2106 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
9a799d71
AK
2107
2108}
2109
9a799d71
AK
2110/**
2111 * ixgbe_clean_rx_ring - Free Rx Buffers per Queue
2112 * @adapter: board private structure
2113 * @rx_ring: ring to free buffers from
2114 **/
2115static void ixgbe_clean_rx_ring(struct ixgbe_adapter *adapter,
b4617240 2116 struct ixgbe_ring *rx_ring)
9a799d71
AK
2117{
2118 struct pci_dev *pdev = adapter->pdev;
2119 unsigned long size;
2120 unsigned int i;
2121
2122 /* Free all the Rx ring sk_buffs */
2123
2124 for (i = 0; i < rx_ring->count; i++) {
2125 struct ixgbe_rx_buffer *rx_buffer_info;
2126
2127 rx_buffer_info = &rx_ring->rx_buffer_info[i];
2128 if (rx_buffer_info->dma) {
2129 pci_unmap_single(pdev, rx_buffer_info->dma,
b4617240
PW
2130 rx_ring->rx_buf_len,
2131 PCI_DMA_FROMDEVICE);
9a799d71
AK
2132 rx_buffer_info->dma = 0;
2133 }
2134 if (rx_buffer_info->skb) {
2135 dev_kfree_skb(rx_buffer_info->skb);
2136 rx_buffer_info->skb = NULL;
2137 }
2138 if (!rx_buffer_info->page)
2139 continue;
762f4c57
JB
2140 pci_unmap_page(pdev, rx_buffer_info->page_dma, PAGE_SIZE / 2,
2141 PCI_DMA_FROMDEVICE);
9a799d71 2142 rx_buffer_info->page_dma = 0;
9a799d71
AK
2143 put_page(rx_buffer_info->page);
2144 rx_buffer_info->page = NULL;
762f4c57 2145 rx_buffer_info->page_offset = 0;
9a799d71
AK
2146 }
2147
2148 size = sizeof(struct ixgbe_rx_buffer) * rx_ring->count;
2149 memset(rx_ring->rx_buffer_info, 0, size);
2150
2151 /* Zero out the descriptor ring */
2152 memset(rx_ring->desc, 0, rx_ring->size);
2153
2154 rx_ring->next_to_clean = 0;
2155 rx_ring->next_to_use = 0;
2156
2157 writel(0, adapter->hw.hw_addr + rx_ring->head);
2158 writel(0, adapter->hw.hw_addr + rx_ring->tail);
2159}
2160
2161/**
2162 * ixgbe_clean_tx_ring - Free Tx Buffers
2163 * @adapter: board private structure
2164 * @tx_ring: ring to be cleaned
2165 **/
2166static void ixgbe_clean_tx_ring(struct ixgbe_adapter *adapter,
b4617240 2167 struct ixgbe_ring *tx_ring)
9a799d71
AK
2168{
2169 struct ixgbe_tx_buffer *tx_buffer_info;
2170 unsigned long size;
2171 unsigned int i;
2172
2173 /* Free all the Tx ring sk_buffs */
2174
2175 for (i = 0; i < tx_ring->count; i++) {
2176 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2177 ixgbe_unmap_and_free_tx_resource(adapter, tx_buffer_info);
2178 }
2179
2180 size = sizeof(struct ixgbe_tx_buffer) * tx_ring->count;
2181 memset(tx_ring->tx_buffer_info, 0, size);
2182
2183 /* Zero out the descriptor ring */
2184 memset(tx_ring->desc, 0, tx_ring->size);
2185
2186 tx_ring->next_to_use = 0;
2187 tx_ring->next_to_clean = 0;
2188
2189 writel(0, adapter->hw.hw_addr + tx_ring->head);
2190 writel(0, adapter->hw.hw_addr + tx_ring->tail);
2191}
2192
2193/**
021230d4 2194 * ixgbe_clean_all_rx_rings - Free Rx Buffers for all queues
9a799d71
AK
2195 * @adapter: board private structure
2196 **/
021230d4 2197static void ixgbe_clean_all_rx_rings(struct ixgbe_adapter *adapter)
9a799d71
AK
2198{
2199 int i;
2200
021230d4
AV
2201 for (i = 0; i < adapter->num_rx_queues; i++)
2202 ixgbe_clean_rx_ring(adapter, &adapter->rx_ring[i]);
9a799d71
AK
2203}
2204
2205/**
021230d4 2206 * ixgbe_clean_all_tx_rings - Free Tx Buffers for all queues
9a799d71
AK
2207 * @adapter: board private structure
2208 **/
021230d4 2209static void ixgbe_clean_all_tx_rings(struct ixgbe_adapter *adapter)
9a799d71
AK
2210{
2211 int i;
2212
021230d4
AV
2213 for (i = 0; i < adapter->num_tx_queues; i++)
2214 ixgbe_clean_tx_ring(adapter, &adapter->tx_ring[i]);
9a799d71
AK
2215}
2216
2217void ixgbe_down(struct ixgbe_adapter *adapter)
2218{
2219 struct net_device *netdev = adapter->netdev;
7f821875 2220 struct ixgbe_hw *hw = &adapter->hw;
9a799d71 2221 u32 rxctrl;
7f821875
JB
2222 u32 txdctl;
2223 int i, j;
9a799d71
AK
2224
2225 /* signal that we are down to the interrupt handler */
2226 set_bit(__IXGBE_DOWN, &adapter->state);
2227
2228 /* disable receives */
7f821875
JB
2229 rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
2230 IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl & ~IXGBE_RXCTRL_RXEN);
9a799d71
AK
2231
2232 netif_tx_disable(netdev);
2233
7f821875 2234 IXGBE_WRITE_FLUSH(hw);
9a799d71
AK
2235 msleep(10);
2236
7f821875
JB
2237 netif_tx_stop_all_queues(netdev);
2238
9a799d71
AK
2239 ixgbe_irq_disable(adapter);
2240
021230d4 2241 ixgbe_napi_disable_all(adapter);
7f821875 2242
9a799d71 2243 del_timer_sync(&adapter->watchdog_timer);
cf8280ee 2244 cancel_work_sync(&adapter->watchdog_task);
9a799d71 2245
7f821875
JB
2246 /* disable transmits in the hardware now that interrupts are off */
2247 for (i = 0; i < adapter->num_tx_queues; i++) {
2248 j = adapter->tx_ring[i].reg_idx;
2249 txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(j));
2250 IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(j),
2251 (txdctl & ~IXGBE_TXDCTL_ENABLE));
2252 }
2253
9a799d71 2254 netif_carrier_off(netdev);
9a799d71 2255
5dd2d332 2256#ifdef CONFIG_IXGBE_DCA
96b0e0f6
JB
2257 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
2258 adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
2259 dca_remove_requester(&adapter->pdev->dev);
2260 }
2261
2262#endif
6f4a0e45
PL
2263 if (!pci_channel_offline(adapter->pdev))
2264 ixgbe_reset(adapter);
9a799d71
AK
2265 ixgbe_clean_all_tx_rings(adapter);
2266 ixgbe_clean_all_rx_rings(adapter);
2267
5dd2d332 2268#ifdef CONFIG_IXGBE_DCA
96b0e0f6
JB
2269 /* since we reset the hardware DCA settings were cleared */
2270 if (dca_add_requester(&adapter->pdev->dev) == 0) {
2271 adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
2272 /* always use CB2 mode, difference is masked
2273 * in the CB driver */
b4617240 2274 IXGBE_WRITE_REG(hw, IXGBE_DCA_CTRL, 2);
96b0e0f6
JB
2275 ixgbe_setup_dca(adapter);
2276 }
2277#endif
9a799d71
AK
2278}
2279
9a799d71 2280/**
021230d4
AV
2281 * ixgbe_poll - NAPI Rx polling callback
2282 * @napi: structure for representing this polling device
2283 * @budget: how many packets driver is allowed to clean
2284 *
2285 * This function is used for legacy and MSI, NAPI mode
9a799d71 2286 **/
021230d4 2287static int ixgbe_poll(struct napi_struct *napi, int budget)
9a799d71 2288{
021230d4 2289 struct ixgbe_q_vector *q_vector = container_of(napi,
b4617240 2290 struct ixgbe_q_vector, napi);
021230d4 2291 struct ixgbe_adapter *adapter = q_vector->adapter;
74ce8dd2 2292 int tx_cleaned, work_done = 0;
9a799d71 2293
5dd2d332 2294#ifdef CONFIG_IXGBE_DCA
bd0362dd
JC
2295 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
2296 ixgbe_update_tx_dca(adapter, adapter->tx_ring);
2297 ixgbe_update_rx_dca(adapter, adapter->rx_ring);
2298 }
2299#endif
2300
d2c7ddd6 2301 tx_cleaned = ixgbe_clean_tx_irq(adapter, adapter->tx_ring);
021230d4 2302 ixgbe_clean_rx_irq(adapter, adapter->rx_ring, &work_done, budget);
9a799d71 2303
d2c7ddd6
DM
2304 if (tx_cleaned)
2305 work_done = budget;
2306
53e52c72
DM
2307 /* If budget not fully consumed, exit the polling mode */
2308 if (work_done < budget) {
021230d4 2309 netif_rx_complete(adapter->netdev, napi);
30efa5a3 2310 if (adapter->itr_setting & 3)
f494e8fa 2311 ixgbe_set_itr(adapter);
d4f80882
AV
2312 if (!test_bit(__IXGBE_DOWN, &adapter->state))
2313 ixgbe_irq_enable(adapter);
9a799d71 2314 }
9a799d71
AK
2315 return work_done;
2316}
2317
2318/**
2319 * ixgbe_tx_timeout - Respond to a Tx Hang
2320 * @netdev: network interface device structure
2321 **/
2322static void ixgbe_tx_timeout(struct net_device *netdev)
2323{
2324 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2325
2326 /* Do the reset outside of interrupt context */
2327 schedule_work(&adapter->reset_task);
2328}
2329
2330static void ixgbe_reset_task(struct work_struct *work)
2331{
2332 struct ixgbe_adapter *adapter;
2333 adapter = container_of(work, struct ixgbe_adapter, reset_task);
2334
2f90b865
AD
2335 /* If we're already down or resetting, just bail */
2336 if (test_bit(__IXGBE_DOWN, &adapter->state) ||
2337 test_bit(__IXGBE_RESETTING, &adapter->state))
2338 return;
2339
9a799d71
AK
2340 adapter->tx_timeout_count++;
2341
d4f80882 2342 ixgbe_reinit_locked(adapter);
9a799d71
AK
2343}
2344
b9804972
JB
2345static void ixgbe_set_num_queues(struct ixgbe_adapter *adapter)
2346{
2347 int nrq = 1, ntq = 1;
2348 int feature_mask = 0, rss_i, rss_m;
2f90b865 2349 int dcb_i, dcb_m;
b9804972
JB
2350
2351 /* Number of supported queues */
2352 switch (adapter->hw.mac.type) {
2353 case ixgbe_mac_82598EB:
2f90b865
AD
2354 dcb_i = adapter->ring_feature[RING_F_DCB].indices;
2355 dcb_m = 0;
b9804972
JB
2356 rss_i = adapter->ring_feature[RING_F_RSS].indices;
2357 rss_m = 0;
2358 feature_mask |= IXGBE_FLAG_RSS_ENABLED;
2f90b865 2359 feature_mask |= IXGBE_FLAG_DCB_ENABLED;
b9804972
JB
2360
2361 switch (adapter->flags & feature_mask) {
2f90b865
AD
2362 case (IXGBE_FLAG_RSS_ENABLED | IXGBE_FLAG_DCB_ENABLED):
2363 dcb_m = 0x7 << 3;
2364 rss_i = min(8, rss_i);
2365 rss_m = 0x7;
2366 nrq = dcb_i * rss_i;
2367 ntq = min(MAX_TX_QUEUES, dcb_i * rss_i);
2368 break;
2369 case (IXGBE_FLAG_DCB_ENABLED):
2370 dcb_m = 0x7 << 3;
2371 nrq = dcb_i;
2372 ntq = dcb_i;
2373 break;
b9804972
JB
2374 case (IXGBE_FLAG_RSS_ENABLED):
2375 rss_m = 0xF;
2376 nrq = rss_i;
2377 ntq = rss_i;
2378 break;
2379 case 0:
2380 default:
2f90b865
AD
2381 dcb_i = 0;
2382 dcb_m = 0;
b9804972
JB
2383 rss_i = 0;
2384 rss_m = 0;
2385 nrq = 1;
2386 ntq = 1;
2387 break;
2388 }
2389
2f90b865
AD
2390 /* Sanity check, we should never have zero queues */
2391 nrq = (nrq ?:1);
2392 ntq = (ntq ?:1);
2393
2394 adapter->ring_feature[RING_F_DCB].indices = dcb_i;
2395 adapter->ring_feature[RING_F_DCB].mask = dcb_m;
b9804972
JB
2396 adapter->ring_feature[RING_F_RSS].indices = rss_i;
2397 adapter->ring_feature[RING_F_RSS].mask = rss_m;
2398 break;
2399 default:
2400 nrq = 1;
2401 ntq = 1;
2402 break;
2403 }
2404
2405 adapter->num_rx_queues = nrq;
2406 adapter->num_tx_queues = ntq;
2407}
2408
021230d4 2409static void ixgbe_acquire_msix_vectors(struct ixgbe_adapter *adapter,
b4617240 2410 int vectors)
021230d4
AV
2411{
2412 int err, vector_threshold;
2413
2414 /* We'll want at least 3 (vector_threshold):
2415 * 1) TxQ[0] Cleanup
2416 * 2) RxQ[0] Cleanup
2417 * 3) Other (Link Status Change, etc.)
2418 * 4) TCP Timer (optional)
2419 */
2420 vector_threshold = MIN_MSIX_COUNT;
2421
2422 /* The more we get, the more we will assign to Tx/Rx Cleanup
2423 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
2424 * Right now, we simply care about how many we'll get; we'll
2425 * set them up later while requesting irq's.
2426 */
2427 while (vectors >= vector_threshold) {
2428 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
b4617240 2429 vectors);
021230d4
AV
2430 if (!err) /* Success in acquiring all requested vectors. */
2431 break;
2432 else if (err < 0)
2433 vectors = 0; /* Nasty failure, quit now */
2434 else /* err == number of vectors we should try again with */
2435 vectors = err;
2436 }
2437
2438 if (vectors < vector_threshold) {
2439 /* Can't allocate enough MSI-X interrupts? Oh well.
2440 * This just means we'll go with either a single MSI
2441 * vector or fall back to legacy interrupts.
2442 */
2443 DPRINTK(HW, DEBUG, "Unable to allocate MSI-X interrupts\n");
2444 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
2445 kfree(adapter->msix_entries);
2446 adapter->msix_entries = NULL;
2f90b865 2447 adapter->flags &= ~IXGBE_FLAG_DCB_ENABLED;
021230d4 2448 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
b9804972 2449 ixgbe_set_num_queues(adapter);
021230d4
AV
2450 } else {
2451 adapter->flags |= IXGBE_FLAG_MSIX_ENABLED; /* Woot! */
2452 adapter->num_msix_vectors = vectors;
2453 }
2454}
2455
021230d4
AV
2456/**
2457 * ixgbe_cache_ring_register - Descriptor ring to register mapping
2458 * @adapter: board private structure to initialize
2459 *
2460 * Once we know the feature-set enabled for the device, we'll cache
2461 * the register offset the descriptor ring is assigned to.
2462 **/
2463static void __devinit ixgbe_cache_ring_register(struct ixgbe_adapter *adapter)
2464{
021230d4
AV
2465 int feature_mask = 0, rss_i;
2466 int i, txr_idx, rxr_idx;
2f90b865 2467 int dcb_i;
021230d4
AV
2468
2469 /* Number of supported queues */
2470 switch (adapter->hw.mac.type) {
2471 case ixgbe_mac_82598EB:
2f90b865 2472 dcb_i = adapter->ring_feature[RING_F_DCB].indices;
021230d4
AV
2473 rss_i = adapter->ring_feature[RING_F_RSS].indices;
2474 txr_idx = 0;
2475 rxr_idx = 0;
2f90b865 2476 feature_mask |= IXGBE_FLAG_DCB_ENABLED;
021230d4
AV
2477 feature_mask |= IXGBE_FLAG_RSS_ENABLED;
2478 switch (adapter->flags & feature_mask) {
2f90b865
AD
2479 case (IXGBE_FLAG_RSS_ENABLED | IXGBE_FLAG_DCB_ENABLED):
2480 for (i = 0; i < dcb_i; i++) {
2481 int j;
2482 /* Rx first */
2483 for (j = 0; j < adapter->num_rx_queues; j++) {
2484 adapter->rx_ring[rxr_idx].reg_idx =
2485 i << 3 | j;
2486 rxr_idx++;
2487 }
2488 /* Tx now */
2489 for (j = 0; j < adapter->num_tx_queues; j++) {
2490 adapter->tx_ring[txr_idx].reg_idx =
2491 i << 2 | (j >> 1);
2492 if (j & 1)
2493 txr_idx++;
2494 }
2495 }
2496 case (IXGBE_FLAG_DCB_ENABLED):
2497 /* the number of queues is assumed to be symmetric */
2498 for (i = 0; i < dcb_i; i++) {
2499 adapter->rx_ring[i].reg_idx = i << 3;
2500 adapter->tx_ring[i].reg_idx = i << 2;
2501 }
2502 break;
021230d4
AV
2503 case (IXGBE_FLAG_RSS_ENABLED):
2504 for (i = 0; i < adapter->num_rx_queues; i++)
2505 adapter->rx_ring[i].reg_idx = i;
2506 for (i = 0; i < adapter->num_tx_queues; i++)
2507 adapter->tx_ring[i].reg_idx = i;
2508 break;
2509 case 0:
2510 default:
2511 break;
2512 }
2513 break;
2514 default:
2515 break;
2516 }
2517}
2518
9a799d71
AK
2519/**
2520 * ixgbe_alloc_queues - Allocate memory for all rings
2521 * @adapter: board private structure to initialize
2522 *
2523 * We allocate one ring per queue at run-time since we don't know the
2524 * number of queues at compile-time. The polling_netdev array is
2525 * intended for Multiqueue, but should work fine with a single queue.
2526 **/
2f90b865 2527static int ixgbe_alloc_queues(struct ixgbe_adapter *adapter)
9a799d71
AK
2528{
2529 int i;
2530
2531 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
b4617240 2532 sizeof(struct ixgbe_ring), GFP_KERNEL);
9a799d71 2533 if (!adapter->tx_ring)
021230d4 2534 goto err_tx_ring_allocation;
9a799d71
AK
2535
2536 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
b4617240 2537 sizeof(struct ixgbe_ring), GFP_KERNEL);
021230d4
AV
2538 if (!adapter->rx_ring)
2539 goto err_rx_ring_allocation;
9a799d71 2540
021230d4 2541 for (i = 0; i < adapter->num_tx_queues; i++) {
b9804972 2542 adapter->tx_ring[i].count = adapter->tx_ring_count;
021230d4
AV
2543 adapter->tx_ring[i].queue_index = i;
2544 }
b9804972 2545
9a799d71 2546 for (i = 0; i < adapter->num_rx_queues; i++) {
b9804972 2547 adapter->rx_ring[i].count = adapter->rx_ring_count;
021230d4
AV
2548 adapter->rx_ring[i].queue_index = i;
2549 }
2550
2551 ixgbe_cache_ring_register(adapter);
2552
2553 return 0;
2554
2555err_rx_ring_allocation:
2556 kfree(adapter->tx_ring);
2557err_tx_ring_allocation:
2558 return -ENOMEM;
2559}
2560
2561/**
2562 * ixgbe_set_interrupt_capability - set MSI-X or MSI if supported
2563 * @adapter: board private structure to initialize
2564 *
2565 * Attempt to configure the interrupts using the best available
2566 * capabilities of the hardware and the kernel.
2567 **/
2568static int __devinit ixgbe_set_interrupt_capability(struct ixgbe_adapter
b4617240 2569 *adapter)
021230d4
AV
2570{
2571 int err = 0;
2572 int vector, v_budget;
2573
2574 /*
2575 * It's easy to be greedy for MSI-X vectors, but it really
2576 * doesn't do us much good if we have a lot more vectors
2577 * than CPU's. So let's be conservative and only ask for
2578 * (roughly) twice the number of vectors as there are CPU's.
2579 */
2580 v_budget = min(adapter->num_rx_queues + adapter->num_tx_queues,
b4617240 2581 (int)(num_online_cpus() * 2)) + NON_Q_VECTORS;
021230d4
AV
2582
2583 /*
2584 * At the same time, hardware can only support a maximum of
2585 * MAX_MSIX_COUNT vectors. With features such as RSS and VMDq,
2586 * we can easily reach upwards of 64 Rx descriptor queues and
2587 * 32 Tx queues. Thus, we cap it off in those rare cases where
2588 * the cpu count also exceeds our vector limit.
2589 */
2590 v_budget = min(v_budget, MAX_MSIX_COUNT);
2591
2592 /* A failure in MSI-X entry allocation isn't fatal, but it does
2593 * mean we disable MSI-X capabilities of the adapter. */
2594 adapter->msix_entries = kcalloc(v_budget,
b4617240 2595 sizeof(struct msix_entry), GFP_KERNEL);
021230d4 2596 if (!adapter->msix_entries) {
2f90b865 2597 adapter->flags &= ~IXGBE_FLAG_DCB_ENABLED;
021230d4
AV
2598 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
2599 ixgbe_set_num_queues(adapter);
2600 kfree(adapter->tx_ring);
2601 kfree(adapter->rx_ring);
2602 err = ixgbe_alloc_queues(adapter);
2603 if (err) {
2604 DPRINTK(PROBE, ERR, "Unable to allocate memory "
b4617240 2605 "for queues\n");
021230d4
AV
2606 goto out;
2607 }
2608
2609 goto try_msi;
2610 }
2611
2612 for (vector = 0; vector < v_budget; vector++)
2613 adapter->msix_entries[vector].entry = vector;
2614
2615 ixgbe_acquire_msix_vectors(adapter, v_budget);
2616
2617 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
2618 goto out;
2619
2620try_msi:
2621 err = pci_enable_msi(adapter->pdev);
2622 if (!err) {
2623 adapter->flags |= IXGBE_FLAG_MSI_ENABLED;
2624 } else {
2625 DPRINTK(HW, DEBUG, "Unable to allocate MSI interrupt, "
b4617240 2626 "falling back to legacy. Error: %d\n", err);
021230d4
AV
2627 /* reset err */
2628 err = 0;
2629 }
2630
2631out:
30eba97a 2632 /* Notify the stack of the (possibly) reduced Tx Queue count. */
fd2ea0a7 2633 adapter->netdev->real_num_tx_queues = adapter->num_tx_queues;
021230d4
AV
2634
2635 return err;
2636}
2637
2f90b865 2638void ixgbe_reset_interrupt_capability(struct ixgbe_adapter *adapter)
021230d4
AV
2639{
2640 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
2641 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
2642 pci_disable_msix(adapter->pdev);
2643 kfree(adapter->msix_entries);
2644 adapter->msix_entries = NULL;
2645 } else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED) {
2646 adapter->flags &= ~IXGBE_FLAG_MSI_ENABLED;
2647 pci_disable_msi(adapter->pdev);
2648 }
2649 return;
2650}
2651
2652/**
2653 * ixgbe_init_interrupt_scheme - Determine proper interrupt scheme
2654 * @adapter: board private structure to initialize
2655 *
2656 * We determine which interrupt scheme to use based on...
2657 * - Kernel support (MSI, MSI-X)
2658 * - which can be user-defined (via MODULE_PARAM)
2659 * - Hardware queue count (num_*_queues)
2660 * - defined by miscellaneous hardware support/features (RSS, etc.)
2661 **/
2f90b865 2662int ixgbe_init_interrupt_scheme(struct ixgbe_adapter *adapter)
021230d4
AV
2663{
2664 int err;
2665
2666 /* Number of supported queues */
2667 ixgbe_set_num_queues(adapter);
2668
2669 err = ixgbe_alloc_queues(adapter);
2670 if (err) {
2671 DPRINTK(PROBE, ERR, "Unable to allocate memory for queues\n");
2672 goto err_alloc_queues;
2673 }
2674
2675 err = ixgbe_set_interrupt_capability(adapter);
2676 if (err) {
2677 DPRINTK(PROBE, ERR, "Unable to setup interrupt capabilities\n");
2678 goto err_set_interrupt;
9a799d71
AK
2679 }
2680
021230d4 2681 DPRINTK(DRV, INFO, "Multiqueue %s: Rx Queue count = %u, "
b4617240
PW
2682 "Tx Queue count = %u\n",
2683 (adapter->num_rx_queues > 1) ? "Enabled" :
2684 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
021230d4
AV
2685
2686 set_bit(__IXGBE_DOWN, &adapter->state);
2687
9a799d71 2688 return 0;
021230d4
AV
2689
2690err_set_interrupt:
2691 kfree(adapter->tx_ring);
2692 kfree(adapter->rx_ring);
2693err_alloc_queues:
2694 return err;
9a799d71
AK
2695}
2696
c4900be0
DS
2697/**
2698 * ixgbe_sfp_timer - worker thread to find a missing module
2699 * @data: pointer to our adapter struct
2700 **/
2701static void ixgbe_sfp_timer(unsigned long data)
2702{
2703 struct ixgbe_adapter *adapter = (struct ixgbe_adapter *)data;
2704
2705 /* Do the sfp_timer outside of interrupt context due to the
2706 * delays that sfp+ detection requires
2707 */
2708 schedule_work(&adapter->sfp_task);
2709}
2710
2711/**
2712 * ixgbe_sfp_task - worker thread to find a missing module
2713 * @work: pointer to work_struct containing our data
2714 **/
2715static void ixgbe_sfp_task(struct work_struct *work)
2716{
2717 struct ixgbe_adapter *adapter = container_of(work,
2718 struct ixgbe_adapter,
2719 sfp_task);
2720 struct ixgbe_hw *hw = &adapter->hw;
2721
2722 if ((hw->phy.type == ixgbe_phy_nl) &&
2723 (hw->phy.sfp_type == ixgbe_sfp_type_not_present)) {
2724 s32 ret = hw->phy.ops.identify_sfp(hw);
2725 if (ret)
2726 goto reschedule;
2727 ret = hw->phy.ops.reset(hw);
2728 if (ret == IXGBE_ERR_SFP_NOT_SUPPORTED) {
2729 DPRINTK(PROBE, ERR, "failed to initialize because an "
2730 "unsupported SFP+ module type was detected.\n"
2731 "Reload the driver after installing a "
2732 "supported module.\n");
2733 unregister_netdev(adapter->netdev);
2734 } else {
2735 DPRINTK(PROBE, INFO, "detected SFP+: %d\n",
2736 hw->phy.sfp_type);
2737 }
2738 /* don't need this routine any more */
2739 clear_bit(__IXGBE_SFP_MODULE_NOT_FOUND, &adapter->state);
2740 }
2741 return;
2742reschedule:
2743 if (test_bit(__IXGBE_SFP_MODULE_NOT_FOUND, &adapter->state))
2744 mod_timer(&adapter->sfp_timer,
2745 round_jiffies(jiffies + (2 * HZ)));
2746}
2747
9a799d71
AK
2748/**
2749 * ixgbe_sw_init - Initialize general software structures (struct ixgbe_adapter)
2750 * @adapter: board private structure to initialize
2751 *
2752 * ixgbe_sw_init initializes the Adapter private data structure.
2753 * Fields are initialized based on PCI device information and
2754 * OS network device settings (MTU size).
2755 **/
2756static int __devinit ixgbe_sw_init(struct ixgbe_adapter *adapter)
2757{
2758 struct ixgbe_hw *hw = &adapter->hw;
2759 struct pci_dev *pdev = adapter->pdev;
021230d4 2760 unsigned int rss;
7a6b6f51 2761#ifdef CONFIG_IXGBE_DCB
2f90b865
AD
2762 int j;
2763 struct tc_configuration *tc;
2764#endif
021230d4 2765
c44ade9e
JB
2766 /* PCI config space info */
2767
2768 hw->vendor_id = pdev->vendor;
2769 hw->device_id = pdev->device;
2770 hw->revision_id = pdev->revision;
2771 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2772 hw->subsystem_device_id = pdev->subsystem_device;
2773
021230d4
AV
2774 /* Set capability flags */
2775 rss = min(IXGBE_MAX_RSS_INDICES, (int)num_online_cpus());
2776 adapter->ring_feature[RING_F_RSS].indices = rss;
2777 adapter->flags |= IXGBE_FLAG_RSS_ENABLED;
2f90b865
AD
2778 adapter->ring_feature[RING_F_DCB].indices = IXGBE_MAX_DCB_INDICES;
2779
7a6b6f51 2780#ifdef CONFIG_IXGBE_DCB
2f90b865
AD
2781 /* Configure DCB traffic classes */
2782 for (j = 0; j < MAX_TRAFFIC_CLASS; j++) {
2783 tc = &adapter->dcb_cfg.tc_config[j];
2784 tc->path[DCB_TX_CONFIG].bwg_id = 0;
2785 tc->path[DCB_TX_CONFIG].bwg_percent = 12 + (j & 1);
2786 tc->path[DCB_RX_CONFIG].bwg_id = 0;
2787 tc->path[DCB_RX_CONFIG].bwg_percent = 12 + (j & 1);
2788 tc->dcb_pfc = pfc_disabled;
2789 }
2790 adapter->dcb_cfg.bw_percentage[DCB_TX_CONFIG][0] = 100;
2791 adapter->dcb_cfg.bw_percentage[DCB_RX_CONFIG][0] = 100;
2792 adapter->dcb_cfg.rx_pba_cfg = pba_equal;
2793 adapter->dcb_cfg.round_robin_enable = false;
2794 adapter->dcb_set_bitmap = 0x00;
2795 ixgbe_copy_dcb_cfg(&adapter->dcb_cfg, &adapter->temp_dcb_cfg,
2796 adapter->ring_feature[RING_F_DCB].indices);
2797
2798#endif
0befdb3e
JB
2799 if (hw->mac.ops.get_media_type &&
2800 (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_copper))
2801 adapter->flags |= IXGBE_FLAG_FAN_FAIL_CAPABLE;
9a799d71
AK
2802
2803 /* default flow control settings */
2b9ade93
JB
2804 hw->fc.original_type = ixgbe_fc_none;
2805 hw->fc.type = ixgbe_fc_none;
2806 hw->fc.high_water = IXGBE_DEFAULT_FCRTH;
2807 hw->fc.low_water = IXGBE_DEFAULT_FCRTL;
2808 hw->fc.pause_time = IXGBE_DEFAULT_FCPAUSE;
2809 hw->fc.send_xon = true;
9a799d71 2810
021230d4 2811 /* select 10G link by default */
9a799d71 2812 hw->mac.link_mode_select = IXGBE_AUTOC_LMS_10G_LINK_NO_AN;
9a799d71 2813
30efa5a3
JB
2814 /* enable itr by default in dynamic mode */
2815 adapter->itr_setting = 1;
2816 adapter->eitr_param = 20000;
2817
2818 /* set defaults for eitr in MegaBytes */
2819 adapter->eitr_low = 10;
2820 adapter->eitr_high = 20;
2821
2822 /* set default ring sizes */
2823 adapter->tx_ring_count = IXGBE_DEFAULT_TXD;
2824 adapter->rx_ring_count = IXGBE_DEFAULT_RXD;
2825
9a799d71 2826 /* initialize eeprom parameters */
c44ade9e 2827 if (ixgbe_init_eeprom_params_generic(hw)) {
9a799d71
AK
2828 dev_err(&pdev->dev, "EEPROM initialization failed\n");
2829 return -EIO;
2830 }
2831
021230d4 2832 /* enable rx csum by default */
9a799d71
AK
2833 adapter->flags |= IXGBE_FLAG_RX_CSUM_ENABLED;
2834
9a799d71
AK
2835 set_bit(__IXGBE_DOWN, &adapter->state);
2836
2837 return 0;
2838}
2839
2840/**
2841 * ixgbe_setup_tx_resources - allocate Tx resources (Descriptors)
2842 * @adapter: board private structure
3a581073 2843 * @tx_ring: tx descriptor ring (for a specific queue) to setup
9a799d71
AK
2844 *
2845 * Return 0 on success, negative on failure
2846 **/
2847int ixgbe_setup_tx_resources(struct ixgbe_adapter *adapter,
e01c31a5 2848 struct ixgbe_ring *tx_ring)
9a799d71
AK
2849{
2850 struct pci_dev *pdev = adapter->pdev;
2851 int size;
2852
3a581073
JB
2853 size = sizeof(struct ixgbe_tx_buffer) * tx_ring->count;
2854 tx_ring->tx_buffer_info = vmalloc(size);
e01c31a5
JB
2855 if (!tx_ring->tx_buffer_info)
2856 goto err;
3a581073 2857 memset(tx_ring->tx_buffer_info, 0, size);
9a799d71
AK
2858
2859 /* round up to nearest 4K */
e01c31a5
JB
2860 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc) +
2861 sizeof(u32);
3a581073 2862 tx_ring->size = ALIGN(tx_ring->size, 4096);
9a799d71 2863
3a581073
JB
2864 tx_ring->desc = pci_alloc_consistent(pdev, tx_ring->size,
2865 &tx_ring->dma);
e01c31a5
JB
2866 if (!tx_ring->desc)
2867 goto err;
9a799d71 2868
3a581073
JB
2869 tx_ring->next_to_use = 0;
2870 tx_ring->next_to_clean = 0;
2871 tx_ring->work_limit = tx_ring->count;
9a799d71 2872 return 0;
e01c31a5
JB
2873
2874err:
2875 vfree(tx_ring->tx_buffer_info);
2876 tx_ring->tx_buffer_info = NULL;
2877 DPRINTK(PROBE, ERR, "Unable to allocate memory for the transmit "
2878 "descriptor ring\n");
2879 return -ENOMEM;
9a799d71
AK
2880}
2881
69888674
AD
2882/**
2883 * ixgbe_setup_all_tx_resources - allocate all queues Tx resources
2884 * @adapter: board private structure
2885 *
2886 * If this function returns with an error, then it's possible one or
2887 * more of the rings is populated (while the rest are not). It is the
2888 * callers duty to clean those orphaned rings.
2889 *
2890 * Return 0 on success, negative on failure
2891 **/
2892static int ixgbe_setup_all_tx_resources(struct ixgbe_adapter *adapter)
2893{
2894 int i, err = 0;
2895
2896 for (i = 0; i < adapter->num_tx_queues; i++) {
2897 err = ixgbe_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2898 if (!err)
2899 continue;
2900 DPRINTK(PROBE, ERR, "Allocation for Tx Queue %u failed\n", i);
2901 break;
2902 }
2903
2904 return err;
2905}
2906
9a799d71
AK
2907/**
2908 * ixgbe_setup_rx_resources - allocate Rx resources (Descriptors)
2909 * @adapter: board private structure
3a581073 2910 * @rx_ring: rx descriptor ring (for a specific queue) to setup
9a799d71
AK
2911 *
2912 * Returns 0 on success, negative on failure
2913 **/
2914int ixgbe_setup_rx_resources(struct ixgbe_adapter *adapter,
b4617240 2915 struct ixgbe_ring *rx_ring)
9a799d71
AK
2916{
2917 struct pci_dev *pdev = adapter->pdev;
021230d4 2918 int size;
9a799d71 2919
177db6ff 2920 size = sizeof(struct net_lro_desc) * IXGBE_MAX_LRO_DESCRIPTORS;
3a581073
JB
2921 rx_ring->lro_mgr.lro_arr = vmalloc(size);
2922 if (!rx_ring->lro_mgr.lro_arr)
177db6ff 2923 return -ENOMEM;
3a581073 2924 memset(rx_ring->lro_mgr.lro_arr, 0, size);
177db6ff 2925
3a581073
JB
2926 size = sizeof(struct ixgbe_rx_buffer) * rx_ring->count;
2927 rx_ring->rx_buffer_info = vmalloc(size);
2928 if (!rx_ring->rx_buffer_info) {
9a799d71 2929 DPRINTK(PROBE, ERR,
b4617240 2930 "vmalloc allocation failed for the rx desc ring\n");
177db6ff 2931 goto alloc_failed;
9a799d71 2932 }
3a581073 2933 memset(rx_ring->rx_buffer_info, 0, size);
9a799d71 2934
9a799d71 2935 /* Round up to nearest 4K */
3a581073
JB
2936 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2937 rx_ring->size = ALIGN(rx_ring->size, 4096);
9a799d71 2938
3a581073 2939 rx_ring->desc = pci_alloc_consistent(pdev, rx_ring->size, &rx_ring->dma);
9a799d71 2940
3a581073 2941 if (!rx_ring->desc) {
9a799d71 2942 DPRINTK(PROBE, ERR,
b4617240 2943 "Memory allocation failed for the rx desc ring\n");
3a581073 2944 vfree(rx_ring->rx_buffer_info);
177db6ff 2945 goto alloc_failed;
9a799d71
AK
2946 }
2947
3a581073
JB
2948 rx_ring->next_to_clean = 0;
2949 rx_ring->next_to_use = 0;
9a799d71
AK
2950
2951 return 0;
177db6ff
MC
2952
2953alloc_failed:
3a581073
JB
2954 vfree(rx_ring->lro_mgr.lro_arr);
2955 rx_ring->lro_mgr.lro_arr = NULL;
177db6ff 2956 return -ENOMEM;
9a799d71
AK
2957}
2958
69888674
AD
2959/**
2960 * ixgbe_setup_all_rx_resources - allocate all queues Rx resources
2961 * @adapter: board private structure
2962 *
2963 * If this function returns with an error, then it's possible one or
2964 * more of the rings is populated (while the rest are not). It is the
2965 * callers duty to clean those orphaned rings.
2966 *
2967 * Return 0 on success, negative on failure
2968 **/
2969
2970static int ixgbe_setup_all_rx_resources(struct ixgbe_adapter *adapter)
2971{
2972 int i, err = 0;
2973
2974 for (i = 0; i < adapter->num_rx_queues; i++) {
2975 err = ixgbe_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2976 if (!err)
2977 continue;
2978 DPRINTK(PROBE, ERR, "Allocation for Rx Queue %u failed\n", i);
2979 break;
2980 }
2981
2982 return err;
2983}
2984
9a799d71
AK
2985/**
2986 * ixgbe_free_tx_resources - Free Tx Resources per Queue
2987 * @adapter: board private structure
2988 * @tx_ring: Tx descriptor ring for a specific queue
2989 *
2990 * Free all transmit software resources
2991 **/
c431f97e
JB
2992void ixgbe_free_tx_resources(struct ixgbe_adapter *adapter,
2993 struct ixgbe_ring *tx_ring)
9a799d71
AK
2994{
2995 struct pci_dev *pdev = adapter->pdev;
2996
2997 ixgbe_clean_tx_ring(adapter, tx_ring);
2998
2999 vfree(tx_ring->tx_buffer_info);
3000 tx_ring->tx_buffer_info = NULL;
3001
3002 pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
3003
3004 tx_ring->desc = NULL;
3005}
3006
3007/**
3008 * ixgbe_free_all_tx_resources - Free Tx Resources for All Queues
3009 * @adapter: board private structure
3010 *
3011 * Free all transmit software resources
3012 **/
3013static void ixgbe_free_all_tx_resources(struct ixgbe_adapter *adapter)
3014{
3015 int i;
3016
3017 for (i = 0; i < adapter->num_tx_queues; i++)
3018 ixgbe_free_tx_resources(adapter, &adapter->tx_ring[i]);
3019}
3020
3021/**
b4617240 3022 * ixgbe_free_rx_resources - Free Rx Resources
9a799d71
AK
3023 * @adapter: board private structure
3024 * @rx_ring: ring to clean the resources from
3025 *
3026 * Free all receive software resources
3027 **/
c431f97e
JB
3028void ixgbe_free_rx_resources(struct ixgbe_adapter *adapter,
3029 struct ixgbe_ring *rx_ring)
9a799d71
AK
3030{
3031 struct pci_dev *pdev = adapter->pdev;
3032
177db6ff
MC
3033 vfree(rx_ring->lro_mgr.lro_arr);
3034 rx_ring->lro_mgr.lro_arr = NULL;
3035
9a799d71
AK
3036 ixgbe_clean_rx_ring(adapter, rx_ring);
3037
3038 vfree(rx_ring->rx_buffer_info);
3039 rx_ring->rx_buffer_info = NULL;
3040
3041 pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
3042
3043 rx_ring->desc = NULL;
3044}
3045
3046/**
3047 * ixgbe_free_all_rx_resources - Free Rx Resources for All Queues
3048 * @adapter: board private structure
3049 *
3050 * Free all receive software resources
3051 **/
3052static void ixgbe_free_all_rx_resources(struct ixgbe_adapter *adapter)
3053{
3054 int i;
3055
3056 for (i = 0; i < adapter->num_rx_queues; i++)
3057 ixgbe_free_rx_resources(adapter, &adapter->rx_ring[i]);
3058}
3059
9a799d71
AK
3060/**
3061 * ixgbe_change_mtu - Change the Maximum Transfer Unit
3062 * @netdev: network interface device structure
3063 * @new_mtu: new value for maximum frame size
3064 *
3065 * Returns 0 on success, negative on failure
3066 **/
3067static int ixgbe_change_mtu(struct net_device *netdev, int new_mtu)
3068{
3069 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3070 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3071
42c783c5
JB
3072 /* MTU < 68 is an error and causes problems on some kernels */
3073 if ((new_mtu < 68) || (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE))
9a799d71
AK
3074 return -EINVAL;
3075
021230d4 3076 DPRINTK(PROBE, INFO, "changing MTU from %d to %d\n",
b4617240 3077 netdev->mtu, new_mtu);
021230d4 3078 /* must set new MTU before calling down or up */
9a799d71
AK
3079 netdev->mtu = new_mtu;
3080
d4f80882
AV
3081 if (netif_running(netdev))
3082 ixgbe_reinit_locked(adapter);
9a799d71
AK
3083
3084 return 0;
3085}
3086
3087/**
3088 * ixgbe_open - Called when a network interface is made active
3089 * @netdev: network interface device structure
3090 *
3091 * Returns 0 on success, negative value on failure
3092 *
3093 * The open entry point is called when a network interface is made
3094 * active by the system (IFF_UP). At this point all resources needed
3095 * for transmit and receive operations are allocated, the interrupt
3096 * handler is registered with the OS, the watchdog timer is started,
3097 * and the stack is notified that the interface is ready.
3098 **/
3099static int ixgbe_open(struct net_device *netdev)
3100{
3101 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3102 int err;
4bebfaa5
AK
3103
3104 /* disallow open during test */
3105 if (test_bit(__IXGBE_TESTING, &adapter->state))
3106 return -EBUSY;
9a799d71 3107
9a799d71
AK
3108 /* allocate transmit descriptors */
3109 err = ixgbe_setup_all_tx_resources(adapter);
3110 if (err)
3111 goto err_setup_tx;
3112
9a799d71
AK
3113 /* allocate receive descriptors */
3114 err = ixgbe_setup_all_rx_resources(adapter);
3115 if (err)
3116 goto err_setup_rx;
3117
3118 ixgbe_configure(adapter);
3119
021230d4 3120 err = ixgbe_request_irq(adapter);
9a799d71
AK
3121 if (err)
3122 goto err_req_irq;
3123
9a799d71
AK
3124 err = ixgbe_up_complete(adapter);
3125 if (err)
3126 goto err_up;
3127
d55b53ff
JK
3128 netif_tx_start_all_queues(netdev);
3129
9a799d71
AK
3130 return 0;
3131
3132err_up:
5eba3699 3133 ixgbe_release_hw_control(adapter);
9a799d71
AK
3134 ixgbe_free_irq(adapter);
3135err_req_irq:
3136 ixgbe_free_all_rx_resources(adapter);
3137err_setup_rx:
3138 ixgbe_free_all_tx_resources(adapter);
3139err_setup_tx:
3140 ixgbe_reset(adapter);
3141
3142 return err;
3143}
3144
3145/**
3146 * ixgbe_close - Disables a network interface
3147 * @netdev: network interface device structure
3148 *
3149 * Returns 0, this is not allowed to fail
3150 *
3151 * The close entry point is called when an interface is de-activated
3152 * by the OS. The hardware is still under the drivers control, but
3153 * needs to be disabled. A global MAC reset is issued to stop the
3154 * hardware, and all transmit and receive resources are freed.
3155 **/
3156static int ixgbe_close(struct net_device *netdev)
3157{
3158 struct ixgbe_adapter *adapter = netdev_priv(netdev);
9a799d71
AK
3159
3160 ixgbe_down(adapter);
3161 ixgbe_free_irq(adapter);
3162
3163 ixgbe_free_all_tx_resources(adapter);
3164 ixgbe_free_all_rx_resources(adapter);
3165
5eba3699 3166 ixgbe_release_hw_control(adapter);
9a799d71
AK
3167
3168 return 0;
3169}
3170
b3c8b4ba
AD
3171/**
3172 * ixgbe_napi_add_all - prep napi structs for use
3173 * @adapter: private struct
3174 * helper function to napi_add each possible q_vector->napi
3175 */
2f90b865 3176void ixgbe_napi_add_all(struct ixgbe_adapter *adapter)
b3c8b4ba
AD
3177{
3178 int q_idx, q_vectors;
3179 int (*poll)(struct napi_struct *, int);
3180
3181 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
3182 poll = &ixgbe_clean_rxonly;
3183 /* Only enable as many vectors as we have rx queues. */
3184 q_vectors = adapter->num_rx_queues;
3185 } else {
3186 poll = &ixgbe_poll;
3187 /* only one q_vector for legacy modes */
3188 q_vectors = 1;
3189 }
3190
3191 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
3192 struct ixgbe_q_vector *q_vector = &adapter->q_vector[q_idx];
3193 netif_napi_add(adapter->netdev, &q_vector->napi, (*poll), 64);
3194 }
3195}
3196
2f90b865 3197void ixgbe_napi_del_all(struct ixgbe_adapter *adapter)
b3c8b4ba
AD
3198{
3199 int q_idx;
3200 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
3201
3202 /* legacy and MSI only use one vector */
3203 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
3204 q_vectors = 1;
3205
3206 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
3207 struct ixgbe_q_vector *q_vector = &adapter->q_vector[q_idx];
3208 if (!q_vector->rxr_count)
3209 continue;
3210 netif_napi_del(&q_vector->napi);
3211 }
3212}
3213
3214#ifdef CONFIG_PM
3215static int ixgbe_resume(struct pci_dev *pdev)
3216{
3217 struct net_device *netdev = pci_get_drvdata(pdev);
3218 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3219 u32 err;
3220
3221 pci_set_power_state(pdev, PCI_D0);
3222 pci_restore_state(pdev);
3223 err = pci_enable_device(pdev);
3224 if (err) {
69888674 3225 printk(KERN_ERR "ixgbe: Cannot enable PCI device from "
b3c8b4ba
AD
3226 "suspend\n");
3227 return err;
3228 }
3229 pci_set_master(pdev);
3230
3231 pci_enable_wake(pdev, PCI_D3hot, 0);
3232 pci_enable_wake(pdev, PCI_D3cold, 0);
3233
3234 err = ixgbe_init_interrupt_scheme(adapter);
3235 if (err) {
3236 printk(KERN_ERR "ixgbe: Cannot initialize interrupts for "
3237 "device\n");
3238 return err;
3239 }
3240
3241 ixgbe_napi_add_all(adapter);
3242 ixgbe_reset(adapter);
3243
3244 if (netif_running(netdev)) {
3245 err = ixgbe_open(adapter->netdev);
3246 if (err)
3247 return err;
3248 }
3249
3250 netif_device_attach(netdev);
3251
3252 return 0;
3253}
3254
3255#endif /* CONFIG_PM */
3256static int ixgbe_suspend(struct pci_dev *pdev, pm_message_t state)
3257{
3258 struct net_device *netdev = pci_get_drvdata(pdev);
3259 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3260#ifdef CONFIG_PM
3261 int retval = 0;
3262#endif
3263
3264 netif_device_detach(netdev);
3265
3266 if (netif_running(netdev)) {
3267 ixgbe_down(adapter);
3268 ixgbe_free_irq(adapter);
3269 ixgbe_free_all_tx_resources(adapter);
3270 ixgbe_free_all_rx_resources(adapter);
3271 }
3272 ixgbe_reset_interrupt_capability(adapter);
3273 ixgbe_napi_del_all(adapter);
3274 kfree(adapter->tx_ring);
3275 kfree(adapter->rx_ring);
3276
3277#ifdef CONFIG_PM
3278 retval = pci_save_state(pdev);
3279 if (retval)
3280 return retval;
3281#endif
3282
3283 pci_enable_wake(pdev, PCI_D3hot, 0);
3284 pci_enable_wake(pdev, PCI_D3cold, 0);
3285
3286 ixgbe_release_hw_control(adapter);
3287
3288 pci_disable_device(pdev);
3289
3290 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3291
3292 return 0;
3293}
3294
3295static void ixgbe_shutdown(struct pci_dev *pdev)
3296{
3297 ixgbe_suspend(pdev, PMSG_SUSPEND);
3298}
3299
9a799d71
AK
3300/**
3301 * ixgbe_update_stats - Update the board statistics counters.
3302 * @adapter: board private structure
3303 **/
3304void ixgbe_update_stats(struct ixgbe_adapter *adapter)
3305{
3306 struct ixgbe_hw *hw = &adapter->hw;
6f11eef7
AV
3307 u64 total_mpc = 0;
3308 u32 i, missed_rx = 0, mpc, bprc, lxon, lxoff, xon_off_tot;
9a799d71
AK
3309
3310 adapter->stats.crcerrs += IXGBE_READ_REG(hw, IXGBE_CRCERRS);
6f11eef7
AV
3311 for (i = 0; i < 8; i++) {
3312 /* for packet buffers not used, the register should read 0 */
3313 mpc = IXGBE_READ_REG(hw, IXGBE_MPC(i));
3314 missed_rx += mpc;
3315 adapter->stats.mpc[i] += mpc;
3316 total_mpc += adapter->stats.mpc[i];
3317 adapter->stats.rnbc[i] += IXGBE_READ_REG(hw, IXGBE_RNBC(i));
2f90b865
AD
3318 adapter->stats.qptc[i] += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
3319 adapter->stats.qbtc[i] += IXGBE_READ_REG(hw, IXGBE_QBTC(i));
3320 adapter->stats.qprc[i] += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
3321 adapter->stats.qbrc[i] += IXGBE_READ_REG(hw, IXGBE_QBRC(i));
3322 adapter->stats.pxonrxc[i] += IXGBE_READ_REG(hw,
3323 IXGBE_PXONRXC(i));
3324 adapter->stats.pxontxc[i] += IXGBE_READ_REG(hw,
3325 IXGBE_PXONTXC(i));
3326 adapter->stats.pxoffrxc[i] += IXGBE_READ_REG(hw,
3327 IXGBE_PXOFFRXC(i));
3328 adapter->stats.pxofftxc[i] += IXGBE_READ_REG(hw,
3329 IXGBE_PXOFFTXC(i));
6f11eef7
AV
3330 }
3331 adapter->stats.gprc += IXGBE_READ_REG(hw, IXGBE_GPRC);
3332 /* work around hardware counting issue */
3333 adapter->stats.gprc -= missed_rx;
3334
3335 /* 82598 hardware only has a 32 bit counter in the high register */
9a799d71 3336 adapter->stats.gorc += IXGBE_READ_REG(hw, IXGBE_GORCH);
6f11eef7
AV
3337 adapter->stats.gotc += IXGBE_READ_REG(hw, IXGBE_GOTCH);
3338 adapter->stats.tor += IXGBE_READ_REG(hw, IXGBE_TORH);
9a799d71
AK
3339 bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
3340 adapter->stats.bprc += bprc;
3341 adapter->stats.mprc += IXGBE_READ_REG(hw, IXGBE_MPRC);
3342 adapter->stats.mprc -= bprc;
3343 adapter->stats.roc += IXGBE_READ_REG(hw, IXGBE_ROC);
3344 adapter->stats.prc64 += IXGBE_READ_REG(hw, IXGBE_PRC64);
3345 adapter->stats.prc127 += IXGBE_READ_REG(hw, IXGBE_PRC127);
3346 adapter->stats.prc255 += IXGBE_READ_REG(hw, IXGBE_PRC255);
3347 adapter->stats.prc511 += IXGBE_READ_REG(hw, IXGBE_PRC511);
3348 adapter->stats.prc1023 += IXGBE_READ_REG(hw, IXGBE_PRC1023);
3349 adapter->stats.prc1522 += IXGBE_READ_REG(hw, IXGBE_PRC1522);
9a799d71
AK
3350 adapter->stats.rlec += IXGBE_READ_REG(hw, IXGBE_RLEC);
3351 adapter->stats.lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
9a799d71 3352 adapter->stats.lxoffrxc += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
6f11eef7
AV
3353 lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
3354 adapter->stats.lxontxc += lxon;
3355 lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
3356 adapter->stats.lxofftxc += lxoff;
9a799d71
AK
3357 adapter->stats.ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
3358 adapter->stats.gptc += IXGBE_READ_REG(hw, IXGBE_GPTC);
6f11eef7
AV
3359 adapter->stats.mptc += IXGBE_READ_REG(hw, IXGBE_MPTC);
3360 /*
3361 * 82598 errata - tx of flow control packets is included in tx counters
3362 */
3363 xon_off_tot = lxon + lxoff;
3364 adapter->stats.gptc -= xon_off_tot;
3365 adapter->stats.mptc -= xon_off_tot;
3366 adapter->stats.gotc -= (xon_off_tot * (ETH_ZLEN + ETH_FCS_LEN));
9a799d71
AK
3367 adapter->stats.ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
3368 adapter->stats.rfc += IXGBE_READ_REG(hw, IXGBE_RFC);
3369 adapter->stats.rjc += IXGBE_READ_REG(hw, IXGBE_RJC);
9a799d71
AK
3370 adapter->stats.tpr += IXGBE_READ_REG(hw, IXGBE_TPR);
3371 adapter->stats.ptc64 += IXGBE_READ_REG(hw, IXGBE_PTC64);
6f11eef7 3372 adapter->stats.ptc64 -= xon_off_tot;
9a799d71
AK
3373 adapter->stats.ptc127 += IXGBE_READ_REG(hw, IXGBE_PTC127);
3374 adapter->stats.ptc255 += IXGBE_READ_REG(hw, IXGBE_PTC255);
3375 adapter->stats.ptc511 += IXGBE_READ_REG(hw, IXGBE_PTC511);
3376 adapter->stats.ptc1023 += IXGBE_READ_REG(hw, IXGBE_PTC1023);
3377 adapter->stats.ptc1522 += IXGBE_READ_REG(hw, IXGBE_PTC1522);
9a799d71
AK
3378 adapter->stats.bptc += IXGBE_READ_REG(hw, IXGBE_BPTC);
3379
3380 /* Fill out the OS statistics structure */
9a799d71
AK
3381 adapter->net_stats.multicast = adapter->stats.mprc;
3382
3383 /* Rx Errors */
3384 adapter->net_stats.rx_errors = adapter->stats.crcerrs +
b4617240 3385 adapter->stats.rlec;
9a799d71
AK
3386 adapter->net_stats.rx_dropped = 0;
3387 adapter->net_stats.rx_length_errors = adapter->stats.rlec;
3388 adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
6f11eef7 3389 adapter->net_stats.rx_missed_errors = total_mpc;
9a799d71
AK
3390}
3391
3392/**
3393 * ixgbe_watchdog - Timer Call-back
3394 * @data: pointer to adapter cast into an unsigned long
3395 **/
3396static void ixgbe_watchdog(unsigned long data)
3397{
3398 struct ixgbe_adapter *adapter = (struct ixgbe_adapter *)data;
cf8280ee
JB
3399 struct ixgbe_hw *hw = &adapter->hw;
3400
3401 /* Do the watchdog outside of interrupt context due to the lovely
3402 * delays that some of the newer hardware requires */
3403 if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
3404 /* Cause software interrupt to ensure rx rings are cleaned */
3405 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
3406 u32 eics =
3407 (1 << (adapter->num_msix_vectors - NON_Q_VECTORS)) - 1;
3408 IXGBE_WRITE_REG(hw, IXGBE_EICS, eics);
3409 } else {
3410 /* For legacy and MSI interrupts don't set any bits that
3411 * are enabled for EIAM, because this operation would
3412 * set *both* EIMS and EICS for any bit in EIAM */
3413 IXGBE_WRITE_REG(hw, IXGBE_EICS,
3414 (IXGBE_EICS_TCP_TIMER | IXGBE_EICS_OTHER));
3415 }
3416 /* Reset the timer */
3417 mod_timer(&adapter->watchdog_timer,
3418 round_jiffies(jiffies + 2 * HZ));
3419 }
9a799d71 3420
cf8280ee
JB
3421 schedule_work(&adapter->watchdog_task);
3422}
3423
3424/**
69888674
AD
3425 * ixgbe_watchdog_task - worker thread to bring link up
3426 * @work: pointer to work_struct containing our data
cf8280ee
JB
3427 **/
3428static void ixgbe_watchdog_task(struct work_struct *work)
3429{
3430 struct ixgbe_adapter *adapter = container_of(work,
3431 struct ixgbe_adapter,
3432 watchdog_task);
3433 struct net_device *netdev = adapter->netdev;
3434 struct ixgbe_hw *hw = &adapter->hw;
3435 u32 link_speed = adapter->link_speed;
3436 bool link_up = adapter->link_up;
3437
3438 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
3439
3440 if (adapter->flags & IXGBE_FLAG_NEED_LINK_UPDATE) {
3441 hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
3442 if (link_up ||
3443 time_after(jiffies, (adapter->link_check_timeout +
3444 IXGBE_TRY_LINK_TIMEOUT))) {
3445 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMC_LSC);
3446 adapter->flags &= ~IXGBE_FLAG_NEED_LINK_UPDATE;
3447 }
3448 adapter->link_up = link_up;
3449 adapter->link_speed = link_speed;
3450 }
9a799d71
AK
3451
3452 if (link_up) {
3453 if (!netif_carrier_ok(netdev)) {
cf8280ee
JB
3454 u32 frctl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
3455 u32 rmcs = IXGBE_READ_REG(hw, IXGBE_RMCS);
9a799d71
AK
3456#define FLOW_RX (frctl & IXGBE_FCTRL_RFCE)
3457#define FLOW_TX (rmcs & IXGBE_RMCS_TFCE_802_3X)
a46e534b
JK
3458 printk(KERN_INFO "ixgbe: %s NIC Link is Up %s, "
3459 "Flow Control: %s\n",
3460 netdev->name,
3461 (link_speed == IXGBE_LINK_SPEED_10GB_FULL ?
3462 "10 Gbps" :
3463 (link_speed == IXGBE_LINK_SPEED_1GB_FULL ?
3464 "1 Gbps" : "unknown speed")),
3465 ((FLOW_RX && FLOW_TX) ? "RX/TX" :
3466 (FLOW_RX ? "RX" :
3467 (FLOW_TX ? "TX" : "None"))));
9a799d71
AK
3468
3469 netif_carrier_on(netdev);
2f90b865 3470 netif_tx_wake_all_queues(netdev);
9a799d71
AK
3471 } else {
3472 /* Force detection of hung controller */
3473 adapter->detect_tx_hung = true;
3474 }
3475 } else {
cf8280ee
JB
3476 adapter->link_up = false;
3477 adapter->link_speed = 0;
9a799d71 3478 if (netif_carrier_ok(netdev)) {
a46e534b
JK
3479 printk(KERN_INFO "ixgbe: %s NIC Link is Down\n",
3480 netdev->name);
9a799d71 3481 netif_carrier_off(netdev);
2f90b865 3482 netif_tx_stop_all_queues(netdev);
9a799d71
AK
3483 }
3484 }
3485
3486 ixgbe_update_stats(adapter);
cf8280ee 3487 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
9a799d71
AK
3488}
3489
9a799d71 3490static int ixgbe_tso(struct ixgbe_adapter *adapter,
b4617240
PW
3491 struct ixgbe_ring *tx_ring, struct sk_buff *skb,
3492 u32 tx_flags, u8 *hdr_len)
9a799d71
AK
3493{
3494 struct ixgbe_adv_tx_context_desc *context_desc;
3495 unsigned int i;
3496 int err;
3497 struct ixgbe_tx_buffer *tx_buffer_info;
9f8cdf4f
JB
3498 u32 vlan_macip_lens = 0, type_tucmd_mlhl;
3499 u32 mss_l4len_idx, l4len;
9a799d71
AK
3500
3501 if (skb_is_gso(skb)) {
3502 if (skb_header_cloned(skb)) {
3503 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3504 if (err)
3505 return err;
3506 }
3507 l4len = tcp_hdrlen(skb);
3508 *hdr_len += l4len;
3509
8327d000 3510 if (skb->protocol == htons(ETH_P_IP)) {
9a799d71
AK
3511 struct iphdr *iph = ip_hdr(skb);
3512 iph->tot_len = 0;
3513 iph->check = 0;
3514 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
b4617240
PW
3515 iph->daddr, 0,
3516 IPPROTO_TCP,
3517 0);
9a799d71
AK
3518 adapter->hw_tso_ctxt++;
3519 } else if (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6) {
3520 ipv6_hdr(skb)->payload_len = 0;
3521 tcp_hdr(skb)->check =
3522 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
b4617240
PW
3523 &ipv6_hdr(skb)->daddr,
3524 0, IPPROTO_TCP, 0);
9a799d71
AK
3525 adapter->hw_tso6_ctxt++;
3526 }
3527
3528 i = tx_ring->next_to_use;
3529
3530 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3531 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
3532
3533 /* VLAN MACLEN IPLEN */
3534 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3535 vlan_macip_lens |=
3536 (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
3537 vlan_macip_lens |= ((skb_network_offset(skb)) <<
b4617240 3538 IXGBE_ADVTXD_MACLEN_SHIFT);
9a799d71
AK
3539 *hdr_len += skb_network_offset(skb);
3540 vlan_macip_lens |=
3541 (skb_transport_header(skb) - skb_network_header(skb));
3542 *hdr_len +=
3543 (skb_transport_header(skb) - skb_network_header(skb));
3544 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
3545 context_desc->seqnum_seed = 0;
3546
3547 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
9f8cdf4f 3548 type_tucmd_mlhl = (IXGBE_TXD_CMD_DEXT |
b4617240 3549 IXGBE_ADVTXD_DTYP_CTXT);
9a799d71 3550
8327d000 3551 if (skb->protocol == htons(ETH_P_IP))
9a799d71
AK
3552 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
3553 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
3554 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
3555
3556 /* MSS L4LEN IDX */
9f8cdf4f 3557 mss_l4len_idx =
9a799d71
AK
3558 (skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT);
3559 mss_l4len_idx |= (l4len << IXGBE_ADVTXD_L4LEN_SHIFT);
4eeae6fd
PW
3560 /* use index 1 for TSO */
3561 mss_l4len_idx |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
9a799d71
AK
3562 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
3563
3564 tx_buffer_info->time_stamp = jiffies;
3565 tx_buffer_info->next_to_watch = i;
3566
3567 i++;
3568 if (i == tx_ring->count)
3569 i = 0;
3570 tx_ring->next_to_use = i;
3571
3572 return true;
3573 }
3574 return false;
3575}
3576
3577static bool ixgbe_tx_csum(struct ixgbe_adapter *adapter,
b4617240
PW
3578 struct ixgbe_ring *tx_ring,
3579 struct sk_buff *skb, u32 tx_flags)
9a799d71
AK
3580{
3581 struct ixgbe_adv_tx_context_desc *context_desc;
3582 unsigned int i;
3583 struct ixgbe_tx_buffer *tx_buffer_info;
3584 u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
3585
3586 if (skb->ip_summed == CHECKSUM_PARTIAL ||
3587 (tx_flags & IXGBE_TX_FLAGS_VLAN)) {
3588 i = tx_ring->next_to_use;
3589 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3590 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
3591
3592 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3593 vlan_macip_lens |=
3594 (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
3595 vlan_macip_lens |= (skb_network_offset(skb) <<
b4617240 3596 IXGBE_ADVTXD_MACLEN_SHIFT);
9a799d71
AK
3597 if (skb->ip_summed == CHECKSUM_PARTIAL)
3598 vlan_macip_lens |= (skb_transport_header(skb) -
b4617240 3599 skb_network_header(skb));
9a799d71
AK
3600
3601 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
3602 context_desc->seqnum_seed = 0;
3603
3604 type_tucmd_mlhl |= (IXGBE_TXD_CMD_DEXT |
b4617240 3605 IXGBE_ADVTXD_DTYP_CTXT);
9a799d71
AK
3606
3607 if (skb->ip_summed == CHECKSUM_PARTIAL) {
41825d71
AK
3608 switch (skb->protocol) {
3609 case __constant_htons(ETH_P_IP):
9a799d71 3610 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
41825d71
AK
3611 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
3612 type_tucmd_mlhl |=
b4617240 3613 IXGBE_ADVTXD_TUCMD_L4T_TCP;
41825d71 3614 break;
41825d71
AK
3615 case __constant_htons(ETH_P_IPV6):
3616 /* XXX what about other V6 headers?? */
3617 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
3618 type_tucmd_mlhl |=
b4617240 3619 IXGBE_ADVTXD_TUCMD_L4T_TCP;
41825d71 3620 break;
41825d71
AK
3621 default:
3622 if (unlikely(net_ratelimit())) {
3623 DPRINTK(PROBE, WARNING,
3624 "partial checksum but proto=%x!\n",
3625 skb->protocol);
3626 }
3627 break;
3628 }
9a799d71
AK
3629 }
3630
3631 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
4eeae6fd 3632 /* use index zero for tx checksum offload */
9a799d71
AK
3633 context_desc->mss_l4len_idx = 0;
3634
3635 tx_buffer_info->time_stamp = jiffies;
3636 tx_buffer_info->next_to_watch = i;
9f8cdf4f 3637
9a799d71
AK
3638 adapter->hw_csum_tx_good++;
3639 i++;
3640 if (i == tx_ring->count)
3641 i = 0;
3642 tx_ring->next_to_use = i;
3643
3644 return true;
3645 }
9f8cdf4f 3646
9a799d71
AK
3647 return false;
3648}
3649
3650static int ixgbe_tx_map(struct ixgbe_adapter *adapter,
b4617240
PW
3651 struct ixgbe_ring *tx_ring,
3652 struct sk_buff *skb, unsigned int first)
9a799d71
AK
3653{
3654 struct ixgbe_tx_buffer *tx_buffer_info;
3655 unsigned int len = skb->len;
3656 unsigned int offset = 0, size, count = 0, i;
3657 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
3658 unsigned int f;
3659
3660 len -= skb->data_len;
3661
3662 i = tx_ring->next_to_use;
3663
3664 while (len) {
3665 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3666 size = min(len, (uint)IXGBE_MAX_DATA_PER_TXD);
3667
3668 tx_buffer_info->length = size;
3669 tx_buffer_info->dma = pci_map_single(adapter->pdev,
b4617240
PW
3670 skb->data + offset,
3671 size, PCI_DMA_TODEVICE);
9a799d71
AK
3672 tx_buffer_info->time_stamp = jiffies;
3673 tx_buffer_info->next_to_watch = i;
3674
3675 len -= size;
3676 offset += size;
3677 count++;
3678 i++;
3679 if (i == tx_ring->count)
3680 i = 0;
3681 }
3682
3683 for (f = 0; f < nr_frags; f++) {
3684 struct skb_frag_struct *frag;
3685
3686 frag = &skb_shinfo(skb)->frags[f];
3687 len = frag->size;
3688 offset = frag->page_offset;
3689
3690 while (len) {
3691 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3692 size = min(len, (uint)IXGBE_MAX_DATA_PER_TXD);
3693
3694 tx_buffer_info->length = size;
3695 tx_buffer_info->dma = pci_map_page(adapter->pdev,
b4617240
PW
3696 frag->page,
3697 offset,
3698 size,
3699 PCI_DMA_TODEVICE);
9a799d71
AK
3700 tx_buffer_info->time_stamp = jiffies;
3701 tx_buffer_info->next_to_watch = i;
3702
3703 len -= size;
3704 offset += size;
3705 count++;
3706 i++;
3707 if (i == tx_ring->count)
3708 i = 0;
3709 }
3710 }
3711 if (i == 0)
3712 i = tx_ring->count - 1;
3713 else
3714 i = i - 1;
3715 tx_ring->tx_buffer_info[i].skb = skb;
3716 tx_ring->tx_buffer_info[first].next_to_watch = i;
3717
3718 return count;
3719}
3720
3721static void ixgbe_tx_queue(struct ixgbe_adapter *adapter,
b4617240
PW
3722 struct ixgbe_ring *tx_ring,
3723 int tx_flags, int count, u32 paylen, u8 hdr_len)
9a799d71
AK
3724{
3725 union ixgbe_adv_tx_desc *tx_desc = NULL;
3726 struct ixgbe_tx_buffer *tx_buffer_info;
3727 u32 olinfo_status = 0, cmd_type_len = 0;
3728 unsigned int i;
3729 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
3730
3731 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
3732
3733 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
3734
3735 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3736 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
3737
3738 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
3739 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
3740
3741 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
b4617240 3742 IXGBE_ADVTXD_POPTS_SHIFT;
9a799d71 3743
4eeae6fd
PW
3744 /* use index 1 context for tso */
3745 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
9a799d71
AK
3746 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
3747 olinfo_status |= IXGBE_TXD_POPTS_IXSM <<
b4617240 3748 IXGBE_ADVTXD_POPTS_SHIFT;
9a799d71
AK
3749
3750 } else if (tx_flags & IXGBE_TX_FLAGS_CSUM)
3751 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
b4617240 3752 IXGBE_ADVTXD_POPTS_SHIFT;
9a799d71
AK
3753
3754 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
3755
3756 i = tx_ring->next_to_use;
3757 while (count--) {
3758 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3759 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
3760 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
3761 tx_desc->read.cmd_type_len =
b4617240 3762 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
9a799d71 3763 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
9a799d71
AK
3764 i++;
3765 if (i == tx_ring->count)
3766 i = 0;
3767 }
3768
3769 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
3770
3771 /*
3772 * Force memory writes to complete before letting h/w
3773 * know there are new descriptors to fetch. (Only
3774 * applicable for weak-ordered memory model archs,
3775 * such as IA-64).
3776 */
3777 wmb();
3778
3779 tx_ring->next_to_use = i;
3780 writel(i, adapter->hw.hw_addr + tx_ring->tail);
3781}
3782
e092be60 3783static int __ixgbe_maybe_stop_tx(struct net_device *netdev,
b4617240 3784 struct ixgbe_ring *tx_ring, int size)
e092be60
AV
3785{
3786 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3787
30eba97a 3788 netif_stop_subqueue(netdev, tx_ring->queue_index);
e092be60
AV
3789 /* Herbert's original patch had:
3790 * smp_mb__after_netif_stop_queue();
3791 * but since that doesn't exist yet, just open code it. */
3792 smp_mb();
3793
3794 /* We need to check again in a case another CPU has just
3795 * made room available. */
3796 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
3797 return -EBUSY;
3798
3799 /* A reprieve! - use start_queue because it doesn't call schedule */
af72166f 3800 netif_start_subqueue(netdev, tx_ring->queue_index);
e092be60
AV
3801 ++adapter->restart_queue;
3802 return 0;
3803}
3804
3805static int ixgbe_maybe_stop_tx(struct net_device *netdev,
b4617240 3806 struct ixgbe_ring *tx_ring, int size)
e092be60
AV
3807{
3808 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
3809 return 0;
3810 return __ixgbe_maybe_stop_tx(netdev, tx_ring, size);
3811}
3812
9a799d71
AK
3813static int ixgbe_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3814{
3815 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3816 struct ixgbe_ring *tx_ring;
9a799d71
AK
3817 unsigned int first;
3818 unsigned int tx_flags = 0;
30eba97a
AV
3819 u8 hdr_len = 0;
3820 int r_idx = 0, tso;
9a799d71
AK
3821 int count = 0;
3822 unsigned int f;
9f8cdf4f 3823
30eba97a 3824 r_idx = (adapter->num_tx_queues - 1) & skb->queue_mapping;
30eba97a 3825 tx_ring = &adapter->tx_ring[r_idx];
9a799d71 3826
9f8cdf4f
JB
3827 if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
3828 tx_flags |= vlan_tx_tag_get(skb);
2f90b865
AD
3829 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
3830 tx_flags &= ~IXGBE_TX_FLAGS_VLAN_PRIO_MASK;
3831 tx_flags |= (skb->queue_mapping << 13);
3832 }
3833 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
3834 tx_flags |= IXGBE_TX_FLAGS_VLAN;
3835 } else if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
3836 tx_flags |= (skb->queue_mapping << 13);
9f8cdf4f
JB
3837 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
3838 tx_flags |= IXGBE_TX_FLAGS_VLAN;
9a799d71 3839 }
9f8cdf4f
JB
3840 /* three things can cause us to need a context descriptor */
3841 if (skb_is_gso(skb) ||
3842 (skb->ip_summed == CHECKSUM_PARTIAL) ||
3843 (tx_flags & IXGBE_TX_FLAGS_VLAN))
9a799d71
AK
3844 count++;
3845
9f8cdf4f
JB
3846 count += TXD_USE_COUNT(skb_headlen(skb));
3847 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
9a799d71
AK
3848 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
3849
e092be60 3850 if (ixgbe_maybe_stop_tx(netdev, tx_ring, count)) {
9a799d71 3851 adapter->tx_busy++;
9a799d71
AK
3852 return NETDEV_TX_BUSY;
3853 }
9a799d71 3854
8327d000 3855 if (skb->protocol == htons(ETH_P_IP))
9a799d71
AK
3856 tx_flags |= IXGBE_TX_FLAGS_IPV4;
3857 first = tx_ring->next_to_use;
3858 tso = ixgbe_tso(adapter, tx_ring, skb, tx_flags, &hdr_len);
3859 if (tso < 0) {
3860 dev_kfree_skb_any(skb);
3861 return NETDEV_TX_OK;
3862 }
3863
3864 if (tso)
3865 tx_flags |= IXGBE_TX_FLAGS_TSO;
3866 else if (ixgbe_tx_csum(adapter, tx_ring, skb, tx_flags) &&
b4617240 3867 (skb->ip_summed == CHECKSUM_PARTIAL))
9a799d71
AK
3868 tx_flags |= IXGBE_TX_FLAGS_CSUM;
3869
3870 ixgbe_tx_queue(adapter, tx_ring, tx_flags,
b4617240
PW
3871 ixgbe_tx_map(adapter, tx_ring, skb, first),
3872 skb->len, hdr_len);
9a799d71
AK
3873
3874 netdev->trans_start = jiffies;
3875
e092be60 3876 ixgbe_maybe_stop_tx(netdev, tx_ring, DESC_NEEDED);
9a799d71
AK
3877
3878 return NETDEV_TX_OK;
3879}
3880
3881/**
3882 * ixgbe_get_stats - Get System Network Statistics
3883 * @netdev: network interface device structure
3884 *
3885 * Returns the address of the device statistics structure.
3886 * The statistics are actually updated from the timer callback.
3887 **/
3888static struct net_device_stats *ixgbe_get_stats(struct net_device *netdev)
3889{
3890 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3891
3892 /* only return the current stats */
3893 return &adapter->net_stats;
3894}
3895
3896/**
3897 * ixgbe_set_mac - Change the Ethernet Address of the NIC
3898 * @netdev: network interface device structure
3899 * @p: pointer to an address structure
3900 *
3901 * Returns 0 on success, negative on failure
3902 **/
3903static int ixgbe_set_mac(struct net_device *netdev, void *p)
3904{
3905 struct ixgbe_adapter *adapter = netdev_priv(netdev);
b4617240 3906 struct ixgbe_hw *hw = &adapter->hw;
9a799d71
AK
3907 struct sockaddr *addr = p;
3908
3909 if (!is_valid_ether_addr(addr->sa_data))
3910 return -EADDRNOTAVAIL;
3911
3912 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
b4617240 3913 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
9a799d71 3914
b4617240 3915 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
9a799d71
AK
3916
3917 return 0;
3918}
3919
3920#ifdef CONFIG_NET_POLL_CONTROLLER
3921/*
3922 * Polling 'interrupt' - used by things like netconsole to send skbs
3923 * without having to re-enable interrupts. It's not called while
3924 * the interrupt routine is executing.
3925 */
3926static void ixgbe_netpoll(struct net_device *netdev)
3927{
3928 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3929
3930 disable_irq(adapter->pdev->irq);
3931 adapter->flags |= IXGBE_FLAG_IN_NETPOLL;
3932 ixgbe_intr(adapter->pdev->irq, netdev);
3933 adapter->flags &= ~IXGBE_FLAG_IN_NETPOLL;
3934 enable_irq(adapter->pdev->irq);
3935}
3936#endif
3937
c44ade9e
JB
3938/**
3939 * ixgbe_link_config - set up initial link with default speed and duplex
3940 * @hw: pointer to private hardware struct
3941 *
3942 * Returns 0 on success, negative on failure
3943 **/
3944static int ixgbe_link_config(struct ixgbe_hw *hw)
3945{
3946 u32 autoneg = IXGBE_LINK_SPEED_10GB_FULL;
3947
3948 /* must always autoneg for both 1G and 10G link */
3949 hw->mac.autoneg = true;
3950
0befdb3e
JB
3951 if ((hw->mac.type == ixgbe_mac_82598EB) &&
3952 (hw->phy.media_type == ixgbe_media_type_copper))
3953 autoneg = IXGBE_LINK_SPEED_82598_AUTONEG;
3954
c44ade9e
JB
3955 return hw->mac.ops.setup_link_speed(hw, autoneg, true, true);
3956}
3957
0edc3527
SH
3958static const struct net_device_ops ixgbe_netdev_ops = {
3959 .ndo_open = ixgbe_open,
3960 .ndo_stop = ixgbe_close,
00829823 3961 .ndo_start_xmit = ixgbe_xmit_frame,
0edc3527
SH
3962 .ndo_get_stats = ixgbe_get_stats,
3963 .ndo_set_multicast_list = ixgbe_set_rx_mode,
3964 .ndo_validate_addr = eth_validate_addr,
3965 .ndo_set_mac_address = ixgbe_set_mac,
3966 .ndo_change_mtu = ixgbe_change_mtu,
3967 .ndo_tx_timeout = ixgbe_tx_timeout,
3968 .ndo_vlan_rx_register = ixgbe_vlan_rx_register,
3969 .ndo_vlan_rx_add_vid = ixgbe_vlan_rx_add_vid,
3970 .ndo_vlan_rx_kill_vid = ixgbe_vlan_rx_kill_vid,
3971#ifdef CONFIG_NET_POLL_CONTROLLER
3972 .ndo_poll_controller = ixgbe_netpoll,
3973#endif
3974};
3975
9a799d71
AK
3976/**
3977 * ixgbe_probe - Device Initialization Routine
3978 * @pdev: PCI device information struct
3979 * @ent: entry in ixgbe_pci_tbl
3980 *
3981 * Returns 0 on success, negative on failure
3982 *
3983 * ixgbe_probe initializes an adapter identified by a pci_dev structure.
3984 * The OS initialization, configuring of the adapter private structure,
3985 * and a hardware reset occur.
3986 **/
3987static int __devinit ixgbe_probe(struct pci_dev *pdev,
b4617240 3988 const struct pci_device_id *ent)
9a799d71
AK
3989{
3990 struct net_device *netdev;
3991 struct ixgbe_adapter *adapter = NULL;
3992 struct ixgbe_hw *hw;
3993 const struct ixgbe_info *ii = ixgbe_info_tbl[ent->driver_data];
9a799d71
AK
3994 static int cards_found;
3995 int i, err, pci_using_dac;
3996 u16 link_status, link_speed, link_width;
c44ade9e 3997 u32 part_num, eec;
9a799d71
AK
3998
3999 err = pci_enable_device(pdev);
4000 if (err)
4001 return err;
4002
4003 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK) &&
4004 !pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK)) {
4005 pci_using_dac = 1;
4006 } else {
4007 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
4008 if (err) {
4009 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
4010 if (err) {
b4617240
PW
4011 dev_err(&pdev->dev, "No usable DMA "
4012 "configuration, aborting\n");
9a799d71
AK
4013 goto err_dma;
4014 }
4015 }
4016 pci_using_dac = 0;
4017 }
4018
4019 err = pci_request_regions(pdev, ixgbe_driver_name);
4020 if (err) {
4021 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
4022 goto err_pci_reg;
4023 }
4024
4025 pci_set_master(pdev);
fb3b27bc 4026 pci_save_state(pdev);
9a799d71 4027
30eba97a 4028 netdev = alloc_etherdev_mq(sizeof(struct ixgbe_adapter), MAX_TX_QUEUES);
9a799d71
AK
4029 if (!netdev) {
4030 err = -ENOMEM;
4031 goto err_alloc_etherdev;
4032 }
4033
9a799d71
AK
4034 SET_NETDEV_DEV(netdev, &pdev->dev);
4035
4036 pci_set_drvdata(pdev, netdev);
4037 adapter = netdev_priv(netdev);
4038
4039 adapter->netdev = netdev;
4040 adapter->pdev = pdev;
4041 hw = &adapter->hw;
4042 hw->back = adapter;
4043 adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
4044
05857980
JK
4045 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
4046 pci_resource_len(pdev, 0));
9a799d71
AK
4047 if (!hw->hw_addr) {
4048 err = -EIO;
4049 goto err_ioremap;
4050 }
4051
4052 for (i = 1; i <= 5; i++) {
4053 if (pci_resource_len(pdev, i) == 0)
4054 continue;
4055 }
4056
0edc3527 4057 netdev->netdev_ops = &ixgbe_netdev_ops;
9a799d71 4058 ixgbe_set_ethtool_ops(netdev);
9a799d71 4059 netdev->watchdog_timeo = 5 * HZ;
9a799d71
AK
4060 strcpy(netdev->name, pci_name(pdev));
4061
9a799d71
AK
4062 adapter->bd_number = cards_found;
4063
9a799d71
AK
4064 /* Setup hw api */
4065 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
021230d4 4066 hw->mac.type = ii->mac;
9a799d71 4067
c44ade9e
JB
4068 /* EEPROM */
4069 memcpy(&hw->eeprom.ops, ii->eeprom_ops, sizeof(hw->eeprom.ops));
4070 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
4071 /* If EEPROM is valid (bit 8 = 1), use default otherwise use bit bang */
4072 if (!(eec & (1 << 8)))
4073 hw->eeprom.ops.read = &ixgbe_read_eeprom_bit_bang_generic;
4074
4075 /* PHY */
4076 memcpy(&hw->phy.ops, ii->phy_ops, sizeof(hw->phy.ops));
c4900be0
DS
4077 hw->phy.sfp_type = ixgbe_sfp_type_unknown;
4078
4079 /* set up this timer and work struct before calling get_invariants
4080 * which might start the timer
4081 */
4082 init_timer(&adapter->sfp_timer);
4083 adapter->sfp_timer.function = &ixgbe_sfp_timer;
4084 adapter->sfp_timer.data = (unsigned long) adapter;
4085
4086 INIT_WORK(&adapter->sfp_task, ixgbe_sfp_task);
c44ade9e 4087
9a799d71 4088 err = ii->get_invariants(hw);
c4900be0
DS
4089 if (err == IXGBE_ERR_SFP_NOT_PRESENT) {
4090 /* start a kernel thread to watch for a module to arrive */
4091 set_bit(__IXGBE_SFP_MODULE_NOT_FOUND, &adapter->state);
4092 mod_timer(&adapter->sfp_timer,
4093 round_jiffies(jiffies + (2 * HZ)));
4094 err = 0;
4095 } else if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
4096 DPRINTK(PROBE, ERR, "failed to load because an "
4097 "unsupported SFP+ module type was detected.\n");
9a799d71 4098 goto err_hw_init;
c4900be0
DS
4099 } else if (err) {
4100 goto err_hw_init;
4101 }
9a799d71
AK
4102
4103 /* setup the private structure */
4104 err = ixgbe_sw_init(adapter);
4105 if (err)
4106 goto err_sw_init;
4107
c44ade9e
JB
4108 /* reset_hw fills in the perm_addr as well */
4109 err = hw->mac.ops.reset_hw(hw);
4110 if (err) {
4111 dev_err(&adapter->pdev->dev, "HW Init failed: %d\n", err);
4112 goto err_sw_init;
4113 }
4114
9a799d71 4115 netdev->features = NETIF_F_SG |
b4617240
PW
4116 NETIF_F_IP_CSUM |
4117 NETIF_F_HW_VLAN_TX |
4118 NETIF_F_HW_VLAN_RX |
4119 NETIF_F_HW_VLAN_FILTER;
9a799d71 4120
e9990a9c 4121 netdev->features |= NETIF_F_IPV6_CSUM;
9a799d71 4122 netdev->features |= NETIF_F_TSO;
9a799d71 4123 netdev->features |= NETIF_F_TSO6;
e9990a9c 4124 netdev->features |= NETIF_F_LRO;
ad31c402
JK
4125
4126 netdev->vlan_features |= NETIF_F_TSO;
4127 netdev->vlan_features |= NETIF_F_TSO6;
22f32b7a 4128 netdev->vlan_features |= NETIF_F_IP_CSUM;
ad31c402
JK
4129 netdev->vlan_features |= NETIF_F_SG;
4130
2f90b865
AD
4131 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED)
4132 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
4133
7a6b6f51 4134#ifdef CONFIG_IXGBE_DCB
2f90b865
AD
4135 netdev->dcbnl_ops = &dcbnl_ops;
4136#endif
4137
9a799d71
AK
4138 if (pci_using_dac)
4139 netdev->features |= NETIF_F_HIGHDMA;
4140
9a799d71 4141 /* make sure the EEPROM is good */
c44ade9e 4142 if (hw->eeprom.ops.validate_checksum(hw, NULL) < 0) {
9a799d71
AK
4143 dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n");
4144 err = -EIO;
4145 goto err_eeprom;
4146 }
4147
4148 memcpy(netdev->dev_addr, hw->mac.perm_addr, netdev->addr_len);
4149 memcpy(netdev->perm_addr, hw->mac.perm_addr, netdev->addr_len);
4150
c44ade9e
JB
4151 if (ixgbe_validate_mac_addr(netdev->perm_addr)) {
4152 dev_err(&pdev->dev, "invalid MAC address\n");
9a799d71
AK
4153 err = -EIO;
4154 goto err_eeprom;
4155 }
4156
4157 init_timer(&adapter->watchdog_timer);
4158 adapter->watchdog_timer.function = &ixgbe_watchdog;
4159 adapter->watchdog_timer.data = (unsigned long)adapter;
4160
4161 INIT_WORK(&adapter->reset_task, ixgbe_reset_task);
cf8280ee 4162 INIT_WORK(&adapter->watchdog_task, ixgbe_watchdog_task);
9a799d71 4163
021230d4
AV
4164 err = ixgbe_init_interrupt_scheme(adapter);
4165 if (err)
4166 goto err_sw_init;
9a799d71
AK
4167
4168 /* print bus type/speed/width info */
4169 pci_read_config_word(pdev, IXGBE_PCI_LINK_STATUS, &link_status);
4170 link_speed = link_status & IXGBE_PCI_LINK_SPEED;
4171 link_width = link_status & IXGBE_PCI_LINK_WIDTH;
7c510e4b 4172 dev_info(&pdev->dev, "(PCI Express:%s:%s) %pM\n",
b4617240
PW
4173 ((link_speed == IXGBE_PCI_LINK_SPEED_5000) ? "5.0Gb/s" :
4174 (link_speed == IXGBE_PCI_LINK_SPEED_2500) ? "2.5Gb/s" :
4175 "Unknown"),
4176 ((link_width == IXGBE_PCI_LINK_WIDTH_8) ? "Width x8" :
4177 (link_width == IXGBE_PCI_LINK_WIDTH_4) ? "Width x4" :
4178 (link_width == IXGBE_PCI_LINK_WIDTH_2) ? "Width x2" :
4179 (link_width == IXGBE_PCI_LINK_WIDTH_1) ? "Width x1" :
4180 "Unknown"),
7c510e4b 4181 netdev->dev_addr);
c44ade9e 4182 ixgbe_read_pba_num_generic(hw, &part_num);
9a799d71 4183 dev_info(&pdev->dev, "MAC: %d, PHY: %d, PBA No: %06x-%03x\n",
b4617240
PW
4184 hw->mac.type, hw->phy.type,
4185 (part_num >> 8), (part_num & 0xff));
9a799d71 4186
0c254d86
AK
4187 if (link_width <= IXGBE_PCI_LINK_WIDTH_4) {
4188 dev_warn(&pdev->dev, "PCI-Express bandwidth available for "
b4617240
PW
4189 "this card is not sufficient for optimal "
4190 "performance.\n");
0c254d86 4191 dev_warn(&pdev->dev, "For optimal performance a x8 "
b4617240 4192 "PCI-Express slot is required.\n");
0c254d86
AK
4193 }
4194
9a799d71 4195 /* reset the hardware with the new settings */
c44ade9e
JB
4196 hw->mac.ops.start_hw(hw);
4197
4198 /* link_config depends on start_hw being called at least once */
4199 err = ixgbe_link_config(hw);
4200 if (err) {
4201 dev_err(&pdev->dev, "setup_link_speed FAILED %d\n", err);
4202 goto err_register;
4203 }
9a799d71
AK
4204
4205 netif_carrier_off(netdev);
2f90b865 4206 netif_tx_stop_all_queues(netdev);
9a799d71 4207
021230d4
AV
4208 ixgbe_napi_add_all(adapter);
4209
9a799d71
AK
4210 strcpy(netdev->name, "eth%d");
4211 err = register_netdev(netdev);
4212 if (err)
4213 goto err_register;
4214
5dd2d332 4215#ifdef CONFIG_IXGBE_DCA
652f093f 4216 if (dca_add_requester(&pdev->dev) == 0) {
bd0362dd
JC
4217 adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
4218 /* always use CB2 mode, difference is masked
4219 * in the CB driver */
4220 IXGBE_WRITE_REG(hw, IXGBE_DCA_CTRL, 2);
4221 ixgbe_setup_dca(adapter);
4222 }
4223#endif
9a799d71
AK
4224
4225 dev_info(&pdev->dev, "Intel(R) 10 Gigabit Network Connection\n");
4226 cards_found++;
4227 return 0;
4228
4229err_register:
5eba3699 4230 ixgbe_release_hw_control(adapter);
9a799d71
AK
4231err_hw_init:
4232err_sw_init:
021230d4 4233 ixgbe_reset_interrupt_capability(adapter);
9a799d71 4234err_eeprom:
c4900be0
DS
4235 clear_bit(__IXGBE_SFP_MODULE_NOT_FOUND, &adapter->state);
4236 del_timer_sync(&adapter->sfp_timer);
4237 cancel_work_sync(&adapter->sfp_task);
9a799d71
AK
4238 iounmap(hw->hw_addr);
4239err_ioremap:
4240 free_netdev(netdev);
4241err_alloc_etherdev:
4242 pci_release_regions(pdev);
4243err_pci_reg:
4244err_dma:
4245 pci_disable_device(pdev);
4246 return err;
4247}
4248
4249/**
4250 * ixgbe_remove - Device Removal Routine
4251 * @pdev: PCI device information struct
4252 *
4253 * ixgbe_remove is called by the PCI subsystem to alert the driver
4254 * that it should release a PCI device. The could be caused by a
4255 * Hot-Plug event, or because the driver is going to be removed from
4256 * memory.
4257 **/
4258static void __devexit ixgbe_remove(struct pci_dev *pdev)
4259{
4260 struct net_device *netdev = pci_get_drvdata(pdev);
4261 struct ixgbe_adapter *adapter = netdev_priv(netdev);
4262
4263 set_bit(__IXGBE_DOWN, &adapter->state);
c4900be0
DS
4264 /* clear the module not found bit to make sure the worker won't
4265 * reschedule
4266 */
4267 clear_bit(__IXGBE_SFP_MODULE_NOT_FOUND, &adapter->state);
9a799d71
AK
4268 del_timer_sync(&adapter->watchdog_timer);
4269
c4900be0
DS
4270 del_timer_sync(&adapter->sfp_timer);
4271 cancel_work_sync(&adapter->watchdog_task);
4272 cancel_work_sync(&adapter->sfp_task);
9a799d71
AK
4273 flush_scheduled_work();
4274
5dd2d332 4275#ifdef CONFIG_IXGBE_DCA
bd0362dd
JC
4276 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
4277 adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
4278 dca_remove_requester(&pdev->dev);
4279 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 1);
4280 }
4281
4282#endif
c4900be0
DS
4283 if (netdev->reg_state == NETREG_REGISTERED)
4284 unregister_netdev(netdev);
9a799d71 4285
021230d4 4286 ixgbe_reset_interrupt_capability(adapter);
5eba3699 4287
021230d4 4288 ixgbe_release_hw_control(adapter);
9a799d71
AK
4289
4290 iounmap(adapter->hw.hw_addr);
4291 pci_release_regions(pdev);
4292
021230d4 4293 DPRINTK(PROBE, INFO, "complete\n");
b3c8b4ba 4294 ixgbe_napi_del_all(adapter);
021230d4
AV
4295 kfree(adapter->tx_ring);
4296 kfree(adapter->rx_ring);
4297
9a799d71
AK
4298 free_netdev(netdev);
4299
4300 pci_disable_device(pdev);
4301}
4302
4303/**
4304 * ixgbe_io_error_detected - called when PCI error is detected
4305 * @pdev: Pointer to PCI device
4306 * @state: The current pci connection state
4307 *
4308 * This function is called after a PCI bus error affecting
4309 * this device has been detected.
4310 */
4311static pci_ers_result_t ixgbe_io_error_detected(struct pci_dev *pdev,
b4617240 4312 pci_channel_state_t state)
9a799d71
AK
4313{
4314 struct net_device *netdev = pci_get_drvdata(pdev);
454d7c9b 4315 struct ixgbe_adapter *adapter = netdev_priv(netdev);
9a799d71
AK
4316
4317 netif_device_detach(netdev);
4318
4319 if (netif_running(netdev))
4320 ixgbe_down(adapter);
4321 pci_disable_device(pdev);
4322
b4617240 4323 /* Request a slot reset. */
9a799d71
AK
4324 return PCI_ERS_RESULT_NEED_RESET;
4325}
4326
4327/**
4328 * ixgbe_io_slot_reset - called after the pci bus has been reset.
4329 * @pdev: Pointer to PCI device
4330 *
4331 * Restart the card from scratch, as if from a cold-boot.
4332 */
4333static pci_ers_result_t ixgbe_io_slot_reset(struct pci_dev *pdev)
4334{
4335 struct net_device *netdev = pci_get_drvdata(pdev);
454d7c9b 4336 struct ixgbe_adapter *adapter = netdev_priv(netdev);
9a799d71
AK
4337
4338 if (pci_enable_device(pdev)) {
4339 DPRINTK(PROBE, ERR,
b4617240 4340 "Cannot re-enable PCI device after reset.\n");
9a799d71
AK
4341 return PCI_ERS_RESULT_DISCONNECT;
4342 }
4343 pci_set_master(pdev);
fb3b27bc 4344 pci_restore_state(pdev);
9a799d71
AK
4345
4346 pci_enable_wake(pdev, PCI_D3hot, 0);
4347 pci_enable_wake(pdev, PCI_D3cold, 0);
4348
4349 ixgbe_reset(adapter);
4350
4351 return PCI_ERS_RESULT_RECOVERED;
4352}
4353
4354/**
4355 * ixgbe_io_resume - called when traffic can start flowing again.
4356 * @pdev: Pointer to PCI device
4357 *
4358 * This callback is called when the error recovery driver tells us that
4359 * its OK to resume normal operation.
4360 */
4361static void ixgbe_io_resume(struct pci_dev *pdev)
4362{
4363 struct net_device *netdev = pci_get_drvdata(pdev);
454d7c9b 4364 struct ixgbe_adapter *adapter = netdev_priv(netdev);
9a799d71
AK
4365
4366 if (netif_running(netdev)) {
4367 if (ixgbe_up(adapter)) {
4368 DPRINTK(PROBE, INFO, "ixgbe_up failed after reset\n");
4369 return;
4370 }
4371 }
4372
4373 netif_device_attach(netdev);
9a799d71
AK
4374}
4375
4376static struct pci_error_handlers ixgbe_err_handler = {
4377 .error_detected = ixgbe_io_error_detected,
4378 .slot_reset = ixgbe_io_slot_reset,
4379 .resume = ixgbe_io_resume,
4380};
4381
4382static struct pci_driver ixgbe_driver = {
4383 .name = ixgbe_driver_name,
4384 .id_table = ixgbe_pci_tbl,
4385 .probe = ixgbe_probe,
4386 .remove = __devexit_p(ixgbe_remove),
4387#ifdef CONFIG_PM
4388 .suspend = ixgbe_suspend,
4389 .resume = ixgbe_resume,
4390#endif
4391 .shutdown = ixgbe_shutdown,
4392 .err_handler = &ixgbe_err_handler
4393};
4394
4395/**
4396 * ixgbe_init_module - Driver Registration Routine
4397 *
4398 * ixgbe_init_module is the first routine called when the driver is
4399 * loaded. All it does is register with the PCI subsystem.
4400 **/
4401static int __init ixgbe_init_module(void)
4402{
4403 int ret;
4404 printk(KERN_INFO "%s: %s - version %s\n", ixgbe_driver_name,
4405 ixgbe_driver_string, ixgbe_driver_version);
4406
4407 printk(KERN_INFO "%s: %s\n", ixgbe_driver_name, ixgbe_copyright);
4408
5dd2d332 4409#ifdef CONFIG_IXGBE_DCA
bd0362dd 4410 dca_register_notify(&dca_notifier);
bd0362dd 4411#endif
5dd2d332 4412
9a799d71
AK
4413 ret = pci_register_driver(&ixgbe_driver);
4414 return ret;
4415}
b4617240 4416
9a799d71
AK
4417module_init(ixgbe_init_module);
4418
4419/**
4420 * ixgbe_exit_module - Driver Exit Cleanup Routine
4421 *
4422 * ixgbe_exit_module is called just before the driver is removed
4423 * from memory.
4424 **/
4425static void __exit ixgbe_exit_module(void)
4426{
5dd2d332 4427#ifdef CONFIG_IXGBE_DCA
bd0362dd
JC
4428 dca_unregister_notify(&dca_notifier);
4429#endif
9a799d71
AK
4430 pci_unregister_driver(&ixgbe_driver);
4431}
bd0362dd 4432
5dd2d332 4433#ifdef CONFIG_IXGBE_DCA
bd0362dd 4434static int ixgbe_notify_dca(struct notifier_block *nb, unsigned long event,
b4617240 4435 void *p)
bd0362dd
JC
4436{
4437 int ret_val;
4438
4439 ret_val = driver_for_each_device(&ixgbe_driver.driver, NULL, &event,
b4617240 4440 __ixgbe_notify_dca);
bd0362dd
JC
4441
4442 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
4443}
5dd2d332 4444#endif /* CONFIG_IXGBE_DCA */
bd0362dd 4445
9a799d71
AK
4446module_exit(ixgbe_exit_module);
4447
4448/* ixgbe_main.c */