blob: fecc8ea79e9d683e8a01ba55b3295b005742df29 [file] [log] [blame]
Auke Kok9a799d72007-09-15 14:07:45 -07001/*******************************************************************************
2
3 Intel 10 Gigabit PCI Express Linux driver
4 Copyright(c) 1999 - 2007 Intel Corporation.
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:
23 Linux NICS <linux.nics@intel.com>
24 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27*******************************************************************************/
28
29#include <linux/types.h>
30#include <linux/module.h>
31#include <linux/pci.h>
32#include <linux/netdevice.h>
33#include <linux/vmalloc.h>
34#include <linux/string.h>
35#include <linux/in.h>
36#include <linux/ip.h>
37#include <linux/tcp.h>
38#include <linux/ipv6.h>
39#include <net/checksum.h>
40#include <net/ip6_checksum.h>
41#include <linux/ethtool.h>
42#include <linux/if_vlan.h>
43
44#include "ixgbe.h"
45#include "ixgbe_common.h"
46
47char ixgbe_driver_name[] = "ixgbe";
Stephen Hemminger9c8eb722007-10-29 10:46:24 -070048static const char ixgbe_driver_string[] =
49 "Intel(R) 10 Gigabit PCI Express Network Driver";
Auke Kok9a799d72007-09-15 14:07:45 -070050
51#define DRV_VERSION "1.1.18"
Stephen Hemminger9c8eb722007-10-29 10:46:24 -070052const char ixgbe_driver_version[] = DRV_VERSION;
53static const char ixgbe_copyright[] =
54 "Copyright (c) 1999-2007 Intel Corporation.";
Auke Kok9a799d72007-09-15 14:07:45 -070055
56static const struct ixgbe_info *ixgbe_info_tbl[] = {
Auke Kok3957d632007-10-31 15:22:10 -070057 [board_82598] = &ixgbe_82598_info,
Auke Kok9a799d72007-09-15 14:07:45 -070058};
59
60/* ixgbe_pci_tbl - PCI Device ID Table
61 *
62 * Wildcard entries (PCI_ANY_ID) should come last
63 * Last entry must be all 0s
64 *
65 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
66 * Class, Class Mask, private data (not used) }
67 */
68static struct pci_device_id ixgbe_pci_tbl[] = {
69 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_DUAL_PORT),
Auke Kok3957d632007-10-31 15:22:10 -070070 board_82598 },
Auke Kok9a799d72007-09-15 14:07:45 -070071 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_SINGLE_PORT),
Auke Kok3957d632007-10-31 15:22:10 -070072 board_82598 },
Auke Kok9a799d72007-09-15 14:07:45 -070073 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AT_DUAL_PORT),
Auke Kok3957d632007-10-31 15:22:10 -070074 board_82598 },
Auke Kok9a799d72007-09-15 14:07:45 -070075 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_CX4),
Auke Kok3957d632007-10-31 15:22:10 -070076 board_82598 },
Auke Kok9a799d72007-09-15 14:07:45 -070077
78 /* required last entry */
79 {0, }
80};
81MODULE_DEVICE_TABLE(pci, ixgbe_pci_tbl);
82
83MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
84MODULE_DESCRIPTION("Intel(R) 10 Gigabit PCI Express Network Driver");
85MODULE_LICENSE("GPL");
86MODULE_VERSION(DRV_VERSION);
87
88#define DEFAULT_DEBUG_LEVEL_SHIFT 3
89
Ayyappan Veeraiyan5eba3692008-02-01 15:59:04 -080090static void ixgbe_release_hw_control(struct ixgbe_adapter *adapter)
91{
92 u32 ctrl_ext;
93
94 /* Let firmware take over control of h/w */
95 ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
96 IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
97 ctrl_ext & ~IXGBE_CTRL_EXT_DRV_LOAD);
98}
99
100static void ixgbe_get_hw_control(struct ixgbe_adapter *adapter)
101{
102 u32 ctrl_ext;
103
104 /* Let firmware know the driver has taken over */
105 ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
106 IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
107 ctrl_ext | IXGBE_CTRL_EXT_DRV_LOAD);
108}
Auke Kok9a799d72007-09-15 14:07:45 -0700109
110#ifdef DEBUG
111/**
112 * ixgbe_get_hw_dev_name - return device name string
113 * used by hardware layer to print debugging information
114 **/
115char *ixgbe_get_hw_dev_name(struct ixgbe_hw *hw)
116{
117 struct ixgbe_adapter *adapter = hw->back;
118 struct net_device *netdev = adapter->netdev;
119 return netdev->name;
120}
121#endif
122
123static void ixgbe_set_ivar(struct ixgbe_adapter *adapter, u16 int_alloc_entry,
124 u8 msix_vector)
125{
126 u32 ivar, index;
127
128 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
129 index = (int_alloc_entry >> 2) & 0x1F;
130 ivar = IXGBE_READ_REG(&adapter->hw, IXGBE_IVAR(index));
131 ivar &= ~(0xFF << (8 * (int_alloc_entry & 0x3)));
132 ivar |= (msix_vector << (8 * (int_alloc_entry & 0x3)));
133 IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR(index), ivar);
134}
135
136static void ixgbe_unmap_and_free_tx_resource(struct ixgbe_adapter *adapter,
137 struct ixgbe_tx_buffer
138 *tx_buffer_info)
139{
140 if (tx_buffer_info->dma) {
141 pci_unmap_page(adapter->pdev,
142 tx_buffer_info->dma,
143 tx_buffer_info->length, PCI_DMA_TODEVICE);
144 tx_buffer_info->dma = 0;
145 }
146 if (tx_buffer_info->skb) {
147 dev_kfree_skb_any(tx_buffer_info->skb);
148 tx_buffer_info->skb = NULL;
149 }
150 /* tx_buffer_info must be completely set up in the transmit path */
151}
152
153static inline bool ixgbe_check_tx_hang(struct ixgbe_adapter *adapter,
154 struct ixgbe_ring *tx_ring,
155 unsigned int eop,
156 union ixgbe_adv_tx_desc *eop_desc)
157{
158 /* Detect a transmit hang in hardware, this serializes the
159 * check with the clearing of time_stamp and movement of i */
160 adapter->detect_tx_hung = false;
161 if (tx_ring->tx_buffer_info[eop].dma &&
162 time_after(jiffies, tx_ring->tx_buffer_info[eop].time_stamp + HZ) &&
163 !(IXGBE_READ_REG(&adapter->hw, IXGBE_TFCS) & IXGBE_TFCS_TXOFF)) {
164 /* detected Tx unit hang */
165 DPRINTK(DRV, ERR, "Detected Tx Unit Hang\n"
166 " TDH <%x>\n"
167 " TDT <%x>\n"
168 " next_to_use <%x>\n"
169 " next_to_clean <%x>\n"
170 "tx_buffer_info[next_to_clean]\n"
171 " time_stamp <%lx>\n"
172 " next_to_watch <%x>\n"
173 " jiffies <%lx>\n"
174 " next_to_watch.status <%x>\n",
175 readl(adapter->hw.hw_addr + tx_ring->head),
176 readl(adapter->hw.hw_addr + tx_ring->tail),
177 tx_ring->next_to_use,
178 tx_ring->next_to_clean,
179 tx_ring->tx_buffer_info[eop].time_stamp,
180 eop, jiffies, eop_desc->wb.status);
181 return true;
182 }
183
184 return false;
185}
186
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800187#define IXGBE_MAX_TXD_PWR 14
188#define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
189
190/* Tx Descriptors needed, worst case */
191#define TXD_USE_COUNT(S) (((S) >> IXGBE_MAX_TXD_PWR) + \
192 (((S) & (IXGBE_MAX_DATA_PER_TXD - 1)) ? 1 : 0))
193#define DESC_NEEDED (TXD_USE_COUNT(IXGBE_MAX_DATA_PER_TXD) /* skb->data */ + \
194 MAX_SKB_FRAGS * TXD_USE_COUNT(PAGE_SIZE) + 1) /* for context */
195
Auke Kok9a799d72007-09-15 14:07:45 -0700196/**
197 * ixgbe_clean_tx_irq - Reclaim resources after transmit completes
198 * @adapter: board private structure
199 **/
200static bool ixgbe_clean_tx_irq(struct ixgbe_adapter *adapter,
201 struct ixgbe_ring *tx_ring)
202{
203 struct net_device *netdev = adapter->netdev;
204 union ixgbe_adv_tx_desc *tx_desc, *eop_desc;
205 struct ixgbe_tx_buffer *tx_buffer_info;
206 unsigned int i, eop;
207 bool cleaned = false;
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800208 unsigned int total_tx_bytes = 0, total_tx_packets = 0;
Auke Kok9a799d72007-09-15 14:07:45 -0700209
210 i = tx_ring->next_to_clean;
211 eop = tx_ring->tx_buffer_info[i].next_to_watch;
212 eop_desc = IXGBE_TX_DESC_ADV(*tx_ring, eop);
213 while (eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)) {
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800214 cleaned = false;
215 while (!cleaned) {
Auke Kok9a799d72007-09-15 14:07:45 -0700216 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
217 tx_buffer_info = &tx_ring->tx_buffer_info[i];
218 cleaned = (i == eop);
219
220 tx_ring->stats.bytes += tx_buffer_info->length;
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800221 if (cleaned) {
222 struct sk_buff *skb = tx_buffer_info->skb;
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800223 unsigned int segs, bytecount;
224 segs = skb_shinfo(skb)->gso_segs ?: 1;
225 /* multiply data chunks by size of headers */
226 bytecount = ((segs - 1) * skb_headlen(skb)) +
227 skb->len;
228 total_tx_packets += segs;
229 total_tx_bytes += bytecount;
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800230 }
Auke Kok9a799d72007-09-15 14:07:45 -0700231 ixgbe_unmap_and_free_tx_resource(adapter,
232 tx_buffer_info);
233 tx_desc->wb.status = 0;
234
235 i++;
236 if (i == tx_ring->count)
237 i = 0;
238 }
239
240 tx_ring->stats.packets++;
241
242 eop = tx_ring->tx_buffer_info[i].next_to_watch;
243 eop_desc = IXGBE_TX_DESC_ADV(*tx_ring, eop);
244
245 /* weight of a sort for tx, avoid endless transmit cleanup */
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800246 if (total_tx_packets >= tx_ring->work_limit)
Auke Kok9a799d72007-09-15 14:07:45 -0700247 break;
248 }
249
250 tx_ring->next_to_clean = i;
251
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800252#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
253 if (total_tx_packets && netif_carrier_ok(netdev) &&
254 (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD)) {
255 /* Make sure that anybody stopping the queue after this
256 * sees the new next_to_clean.
257 */
258 smp_mb();
259 if (netif_queue_stopped(netdev) &&
260 !test_bit(__IXGBE_DOWN, &adapter->state)) {
261 netif_wake_queue(netdev);
262 adapter->restart_queue++;
263 }
264 }
Auke Kok9a799d72007-09-15 14:07:45 -0700265
266 if (adapter->detect_tx_hung)
267 if (ixgbe_check_tx_hang(adapter, tx_ring, eop, eop_desc))
268 netif_stop_queue(netdev);
269
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800270 if (total_tx_packets >= tx_ring->work_limit)
Auke Kok9a799d72007-09-15 14:07:45 -0700271 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS, tx_ring->eims_value);
272
Ayyappan Veeraiyand2f4fbe2008-02-01 15:59:19 -0800273 adapter->net_stats.tx_bytes += total_tx_bytes;
274 adapter->net_stats.tx_packets += total_tx_packets;
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800275 cleaned = total_tx_packets ? true : false;
Auke Kok9a799d72007-09-15 14:07:45 -0700276 return cleaned;
277}
278
279/**
280 * ixgbe_receive_skb - Send a completed packet up the stack
281 * @adapter: board private structure
282 * @skb: packet to send up
283 * @is_vlan: packet has a VLAN tag
284 * @tag: VLAN tag from descriptor
285 **/
286static void ixgbe_receive_skb(struct ixgbe_adapter *adapter,
287 struct sk_buff *skb, bool is_vlan,
288 u16 tag)
289{
290 if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL)) {
291 if (adapter->vlgrp && is_vlan)
292 vlan_hwaccel_receive_skb(skb, adapter->vlgrp, tag);
293 else
294 netif_receive_skb(skb);
295 } else {
296
297 if (adapter->vlgrp && is_vlan)
298 vlan_hwaccel_rx(skb, adapter->vlgrp, tag);
299 else
300 netif_rx(skb);
301 }
302}
303
Ayyappan Veeraiyane59bd252008-02-01 15:59:09 -0800304/**
305 * ixgbe_rx_checksum - indicate in skb if hw indicated a good cksum
306 * @adapter: address of board private structure
307 * @status_err: hardware indication of status of receive
308 * @skb: skb currently being received and modified
309 **/
Auke Kok9a799d72007-09-15 14:07:45 -0700310static inline void ixgbe_rx_checksum(struct ixgbe_adapter *adapter,
311 u32 status_err,
312 struct sk_buff *skb)
313{
314 skb->ip_summed = CHECKSUM_NONE;
315
Ayyappan Veeraiyane59bd252008-02-01 15:59:09 -0800316 /* Ignore Checksum bit is set, or rx csum disabled */
Auke Kok9a799d72007-09-15 14:07:45 -0700317 if ((status_err & IXGBE_RXD_STAT_IXSM) ||
Ayyappan Veeraiyane59bd252008-02-01 15:59:09 -0800318 !(adapter->flags & IXGBE_FLAG_RX_CSUM_ENABLED))
Auke Kok9a799d72007-09-15 14:07:45 -0700319 return;
Ayyappan Veeraiyane59bd252008-02-01 15:59:09 -0800320
321 /* if IP and error */
322 if ((status_err & IXGBE_RXD_STAT_IPCS) &&
323 (status_err & IXGBE_RXDADV_ERR_IPE)) {
Auke Kok9a799d72007-09-15 14:07:45 -0700324 adapter->hw_csum_rx_error++;
325 return;
326 }
Ayyappan Veeraiyane59bd252008-02-01 15:59:09 -0800327
328 if (!(status_err & IXGBE_RXD_STAT_L4CS))
329 return;
330
331 if (status_err & IXGBE_RXDADV_ERR_TCPE) {
332 adapter->hw_csum_rx_error++;
333 return;
334 }
335
Auke Kok9a799d72007-09-15 14:07:45 -0700336 /* It must be a TCP or UDP packet with a valid checksum */
Ayyappan Veeraiyane59bd252008-02-01 15:59:09 -0800337 skb->ip_summed = CHECKSUM_UNNECESSARY;
Auke Kok9a799d72007-09-15 14:07:45 -0700338 adapter->hw_csum_rx_good++;
339}
340
341/**
342 * ixgbe_alloc_rx_buffers - Replace used receive buffers; packet split
343 * @adapter: address of board private structure
344 **/
345static void ixgbe_alloc_rx_buffers(struct ixgbe_adapter *adapter,
346 struct ixgbe_ring *rx_ring,
347 int cleaned_count)
348{
349 struct net_device *netdev = adapter->netdev;
350 struct pci_dev *pdev = adapter->pdev;
351 union ixgbe_adv_rx_desc *rx_desc;
352 struct ixgbe_rx_buffer *rx_buffer_info;
353 struct sk_buff *skb;
354 unsigned int i;
355 unsigned int bufsz = adapter->rx_buf_len + NET_IP_ALIGN;
356
357 i = rx_ring->next_to_use;
358 rx_buffer_info = &rx_ring->rx_buffer_info[i];
359
360 while (cleaned_count--) {
361 rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
362
363 if (!rx_buffer_info->page &&
364 (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED)) {
365 rx_buffer_info->page = alloc_page(GFP_ATOMIC);
366 if (!rx_buffer_info->page) {
367 adapter->alloc_rx_page_failed++;
368 goto no_buffers;
369 }
370 rx_buffer_info->page_dma =
371 pci_map_page(pdev, rx_buffer_info->page,
372 0, PAGE_SIZE, PCI_DMA_FROMDEVICE);
373 }
374
375 if (!rx_buffer_info->skb) {
376 skb = netdev_alloc_skb(netdev, bufsz);
377
378 if (!skb) {
379 adapter->alloc_rx_buff_failed++;
380 goto no_buffers;
381 }
382
383 /*
384 * Make buffer alignment 2 beyond a 16 byte boundary
385 * this will result in a 16 byte aligned IP header after
386 * the 14 byte MAC header is removed
387 */
388 skb_reserve(skb, NET_IP_ALIGN);
389
390 rx_buffer_info->skb = skb;
391 rx_buffer_info->dma = pci_map_single(pdev, skb->data,
392 bufsz,
393 PCI_DMA_FROMDEVICE);
394 }
395 /* Refresh the desc even if buffer_addrs didn't change because
396 * each write-back erases this info. */
397 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
398 rx_desc->read.pkt_addr =
399 cpu_to_le64(rx_buffer_info->page_dma);
400 rx_desc->read.hdr_addr =
401 cpu_to_le64(rx_buffer_info->dma);
402 } else {
403 rx_desc->read.pkt_addr =
404 cpu_to_le64(rx_buffer_info->dma);
405 }
406
407 i++;
408 if (i == rx_ring->count)
409 i = 0;
410 rx_buffer_info = &rx_ring->rx_buffer_info[i];
411 }
412no_buffers:
413 if (rx_ring->next_to_use != i) {
414 rx_ring->next_to_use = i;
415 if (i-- == 0)
416 i = (rx_ring->count - 1);
417
418 /*
419 * Force memory writes to complete before letting h/w
420 * know there are new descriptors to fetch. (Only
421 * applicable for weak-ordered memory model archs,
422 * such as IA-64).
423 */
424 wmb();
425 writel(i, adapter->hw.hw_addr + rx_ring->tail);
426 }
427}
428
429static bool ixgbe_clean_rx_irq(struct ixgbe_adapter *adapter,
430 struct ixgbe_ring *rx_ring,
431 int *work_done, int work_to_do)
432{
433 struct net_device *netdev = adapter->netdev;
434 struct pci_dev *pdev = adapter->pdev;
435 union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
436 struct ixgbe_rx_buffer *rx_buffer_info, *next_buffer;
437 struct sk_buff *skb;
438 unsigned int i;
439 u32 upper_len, len, staterr;
440 u16 hdr_info, vlan_tag;
441 bool is_vlan, cleaned = false;
442 int cleaned_count = 0;
Ayyappan Veeraiyand2f4fbe2008-02-01 15:59:19 -0800443 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
Auke Kok9a799d72007-09-15 14:07:45 -0700444
445 i = rx_ring->next_to_clean;
446 upper_len = 0;
447 rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
448 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
449 rx_buffer_info = &rx_ring->rx_buffer_info[i];
450 is_vlan = (staterr & IXGBE_RXD_STAT_VP);
451 vlan_tag = le16_to_cpu(rx_desc->wb.upper.vlan);
452
453 while (staterr & IXGBE_RXD_STAT_DD) {
454 if (*work_done >= work_to_do)
455 break;
456 (*work_done)++;
457
458 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
459 hdr_info =
460 le16_to_cpu(rx_desc->wb.lower.lo_dword.hdr_info);
461 len =
462 ((hdr_info & IXGBE_RXDADV_HDRBUFLEN_MASK) >>
463 IXGBE_RXDADV_HDRBUFLEN_SHIFT);
464 if (hdr_info & IXGBE_RXDADV_SPH)
465 adapter->rx_hdr_split++;
466 if (len > IXGBE_RX_HDR_SIZE)
467 len = IXGBE_RX_HDR_SIZE;
468 upper_len = le16_to_cpu(rx_desc->wb.upper.length);
469 } else
470 len = le16_to_cpu(rx_desc->wb.upper.length);
471
472 cleaned = true;
473 skb = rx_buffer_info->skb;
474 prefetch(skb->data - NET_IP_ALIGN);
475 rx_buffer_info->skb = NULL;
476
477 if (len && !skb_shinfo(skb)->nr_frags) {
478 pci_unmap_single(pdev, rx_buffer_info->dma,
479 adapter->rx_buf_len + NET_IP_ALIGN,
480 PCI_DMA_FROMDEVICE);
481 skb_put(skb, len);
482 }
483
484 if (upper_len) {
485 pci_unmap_page(pdev, rx_buffer_info->page_dma,
486 PAGE_SIZE, PCI_DMA_FROMDEVICE);
487 rx_buffer_info->page_dma = 0;
488 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
489 rx_buffer_info->page, 0, upper_len);
490 rx_buffer_info->page = NULL;
491
492 skb->len += upper_len;
493 skb->data_len += upper_len;
494 skb->truesize += upper_len;
495 }
496
497 i++;
498 if (i == rx_ring->count)
499 i = 0;
500 next_buffer = &rx_ring->rx_buffer_info[i];
501
502 next_rxd = IXGBE_RX_DESC_ADV(*rx_ring, i);
503 prefetch(next_rxd);
504
505 cleaned_count++;
506 if (staterr & IXGBE_RXD_STAT_EOP) {
507 rx_ring->stats.packets++;
508 rx_ring->stats.bytes += skb->len;
509 } else {
510 rx_buffer_info->skb = next_buffer->skb;
511 rx_buffer_info->dma = next_buffer->dma;
512 next_buffer->skb = skb;
513 adapter->non_eop_descs++;
514 goto next_desc;
515 }
516
517 if (staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK) {
518 dev_kfree_skb_irq(skb);
519 goto next_desc;
520 }
521
522 ixgbe_rx_checksum(adapter, staterr, skb);
Ayyappan Veeraiyand2f4fbe2008-02-01 15:59:19 -0800523
524 /* probably a little skewed due to removing CRC */
525 total_rx_bytes += skb->len;
526 total_rx_packets++;
527
Auke Kok9a799d72007-09-15 14:07:45 -0700528 skb->protocol = eth_type_trans(skb, netdev);
529 ixgbe_receive_skb(adapter, skb, is_vlan, vlan_tag);
530 netdev->last_rx = jiffies;
531
532next_desc:
533 rx_desc->wb.upper.status_error = 0;
534
535 /* return some buffers to hardware, one at a time is too slow */
536 if (cleaned_count >= IXGBE_RX_BUFFER_WRITE) {
537 ixgbe_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
538 cleaned_count = 0;
539 }
540
541 /* use prefetched values */
542 rx_desc = next_rxd;
543 rx_buffer_info = next_buffer;
544
545 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
546 is_vlan = (staterr & IXGBE_RXD_STAT_VP);
547 vlan_tag = le16_to_cpu(rx_desc->wb.upper.vlan);
548 }
549
550 rx_ring->next_to_clean = i;
551 cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
552
553 if (cleaned_count)
554 ixgbe_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
555
Ayyappan Veeraiyand2f4fbe2008-02-01 15:59:19 -0800556 adapter->net_stats.rx_bytes += total_rx_bytes;
557 adapter->net_stats.rx_packets += total_rx_packets;
558
Auke Kok9a799d72007-09-15 14:07:45 -0700559 return cleaned;
560}
561
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800562static int ixgbe_clean_rxonly(struct napi_struct *, int);
Auke Kok9a799d72007-09-15 14:07:45 -0700563/**
564 * ixgbe_configure_msix - Configure MSI-X hardware
565 * @adapter: board private structure
566 *
567 * ixgbe_configure_msix sets up the hardware to properly generate MSI-X
568 * interrupts.
569 **/
570static void ixgbe_configure_msix(struct ixgbe_adapter *adapter)
571{
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800572 struct ixgbe_q_vector *q_vector;
573 int i, j, q_vectors, v_idx, r_idx;
574 u32 mask;
Auke Kok9a799d72007-09-15 14:07:45 -0700575
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800576 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
577
578 /* Populate the IVAR table and set the ITR values to the
579 * corresponding register.
580 */
581 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
582 q_vector = &adapter->q_vector[v_idx];
583 /* XXX for_each_bit(...) */
584 r_idx = find_first_bit(q_vector->rxr_idx,
585 adapter->num_rx_queues);
586
587 for (i = 0; i < q_vector->rxr_count; i++) {
588 j = adapter->rx_ring[r_idx].reg_idx;
589 ixgbe_set_ivar(adapter, IXGBE_IVAR_RX_QUEUE(j), v_idx);
590 r_idx = find_next_bit(q_vector->rxr_idx,
591 adapter->num_rx_queues,
592 r_idx + 1);
593 }
594 r_idx = find_first_bit(q_vector->txr_idx,
595 adapter->num_tx_queues);
596
597 for (i = 0; i < q_vector->txr_count; i++) {
598 j = adapter->tx_ring[r_idx].reg_idx;
599 ixgbe_set_ivar(adapter, IXGBE_IVAR_TX_QUEUE(j), v_idx);
600 r_idx = find_next_bit(q_vector->txr_idx,
601 adapter->num_tx_queues,
602 r_idx + 1);
603 }
604
605 /* if this is a tx only vector use half the irq (tx) rate */
606 if (q_vector->txr_count && !q_vector->rxr_count)
607 q_vector->eitr = adapter->tx_eitr;
608 else
609 /* rx only or mixed */
610 q_vector->eitr = adapter->rx_eitr;
611
612 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(v_idx),
613 EITR_INTS_PER_SEC_TO_REG(q_vector->eitr));
Auke Kok9a799d72007-09-15 14:07:45 -0700614 }
615
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800616 ixgbe_set_ivar(adapter, IXGBE_IVAR_OTHER_CAUSES_INDEX, v_idx);
617 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(v_idx), 1950);
Auke Kok9a799d72007-09-15 14:07:45 -0700618
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800619 /* set up to autoclear timer, lsc, and the vectors */
620 mask = IXGBE_EIMS_ENABLE_MASK;
621 mask &= ~IXGBE_EIMS_OTHER;
622 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC, mask);
Auke Kok9a799d72007-09-15 14:07:45 -0700623}
624
625static irqreturn_t ixgbe_msix_lsc(int irq, void *data)
626{
627 struct net_device *netdev = data;
628 struct ixgbe_adapter *adapter = netdev_priv(netdev);
629 struct ixgbe_hw *hw = &adapter->hw;
630 u32 eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
631
632 if (eicr & IXGBE_EICR_LSC) {
633 adapter->lsc_int++;
634 if (!test_bit(__IXGBE_DOWN, &adapter->state))
635 mod_timer(&adapter->watchdog_timer, jiffies);
636 }
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -0800637
638 if (!test_bit(__IXGBE_DOWN, &adapter->state))
639 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_OTHER);
Auke Kok9a799d72007-09-15 14:07:45 -0700640
641 return IRQ_HANDLED;
642}
643
644static irqreturn_t ixgbe_msix_clean_tx(int irq, void *data)
645{
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800646 struct ixgbe_q_vector *q_vector = data;
647 struct ixgbe_adapter *adapter = q_vector->adapter;
648 struct ixgbe_ring *txr;
649 int i, r_idx;
Auke Kok9a799d72007-09-15 14:07:45 -0700650
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800651 if (!q_vector->txr_count)
652 return IRQ_HANDLED;
653
654 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
655 for (i = 0; i < q_vector->txr_count; i++) {
656 txr = &(adapter->tx_ring[r_idx]);
657 ixgbe_clean_tx_irq(adapter, txr);
658 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
659 r_idx + 1);
660 }
661
Auke Kok9a799d72007-09-15 14:07:45 -0700662
663 return IRQ_HANDLED;
664}
665
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800666/**
667 * ixgbe_msix_clean_rx - single unshared vector rx clean (all queues)
668 * @irq: unused
669 * @data: pointer to our q_vector struct for this interrupt vector
670 **/
Auke Kok9a799d72007-09-15 14:07:45 -0700671static irqreturn_t ixgbe_msix_clean_rx(int irq, void *data)
672{
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800673 struct ixgbe_q_vector *q_vector = data;
674 struct ixgbe_adapter *adapter = q_vector->adapter;
675 struct ixgbe_ring *rxr;
676 int r_idx;
Auke Kok9a799d72007-09-15 14:07:45 -0700677
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800678 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
679 if (!q_vector->rxr_count)
680 return IRQ_HANDLED;
681
682 rxr = &(adapter->rx_ring[r_idx]);
683 /* disable interrupts on this vector only */
684 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, rxr->v_idx);
685 netif_rx_schedule(adapter->netdev, &q_vector->napi);
686
Auke Kok9a799d72007-09-15 14:07:45 -0700687 return IRQ_HANDLED;
688}
689
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800690static irqreturn_t ixgbe_msix_clean_many(int irq, void *data)
691{
692 ixgbe_msix_clean_rx(irq, data);
693 ixgbe_msix_clean_tx(irq, data);
694
695 return IRQ_HANDLED;
696}
697
698/**
699 * ixgbe_clean_rxonly - msix (aka one shot) rx clean routine
700 * @napi: napi struct with our devices info in it
701 * @budget: amount of work driver is allowed to do this pass, in packets
702 *
703 **/
Auke Kok9a799d72007-09-15 14:07:45 -0700704static int ixgbe_clean_rxonly(struct napi_struct *napi, int budget)
705{
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800706 struct ixgbe_q_vector *q_vector =
707 container_of(napi, struct ixgbe_q_vector, napi);
708 struct ixgbe_adapter *adapter = q_vector->adapter;
709 struct ixgbe_ring *rxr;
Auke Kok9a799d72007-09-15 14:07:45 -0700710 int work_done = 0;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800711 long r_idx;
Auke Kok9a799d72007-09-15 14:07:45 -0700712
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800713 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
714 rxr = &(adapter->rx_ring[r_idx]);
Auke Kok9a799d72007-09-15 14:07:45 -0700715
716 ixgbe_clean_rx_irq(adapter, rxr, &work_done, budget);
717
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800718 /* If all Rx work done, exit the polling mode */
719 if (work_done < budget) {
720 netif_rx_complete(adapter->netdev, napi);
Auke Kok9a799d72007-09-15 14:07:45 -0700721 if (!test_bit(__IXGBE_DOWN, &adapter->state))
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800722 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, rxr->v_idx);
Auke Kok9a799d72007-09-15 14:07:45 -0700723 }
724
725 return work_done;
726}
727
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800728static inline void map_vector_to_rxq(struct ixgbe_adapter *a, int v_idx,
729 int r_idx)
Auke Kok9a799d72007-09-15 14:07:45 -0700730{
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800731 a->q_vector[v_idx].adapter = a;
732 set_bit(r_idx, a->q_vector[v_idx].rxr_idx);
733 a->q_vector[v_idx].rxr_count++;
734 a->rx_ring[r_idx].v_idx = 1 << v_idx;
735}
Auke Kok9a799d72007-09-15 14:07:45 -0700736
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800737static inline void map_vector_to_txq(struct ixgbe_adapter *a, int v_idx,
738 int r_idx)
739{
740 a->q_vector[v_idx].adapter = a;
741 set_bit(r_idx, a->q_vector[v_idx].txr_idx);
742 a->q_vector[v_idx].txr_count++;
743 a->tx_ring[r_idx].v_idx = 1 << v_idx;
744}
Auke Kok9a799d72007-09-15 14:07:45 -0700745
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800746/**
747 * ixgbe_map_rings_to_vectors - Maps descriptor rings to vectors
748 * @adapter: board private structure to initialize
749 * @vectors: allotted vector count for descriptor rings
750 *
751 * This function maps descriptor rings to the queue-specific vectors
752 * we were allotted through the MSI-X enabling code. Ideally, we'd have
753 * one vector per ring/queue, but on a constrained vector budget, we
754 * group the rings as "efficiently" as possible. You would add new
755 * mapping configurations in here.
756 **/
757static int ixgbe_map_rings_to_vectors(struct ixgbe_adapter *adapter,
758 int vectors)
759{
760 int v_start = 0;
761 int rxr_idx = 0, txr_idx = 0;
762 int rxr_remaining = adapter->num_rx_queues;
763 int txr_remaining = adapter->num_tx_queues;
764 int i, j;
765 int rqpv, tqpv;
766 int err = 0;
Auke Kok9a799d72007-09-15 14:07:45 -0700767
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800768 /* No mapping required if MSI-X is disabled. */
769 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
Auke Kok9a799d72007-09-15 14:07:45 -0700770 goto out;
771
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800772 /*
773 * The ideal configuration...
774 * We have enough vectors to map one per queue.
775 */
776 if (vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
777 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
778 map_vector_to_rxq(adapter, v_start, rxr_idx);
779
780 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
781 map_vector_to_txq(adapter, v_start, txr_idx);
782
783 goto out;
784 }
785
786 /*
787 * If we don't have enough vectors for a 1-to-1
788 * mapping, we'll have to group them so there are
789 * multiple queues per vector.
790 */
791 /* Re-adjusting *qpv takes care of the remainder. */
792 for (i = v_start; i < vectors; i++) {
793 rqpv = DIV_ROUND_UP(rxr_remaining, vectors - i);
794 for (j = 0; j < rqpv; j++) {
795 map_vector_to_rxq(adapter, i, rxr_idx);
796 rxr_idx++;
797 rxr_remaining--;
Auke Kok9a799d72007-09-15 14:07:45 -0700798 }
Auke Kok9a799d72007-09-15 14:07:45 -0700799 }
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800800 for (i = v_start; i < vectors; i++) {
801 tqpv = DIV_ROUND_UP(txr_remaining, vectors - i);
802 for (j = 0; j < tqpv; j++) {
803 map_vector_to_txq(adapter, i, txr_idx);
804 txr_idx++;
805 txr_remaining--;
Auke Kok9a799d72007-09-15 14:07:45 -0700806 }
Auke Kok9a799d72007-09-15 14:07:45 -0700807 }
808
Auke Kok9a799d72007-09-15 14:07:45 -0700809out:
Auke Kok9a799d72007-09-15 14:07:45 -0700810 return err;
811}
812
813/**
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800814 * ixgbe_request_msix_irqs - Initialize MSI-X interrupts
815 * @adapter: board private structure
816 *
817 * ixgbe_request_msix_irqs allocates MSI-X vectors and requests
818 * interrupts from the kernel.
819 **/
820static int ixgbe_request_msix_irqs(struct ixgbe_adapter *adapter)
821{
822 struct net_device *netdev = adapter->netdev;
823 irqreturn_t (*handler)(int, void *);
824 int i, vector, q_vectors, err;
825
826 /* Decrement for Other and TCP Timer vectors */
827 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
828
829 /* Map the Tx/Rx rings to the vectors we were allotted. */
830 err = ixgbe_map_rings_to_vectors(adapter, q_vectors);
831 if (err)
832 goto out;
833
834#define SET_HANDLER(_v) ((!(_v)->rxr_count) ? &ixgbe_msix_clean_tx : \
835 (!(_v)->txr_count) ? &ixgbe_msix_clean_rx : \
836 &ixgbe_msix_clean_many)
837 for (vector = 0; vector < q_vectors; vector++) {
838 handler = SET_HANDLER(&adapter->q_vector[vector]);
839 sprintf(adapter->name[vector], "%s:v%d-%s",
840 netdev->name, vector,
841 (handler == &ixgbe_msix_clean_rx) ? "Rx" :
842 ((handler == &ixgbe_msix_clean_tx) ? "Tx" : "TxRx"));
843 err = request_irq(adapter->msix_entries[vector].vector,
844 handler, 0, adapter->name[vector],
845 &(adapter->q_vector[vector]));
846 if (err) {
847 DPRINTK(PROBE, ERR,
848 "request_irq failed for MSIX interrupt "
849 "Error: %d\n", err);
850 goto free_queue_irqs;
851 }
852 }
853
854 sprintf(adapter->name[vector], "%s:lsc", netdev->name);
855 err = request_irq(adapter->msix_entries[vector].vector,
856 &ixgbe_msix_lsc, 0, adapter->name[vector], netdev);
857 if (err) {
858 DPRINTK(PROBE, ERR,
859 "request_irq for msix_lsc failed: %d\n", err);
860 goto free_queue_irqs;
861 }
862
863 return 0;
864
865free_queue_irqs:
866 for (i = vector - 1; i >= 0; i--)
867 free_irq(adapter->msix_entries[--vector].vector,
868 &(adapter->q_vector[i]));
869 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
870 pci_disable_msix(adapter->pdev);
871 kfree(adapter->msix_entries);
872 adapter->msix_entries = NULL;
873out:
874 return err;
875}
876
877static inline void ixgbe_irq_enable(struct ixgbe_adapter *adapter);
878
879/**
880 * ixgbe_intr - legacy mode Interrupt Handler
Auke Kok9a799d72007-09-15 14:07:45 -0700881 * @irq: interrupt number
882 * @data: pointer to a network interface device structure
883 * @pt_regs: CPU registers structure
884 **/
885static irqreturn_t ixgbe_intr(int irq, void *data)
886{
887 struct net_device *netdev = data;
888 struct ixgbe_adapter *adapter = netdev_priv(netdev);
889 struct ixgbe_hw *hw = &adapter->hw;
890 u32 eicr;
891
Auke Kok9a799d72007-09-15 14:07:45 -0700892
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800893 /* for NAPI, using EIAM to auto-mask tx/rx interrupt bits on read
894 * therefore no explict interrupt disable is necessary */
895 eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
Auke Kok9a799d72007-09-15 14:07:45 -0700896 if (!eicr)
897 return IRQ_NONE; /* Not our interrupt */
898
899 if (eicr & IXGBE_EICR_LSC) {
900 adapter->lsc_int++;
901 if (!test_bit(__IXGBE_DOWN, &adapter->state))
902 mod_timer(&adapter->watchdog_timer, jiffies);
903 }
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800904
905
906 if (netif_rx_schedule_prep(netdev, &adapter->q_vector[0].napi)) {
907 /* would disable interrupts here but EIAM disabled it */
908 __netif_rx_schedule(netdev, &adapter->q_vector[0].napi);
Auke Kok9a799d72007-09-15 14:07:45 -0700909 }
910
911 return IRQ_HANDLED;
912}
913
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800914static inline void ixgbe_reset_q_vectors(struct ixgbe_adapter *adapter)
915{
916 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
917
918 for (i = 0; i < q_vectors; i++) {
919 struct ixgbe_q_vector *q_vector = &adapter->q_vector[i];
920 bitmap_zero(q_vector->rxr_idx, MAX_RX_QUEUES);
921 bitmap_zero(q_vector->txr_idx, MAX_TX_QUEUES);
922 q_vector->rxr_count = 0;
923 q_vector->txr_count = 0;
924 }
925}
926
Auke Kok9a799d72007-09-15 14:07:45 -0700927/**
928 * ixgbe_request_irq - initialize interrupts
929 * @adapter: board private structure
930 *
931 * Attempts to configure interrupts using the best available
932 * capabilities of the hardware and kernel.
933 **/
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800934static int ixgbe_request_irq(struct ixgbe_adapter *adapter)
Auke Kok9a799d72007-09-15 14:07:45 -0700935{
936 struct net_device *netdev = adapter->netdev;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800937 int err;
Auke Kok9a799d72007-09-15 14:07:45 -0700938
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800939 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
940 err = ixgbe_request_msix_irqs(adapter);
941 } else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED) {
942 err = request_irq(adapter->pdev->irq, &ixgbe_intr, 0,
943 netdev->name, netdev);
944 } else {
945 err = request_irq(adapter->pdev->irq, &ixgbe_intr, IRQF_SHARED,
946 netdev->name, netdev);
Auke Kok9a799d72007-09-15 14:07:45 -0700947 }
948
Auke Kok9a799d72007-09-15 14:07:45 -0700949 if (err)
950 DPRINTK(PROBE, ERR, "request_irq failed, Error %d\n", err);
951
Auke Kok9a799d72007-09-15 14:07:45 -0700952 return err;
953}
954
955static void ixgbe_free_irq(struct ixgbe_adapter *adapter)
956{
957 struct net_device *netdev = adapter->netdev;
958
959 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800960 int i, q_vectors;
Auke Kok9a799d72007-09-15 14:07:45 -0700961
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800962 q_vectors = adapter->num_msix_vectors;
963
964 i = q_vectors - 1;
Auke Kok9a799d72007-09-15 14:07:45 -0700965 free_irq(adapter->msix_entries[i].vector, netdev);
Auke Kok9a799d72007-09-15 14:07:45 -0700966
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800967 i--;
968 for (; i >= 0; i--) {
969 free_irq(adapter->msix_entries[i].vector,
970 &(adapter->q_vector[i]));
971 }
972
973 ixgbe_reset_q_vectors(adapter);
974 } else {
975 free_irq(adapter->pdev->irq, netdev);
Auke Kok9a799d72007-09-15 14:07:45 -0700976 }
977}
978
979/**
980 * ixgbe_irq_disable - Mask off interrupt generation on the NIC
981 * @adapter: board private structure
982 **/
983static inline void ixgbe_irq_disable(struct ixgbe_adapter *adapter)
984{
Auke Kok9a799d72007-09-15 14:07:45 -0700985 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~0);
986 IXGBE_WRITE_FLUSH(&adapter->hw);
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800987 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
988 int i;
989 for (i = 0; i < adapter->num_msix_vectors; i++)
990 synchronize_irq(adapter->msix_entries[i].vector);
991 } else {
992 synchronize_irq(adapter->pdev->irq);
993 }
Auke Kok9a799d72007-09-15 14:07:45 -0700994}
995
996/**
997 * ixgbe_irq_enable - Enable default interrupt generation settings
998 * @adapter: board private structure
999 **/
1000static inline void ixgbe_irq_enable(struct ixgbe_adapter *adapter)
1001{
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001002 u32 mask;
1003 mask = IXGBE_EIMS_ENABLE_MASK;
1004 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, mask);
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08001005 IXGBE_WRITE_FLUSH(&adapter->hw);
Auke Kok9a799d72007-09-15 14:07:45 -07001006}
1007
1008/**
1009 * ixgbe_configure_msi_and_legacy - Initialize PIN (INTA...) and MSI interrupts
1010 *
1011 **/
1012static void ixgbe_configure_msi_and_legacy(struct ixgbe_adapter *adapter)
1013{
Auke Kok9a799d72007-09-15 14:07:45 -07001014 struct ixgbe_hw *hw = &adapter->hw;
1015
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001016 IXGBE_WRITE_REG(hw, IXGBE_EITR(0),
1017 EITR_INTS_PER_SEC_TO_REG(adapter->rx_eitr));
Auke Kok9a799d72007-09-15 14:07:45 -07001018
1019 ixgbe_set_ivar(adapter, IXGBE_IVAR_RX_QUEUE(0), 0);
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001020 ixgbe_set_ivar(adapter, IXGBE_IVAR_TX_QUEUE(0), 0);
1021
1022 map_vector_to_rxq(adapter, 0, 0);
1023 map_vector_to_txq(adapter, 0, 0);
1024
1025 DPRINTK(HW, INFO, "Legacy interrupt IVAR setup done\n");
Auke Kok9a799d72007-09-15 14:07:45 -07001026}
1027
1028/**
1029 * ixgbe_configure_tx - Configure 8254x Transmit Unit after Reset
1030 * @adapter: board private structure
1031 *
1032 * Configure the Tx unit of the MAC after a reset.
1033 **/
1034static void ixgbe_configure_tx(struct ixgbe_adapter *adapter)
1035{
1036 u64 tdba;
1037 struct ixgbe_hw *hw = &adapter->hw;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001038 u32 i, j, tdlen, txctrl;
Auke Kok9a799d72007-09-15 14:07:45 -07001039
1040 /* Setup the HW Tx Head and Tail descriptor pointers */
1041 for (i = 0; i < adapter->num_tx_queues; i++) {
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001042 j = adapter->tx_ring[i].reg_idx;
Auke Kok9a799d72007-09-15 14:07:45 -07001043 tdba = adapter->tx_ring[i].dma;
1044 tdlen = adapter->tx_ring[i].count *
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001045 sizeof(union ixgbe_adv_tx_desc);
1046 IXGBE_WRITE_REG(hw, IXGBE_TDBAL(j),
1047 (tdba & DMA_32BIT_MASK));
1048 IXGBE_WRITE_REG(hw, IXGBE_TDBAH(j), (tdba >> 32));
1049 IXGBE_WRITE_REG(hw, IXGBE_TDLEN(j), tdlen);
1050 IXGBE_WRITE_REG(hw, IXGBE_TDH(j), 0);
1051 IXGBE_WRITE_REG(hw, IXGBE_TDT(j), 0);
1052 adapter->tx_ring[i].head = IXGBE_TDH(j);
1053 adapter->tx_ring[i].tail = IXGBE_TDT(j);
1054 /* Disable Tx Head Writeback RO bit, since this hoses
1055 * bookkeeping if things aren't delivered in order.
1056 */
1057 txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(i));
1058 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1059 IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(i), txctrl);
Auke Kok9a799d72007-09-15 14:07:45 -07001060 }
Auke Kok9a799d72007-09-15 14:07:45 -07001061}
1062
1063#define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
1064 (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
1065
1066#define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1067/**
1068 * ixgbe_configure_rx - Configure 8254x Receive Unit after Reset
1069 * @adapter: board private structure
1070 *
1071 * Configure the Rx unit of the MAC after a reset.
1072 **/
1073static void ixgbe_configure_rx(struct ixgbe_adapter *adapter)
1074{
1075 u64 rdba;
1076 struct ixgbe_hw *hw = &adapter->hw;
1077 struct net_device *netdev = adapter->netdev;
1078 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001079 int i, j;
Auke Kok9a799d72007-09-15 14:07:45 -07001080 u32 rdlen, rxctrl, rxcsum;
1081 u32 random[10];
Auke Kok9a799d72007-09-15 14:07:45 -07001082 u32 fctrl, hlreg0;
Auke Kok9a799d72007-09-15 14:07:45 -07001083 u32 pages;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001084 u32 reta = 0, mrqc, srrctl;
Auke Kok9a799d72007-09-15 14:07:45 -07001085
1086 /* Decide whether to use packet split mode or not */
1087 if (netdev->mtu > ETH_DATA_LEN)
1088 adapter->flags |= IXGBE_FLAG_RX_PS_ENABLED;
1089 else
1090 adapter->flags &= ~IXGBE_FLAG_RX_PS_ENABLED;
1091
1092 /* Set the RX buffer length according to the mode */
1093 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
1094 adapter->rx_buf_len = IXGBE_RX_HDR_SIZE;
1095 } else {
1096 if (netdev->mtu <= ETH_DATA_LEN)
1097 adapter->rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1098 else
1099 adapter->rx_buf_len = ALIGN(max_frame, 1024);
1100 }
1101
1102 fctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
1103 fctrl |= IXGBE_FCTRL_BAM;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001104 fctrl |= IXGBE_FCTRL_DPF; /* discard pause frames when FC enabled */
Auke Kok9a799d72007-09-15 14:07:45 -07001105 IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, fctrl);
1106
1107 hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
1108 if (adapter->netdev->mtu <= ETH_DATA_LEN)
1109 hlreg0 &= ~IXGBE_HLREG0_JUMBOEN;
1110 else
1111 hlreg0 |= IXGBE_HLREG0_JUMBOEN;
1112 IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
1113
1114 pages = PAGE_USE_COUNT(adapter->netdev->mtu);
1115
1116 srrctl = IXGBE_READ_REG(&adapter->hw, IXGBE_SRRCTL(0));
1117 srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
1118 srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
1119
1120 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
1121 srrctl |= PAGE_SIZE >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1122 srrctl |= IXGBE_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
1123 srrctl |= ((IXGBE_RX_HDR_SIZE <<
1124 IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT) &
1125 IXGBE_SRRCTL_BSIZEHDR_MASK);
1126 } else {
1127 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1128
1129 if (adapter->rx_buf_len == MAXIMUM_ETHERNET_VLAN_SIZE)
1130 srrctl |=
1131 IXGBE_RXBUFFER_2048 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1132 else
1133 srrctl |=
1134 adapter->rx_buf_len >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1135 }
1136 IXGBE_WRITE_REG(&adapter->hw, IXGBE_SRRCTL(0), srrctl);
1137
1138 rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1139 /* disable receives while setting up the descriptors */
1140 rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
1141 IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl & ~IXGBE_RXCTRL_RXEN);
1142
1143 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1144 * the Base and Length of the Rx Descriptor Ring */
1145 for (i = 0; i < adapter->num_rx_queues; i++) {
1146 rdba = adapter->rx_ring[i].dma;
1147 IXGBE_WRITE_REG(hw, IXGBE_RDBAL(i), (rdba & DMA_32BIT_MASK));
1148 IXGBE_WRITE_REG(hw, IXGBE_RDBAH(i), (rdba >> 32));
1149 IXGBE_WRITE_REG(hw, IXGBE_RDLEN(i), rdlen);
1150 IXGBE_WRITE_REG(hw, IXGBE_RDH(i), 0);
1151 IXGBE_WRITE_REG(hw, IXGBE_RDT(i), 0);
1152 adapter->rx_ring[i].head = IXGBE_RDH(i);
1153 adapter->rx_ring[i].tail = IXGBE_RDT(i);
1154 }
1155
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001156 if (adapter->flags & IXGBE_FLAG_RSS_ENABLED) {
Auke Kok9a799d72007-09-15 14:07:45 -07001157 /* Fill out redirection table */
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001158 for (i = 0, j = 0; i < 128; i++, j++) {
1159 if (j == adapter->ring_feature[RING_F_RSS].indices)
1160 j = 0;
1161 /* reta = 4-byte sliding window of
1162 * 0x00..(indices-1)(indices-1)00..etc. */
1163 reta = (reta << 8) | (j * 0x11);
1164 if ((i & 3) == 3)
1165 IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
Auke Kok9a799d72007-09-15 14:07:45 -07001166 }
1167
1168 /* Fill out hash function seeds */
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001169 /* XXX use a random constant here to glue certain flows */
1170 get_random_bytes(&random[0], 40);
Auke Kok9a799d72007-09-15 14:07:45 -07001171 for (i = 0; i < 10; i++)
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001172 IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), random[i]);
Auke Kok9a799d72007-09-15 14:07:45 -07001173
1174 mrqc = IXGBE_MRQC_RSSEN
1175 /* Perform hash on these packet types */
1176 | IXGBE_MRQC_RSS_FIELD_IPV4
1177 | IXGBE_MRQC_RSS_FIELD_IPV4_TCP
1178 | IXGBE_MRQC_RSS_FIELD_IPV4_UDP
1179 | IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP
1180 | IXGBE_MRQC_RSS_FIELD_IPV6_EX
1181 | IXGBE_MRQC_RSS_FIELD_IPV6
1182 | IXGBE_MRQC_RSS_FIELD_IPV6_TCP
1183 | IXGBE_MRQC_RSS_FIELD_IPV6_UDP
1184 | IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP;
1185 IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
Auke Kok9a799d72007-09-15 14:07:45 -07001186 }
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001187
1188 rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
1189
1190 if (adapter->flags & IXGBE_FLAG_RSS_ENABLED ||
1191 adapter->flags & IXGBE_FLAG_RX_CSUM_ENABLED) {
1192 /* Disable indicating checksum in descriptor, enables
1193 * RSS hash */
1194 rxcsum |= IXGBE_RXCSUM_PCSD;
1195 }
1196 if (!(rxcsum & IXGBE_RXCSUM_PCSD)) {
1197 /* Enable IPv4 payload checksum for UDP fragments
1198 * if PCSD is not set */
1199 rxcsum |= IXGBE_RXCSUM_IPPCSE;
1200 }
1201
1202 IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
Auke Kok9a799d72007-09-15 14:07:45 -07001203}
1204
1205static void ixgbe_vlan_rx_register(struct net_device *netdev,
1206 struct vlan_group *grp)
1207{
1208 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1209 u32 ctrl;
1210
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08001211 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1212 ixgbe_irq_disable(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07001213 adapter->vlgrp = grp;
1214
1215 if (grp) {
1216 /* enable VLAN tag insert/strip */
1217 ctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_VLNCTRL);
1218 ctrl |= IXGBE_VLNCTRL_VME | IXGBE_VLNCTRL_VFE;
1219 ctrl &= ~IXGBE_VLNCTRL_CFIEN;
1220 IXGBE_WRITE_REG(&adapter->hw, IXGBE_VLNCTRL, ctrl);
1221 }
1222
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08001223 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1224 ixgbe_irq_enable(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07001225}
1226
1227static void ixgbe_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1228{
1229 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1230
1231 /* add VID to filter table */
1232 ixgbe_set_vfta(&adapter->hw, vid, 0, true);
1233}
1234
1235static void ixgbe_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1236{
1237 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1238
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08001239 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1240 ixgbe_irq_disable(adapter);
1241
Auke Kok9a799d72007-09-15 14:07:45 -07001242 vlan_group_set_device(adapter->vlgrp, vid, NULL);
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08001243
1244 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1245 ixgbe_irq_enable(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07001246
1247 /* remove VID from filter table */
1248 ixgbe_set_vfta(&adapter->hw, vid, 0, false);
1249}
1250
1251static void ixgbe_restore_vlan(struct ixgbe_adapter *adapter)
1252{
1253 ixgbe_vlan_rx_register(adapter->netdev, adapter->vlgrp);
1254
1255 if (adapter->vlgrp) {
1256 u16 vid;
1257 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
1258 if (!vlan_group_get_device(adapter->vlgrp, vid))
1259 continue;
1260 ixgbe_vlan_rx_add_vid(adapter->netdev, vid);
1261 }
1262 }
1263}
1264
1265/**
1266 * ixgbe_set_multi - Multicast and Promiscuous mode set
1267 * @netdev: network interface device structure
1268 *
1269 * The set_multi entry point is called whenever the multicast address
1270 * list or the network interface flags are updated. This routine is
1271 * responsible for configuring the hardware for proper multicast,
1272 * promiscuous mode, and all-multi behavior.
1273 **/
1274static void ixgbe_set_multi(struct net_device *netdev)
1275{
1276 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1277 struct ixgbe_hw *hw = &adapter->hw;
1278 struct dev_mc_list *mc_ptr;
1279 u8 *mta_list;
1280 u32 fctrl;
1281 int i;
1282
1283 /* Check for Promiscuous and All Multicast modes */
1284
1285 fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
1286
1287 if (netdev->flags & IFF_PROMISC) {
1288 fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
1289 } else if (netdev->flags & IFF_ALLMULTI) {
1290 fctrl |= IXGBE_FCTRL_MPE;
1291 fctrl &= ~IXGBE_FCTRL_UPE;
1292 } else {
1293 fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
1294 }
1295
1296 IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
1297
1298 if (netdev->mc_count) {
1299 mta_list = kcalloc(netdev->mc_count, ETH_ALEN, GFP_ATOMIC);
1300 if (!mta_list)
1301 return;
1302
1303 /* Shared function expects packed array of only addresses. */
1304 mc_ptr = netdev->mc_list;
1305
1306 for (i = 0; i < netdev->mc_count; i++) {
1307 if (!mc_ptr)
1308 break;
1309 memcpy(mta_list + (i * ETH_ALEN), mc_ptr->dmi_addr,
1310 ETH_ALEN);
1311 mc_ptr = mc_ptr->next;
1312 }
1313
1314 ixgbe_update_mc_addr_list(hw, mta_list, i, 0);
1315 kfree(mta_list);
1316 } else {
1317 ixgbe_update_mc_addr_list(hw, NULL, 0, 0);
1318 }
1319
1320}
1321
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001322static void ixgbe_napi_enable_all(struct ixgbe_adapter *adapter)
1323{
1324 int q_idx;
1325 struct ixgbe_q_vector *q_vector;
1326 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1327
1328 /* legacy and MSI only use one vector */
1329 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
1330 q_vectors = 1;
1331
1332 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1333 q_vector = &adapter->q_vector[q_idx];
1334 if (!q_vector->rxr_count)
1335 continue;
1336 napi_enable(&q_vector->napi);
1337 }
1338}
1339
1340static void ixgbe_napi_disable_all(struct ixgbe_adapter *adapter)
1341{
1342 int q_idx;
1343 struct ixgbe_q_vector *q_vector;
1344 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1345
1346 /* legacy and MSI only use one vector */
1347 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
1348 q_vectors = 1;
1349
1350 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1351 q_vector = &adapter->q_vector[q_idx];
1352 if (!q_vector->rxr_count)
1353 continue;
1354 napi_disable(&q_vector->napi);
1355 }
1356}
1357
1358
Auke Kok9a799d72007-09-15 14:07:45 -07001359static void ixgbe_configure(struct ixgbe_adapter *adapter)
1360{
1361 struct net_device *netdev = adapter->netdev;
1362 int i;
1363
1364 ixgbe_set_multi(netdev);
1365
1366 ixgbe_restore_vlan(adapter);
1367
1368 ixgbe_configure_tx(adapter);
1369 ixgbe_configure_rx(adapter);
1370 for (i = 0; i < adapter->num_rx_queues; i++)
1371 ixgbe_alloc_rx_buffers(adapter, &adapter->rx_ring[i],
1372 (adapter->rx_ring[i].count - 1));
1373}
1374
1375static int ixgbe_up_complete(struct ixgbe_adapter *adapter)
1376{
1377 struct net_device *netdev = adapter->netdev;
Auke Kok9a799d72007-09-15 14:07:45 -07001378 struct ixgbe_hw *hw = &adapter->hw;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001379 int i, j = 0;
Auke Kok9a799d72007-09-15 14:07:45 -07001380 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001381 u32 txdctl, rxdctl, mhadd;
1382 u32 gpie;
Auke Kok9a799d72007-09-15 14:07:45 -07001383
Ayyappan Veeraiyan5eba3692008-02-01 15:59:04 -08001384 ixgbe_get_hw_control(adapter);
1385
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001386 if ((adapter->flags & IXGBE_FLAG_MSIX_ENABLED) ||
1387 (adapter->flags & IXGBE_FLAG_MSI_ENABLED)) {
Auke Kok9a799d72007-09-15 14:07:45 -07001388 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
1389 gpie = (IXGBE_GPIE_MSIX_MODE | IXGBE_GPIE_EIAME |
1390 IXGBE_GPIE_PBA_SUPPORT | IXGBE_GPIE_OCD);
1391 } else {
1392 /* MSI only */
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001393 gpie = 0;
Auke Kok9a799d72007-09-15 14:07:45 -07001394 }
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001395 /* XXX: to interrupt immediately for EICS writes, enable this */
1396 /* gpie |= IXGBE_GPIE_EIMEN; */
1397 IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
1398 }
1399
1400 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED)) {
1401 /* legacy interrupts, use EIAM to auto-mask when reading EICR,
1402 * specifically only auto mask tx and rx interrupts */
1403 IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
Auke Kok9a799d72007-09-15 14:07:45 -07001404 }
1405
1406 mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
Auke Kok9a799d72007-09-15 14:07:45 -07001407 if (max_frame != (mhadd >> IXGBE_MHADD_MFS_SHIFT)) {
1408 mhadd &= ~IXGBE_MHADD_MFS_MASK;
1409 mhadd |= max_frame << IXGBE_MHADD_MFS_SHIFT;
1410
1411 IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
1412 }
1413
1414 for (i = 0; i < adapter->num_tx_queues; i++) {
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001415 j = adapter->tx_ring[i].reg_idx;
1416 txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(j));
Auke Kok9a799d72007-09-15 14:07:45 -07001417 txdctl |= IXGBE_TXDCTL_ENABLE;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001418 IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(j), txdctl);
Auke Kok9a799d72007-09-15 14:07:45 -07001419 }
1420
1421 for (i = 0; i < adapter->num_rx_queues; i++) {
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001422 j = adapter->rx_ring[i].reg_idx;
1423 rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(j));
1424 /* enable PTHRESH=32 descriptors (half the internal cache)
1425 * and HTHRESH=0 descriptors (to minimize latency on fetch),
1426 * this also removes a pesky rx_no_buffer_count increment */
1427 rxdctl |= 0x0020;
Auke Kok9a799d72007-09-15 14:07:45 -07001428 rxdctl |= IXGBE_RXDCTL_ENABLE;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001429 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(j), rxdctl);
Auke Kok9a799d72007-09-15 14:07:45 -07001430 }
1431 /* enable all receives */
1432 rxdctl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
1433 rxdctl |= (IXGBE_RXCTRL_DMBYPS | IXGBE_RXCTRL_RXEN);
1434 IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxdctl);
1435
1436 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
1437 ixgbe_configure_msix(adapter);
1438 else
1439 ixgbe_configure_msi_and_legacy(adapter);
1440
1441 clear_bit(__IXGBE_DOWN, &adapter->state);
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001442 ixgbe_napi_enable_all(adapter);
1443
1444 /* clear any pending interrupts, may auto mask */
1445 IXGBE_READ_REG(hw, IXGBE_EICR);
1446
Auke Kok9a799d72007-09-15 14:07:45 -07001447 ixgbe_irq_enable(adapter);
1448
1449 /* bring the link up in the watchdog, this could race with our first
1450 * link up interrupt but shouldn't be a problem */
1451 mod_timer(&adapter->watchdog_timer, jiffies);
1452 return 0;
1453}
1454
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08001455void ixgbe_reinit_locked(struct ixgbe_adapter *adapter)
1456{
1457 WARN_ON(in_interrupt());
1458 while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
1459 msleep(1);
1460 ixgbe_down(adapter);
1461 ixgbe_up(adapter);
1462 clear_bit(__IXGBE_RESETTING, &adapter->state);
1463}
1464
Auke Kok9a799d72007-09-15 14:07:45 -07001465int ixgbe_up(struct ixgbe_adapter *adapter)
1466{
1467 /* hardware has been reset, we need to reload some things */
1468 ixgbe_configure(adapter);
1469
1470 return ixgbe_up_complete(adapter);
1471}
1472
1473void ixgbe_reset(struct ixgbe_adapter *adapter)
1474{
1475 if (ixgbe_init_hw(&adapter->hw))
1476 DPRINTK(PROBE, ERR, "Hardware Error\n");
1477
1478 /* reprogram the RAR[0] in case user changed it. */
1479 ixgbe_set_rar(&adapter->hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
1480
1481}
1482
1483#ifdef CONFIG_PM
1484static int ixgbe_resume(struct pci_dev *pdev)
1485{
1486 struct net_device *netdev = pci_get_drvdata(pdev);
1487 struct ixgbe_adapter *adapter = netdev_priv(netdev);
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001488 u32 err;
Auke Kok9a799d72007-09-15 14:07:45 -07001489
1490 pci_set_power_state(pdev, PCI_D0);
1491 pci_restore_state(pdev);
1492 err = pci_enable_device(pdev);
1493 if (err) {
1494 printk(KERN_ERR "ixgbe: Cannot enable PCI device from " \
1495 "suspend\n");
1496 return err;
1497 }
1498 pci_set_master(pdev);
1499
1500 pci_enable_wake(pdev, PCI_D3hot, 0);
1501 pci_enable_wake(pdev, PCI_D3cold, 0);
1502
1503 if (netif_running(netdev)) {
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001504 err = ixgbe_request_irq(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07001505 if (err)
1506 return err;
1507 }
1508
1509 ixgbe_reset(adapter);
1510
1511 if (netif_running(netdev))
1512 ixgbe_up(adapter);
1513
1514 netif_device_attach(netdev);
1515
1516 return 0;
1517}
1518#endif
1519
1520/**
1521 * ixgbe_clean_rx_ring - Free Rx Buffers per Queue
1522 * @adapter: board private structure
1523 * @rx_ring: ring to free buffers from
1524 **/
1525static void ixgbe_clean_rx_ring(struct ixgbe_adapter *adapter,
1526 struct ixgbe_ring *rx_ring)
1527{
1528 struct pci_dev *pdev = adapter->pdev;
1529 unsigned long size;
1530 unsigned int i;
1531
1532 /* Free all the Rx ring sk_buffs */
1533
1534 for (i = 0; i < rx_ring->count; i++) {
1535 struct ixgbe_rx_buffer *rx_buffer_info;
1536
1537 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1538 if (rx_buffer_info->dma) {
1539 pci_unmap_single(pdev, rx_buffer_info->dma,
1540 adapter->rx_buf_len,
1541 PCI_DMA_FROMDEVICE);
1542 rx_buffer_info->dma = 0;
1543 }
1544 if (rx_buffer_info->skb) {
1545 dev_kfree_skb(rx_buffer_info->skb);
1546 rx_buffer_info->skb = NULL;
1547 }
1548 if (!rx_buffer_info->page)
1549 continue;
1550 pci_unmap_page(pdev, rx_buffer_info->page_dma, PAGE_SIZE,
1551 PCI_DMA_FROMDEVICE);
1552 rx_buffer_info->page_dma = 0;
1553
1554 put_page(rx_buffer_info->page);
1555 rx_buffer_info->page = NULL;
1556 }
1557
1558 size = sizeof(struct ixgbe_rx_buffer) * rx_ring->count;
1559 memset(rx_ring->rx_buffer_info, 0, size);
1560
1561 /* Zero out the descriptor ring */
1562 memset(rx_ring->desc, 0, rx_ring->size);
1563
1564 rx_ring->next_to_clean = 0;
1565 rx_ring->next_to_use = 0;
1566
1567 writel(0, adapter->hw.hw_addr + rx_ring->head);
1568 writel(0, adapter->hw.hw_addr + rx_ring->tail);
1569}
1570
1571/**
1572 * ixgbe_clean_tx_ring - Free Tx Buffers
1573 * @adapter: board private structure
1574 * @tx_ring: ring to be cleaned
1575 **/
1576static void ixgbe_clean_tx_ring(struct ixgbe_adapter *adapter,
1577 struct ixgbe_ring *tx_ring)
1578{
1579 struct ixgbe_tx_buffer *tx_buffer_info;
1580 unsigned long size;
1581 unsigned int i;
1582
1583 /* Free all the Tx ring sk_buffs */
1584
1585 for (i = 0; i < tx_ring->count; i++) {
1586 tx_buffer_info = &tx_ring->tx_buffer_info[i];
1587 ixgbe_unmap_and_free_tx_resource(adapter, tx_buffer_info);
1588 }
1589
1590 size = sizeof(struct ixgbe_tx_buffer) * tx_ring->count;
1591 memset(tx_ring->tx_buffer_info, 0, size);
1592
1593 /* Zero out the descriptor ring */
1594 memset(tx_ring->desc, 0, tx_ring->size);
1595
1596 tx_ring->next_to_use = 0;
1597 tx_ring->next_to_clean = 0;
1598
1599 writel(0, adapter->hw.hw_addr + tx_ring->head);
1600 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1601}
1602
1603/**
Auke Kok9a799d72007-09-15 14:07:45 -07001604 * ixgbe_clean_all_rx_rings - Free Rx Buffers for all queues
1605 * @adapter: board private structure
1606 **/
1607static void ixgbe_clean_all_rx_rings(struct ixgbe_adapter *adapter)
1608{
1609 int i;
1610
1611 for (i = 0; i < adapter->num_rx_queues; i++)
1612 ixgbe_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1613}
1614
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001615/**
1616 * ixgbe_clean_all_tx_rings - Free Tx Buffers for all queues
1617 * @adapter: board private structure
1618 **/
1619static void ixgbe_clean_all_tx_rings(struct ixgbe_adapter *adapter)
1620{
1621 int i;
1622
1623 for (i = 0; i < adapter->num_tx_queues; i++)
1624 ixgbe_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1625}
1626
Auke Kok9a799d72007-09-15 14:07:45 -07001627void ixgbe_down(struct ixgbe_adapter *adapter)
1628{
1629 struct net_device *netdev = adapter->netdev;
1630 u32 rxctrl;
1631
1632 /* signal that we are down to the interrupt handler */
1633 set_bit(__IXGBE_DOWN, &adapter->state);
1634
1635 /* disable receives */
1636 rxctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_RXCTRL);
1637 IXGBE_WRITE_REG(&adapter->hw, IXGBE_RXCTRL,
1638 rxctrl & ~IXGBE_RXCTRL_RXEN);
1639
1640 netif_tx_disable(netdev);
1641
1642 /* disable transmits in the hardware */
1643
1644 /* flush both disables */
1645 IXGBE_WRITE_FLUSH(&adapter->hw);
1646 msleep(10);
1647
1648 ixgbe_irq_disable(adapter);
1649
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001650 ixgbe_napi_disable_all(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07001651 del_timer_sync(&adapter->watchdog_timer);
1652
1653 netif_carrier_off(netdev);
1654 netif_stop_queue(netdev);
1655
1656 ixgbe_reset(adapter);
1657 ixgbe_clean_all_tx_rings(adapter);
1658 ixgbe_clean_all_rx_rings(adapter);
1659
1660}
1661
1662static int ixgbe_suspend(struct pci_dev *pdev, pm_message_t state)
1663{
1664 struct net_device *netdev = pci_get_drvdata(pdev);
1665 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1666#ifdef CONFIG_PM
1667 int retval = 0;
1668#endif
1669
1670 netif_device_detach(netdev);
1671
1672 if (netif_running(netdev)) {
1673 ixgbe_down(adapter);
1674 ixgbe_free_irq(adapter);
1675 }
1676
1677#ifdef CONFIG_PM
1678 retval = pci_save_state(pdev);
1679 if (retval)
1680 return retval;
1681#endif
1682
1683 pci_enable_wake(pdev, PCI_D3hot, 0);
1684 pci_enable_wake(pdev, PCI_D3cold, 0);
1685
Ayyappan Veeraiyan5eba3692008-02-01 15:59:04 -08001686 ixgbe_release_hw_control(adapter);
1687
Auke Kok9a799d72007-09-15 14:07:45 -07001688 pci_disable_device(pdev);
1689
1690 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1691
1692 return 0;
1693}
1694
1695static void ixgbe_shutdown(struct pci_dev *pdev)
1696{
1697 ixgbe_suspend(pdev, PMSG_SUSPEND);
1698}
1699
1700/**
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001701 * ixgbe_poll - NAPI Rx polling callback
1702 * @napi: structure for representing this polling device
1703 * @budget: how many packets driver is allowed to clean
1704 *
1705 * This function is used for legacy and MSI, NAPI mode
Auke Kok9a799d72007-09-15 14:07:45 -07001706 **/
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001707static int ixgbe_poll(struct napi_struct *napi, int budget)
Auke Kok9a799d72007-09-15 14:07:45 -07001708{
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001709 struct ixgbe_q_vector *q_vector = container_of(napi,
1710 struct ixgbe_q_vector, napi);
1711 struct ixgbe_adapter *adapter = q_vector->adapter;
David S. Millerd2c7ddd2008-01-15 22:43:24 -08001712 int tx_cleaned = 0, work_done = 0;
Auke Kok9a799d72007-09-15 14:07:45 -07001713
David S. Millerd2c7ddd2008-01-15 22:43:24 -08001714 tx_cleaned = ixgbe_clean_tx_irq(adapter, adapter->tx_ring);
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001715 ixgbe_clean_rx_irq(adapter, adapter->rx_ring, &work_done, budget);
Auke Kok9a799d72007-09-15 14:07:45 -07001716
David S. Millerd2c7ddd2008-01-15 22:43:24 -08001717 if (tx_cleaned)
1718 work_done = budget;
1719
David S. Miller53e52c72008-01-07 21:06:12 -08001720 /* If budget not fully consumed, exit the polling mode */
1721 if (work_done < budget) {
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001722 netif_rx_complete(adapter->netdev, napi);
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08001723 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1724 ixgbe_irq_enable(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07001725 }
1726
1727 return work_done;
1728}
1729
1730/**
1731 * ixgbe_tx_timeout - Respond to a Tx Hang
1732 * @netdev: network interface device structure
1733 **/
1734static void ixgbe_tx_timeout(struct net_device *netdev)
1735{
1736 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1737
1738 /* Do the reset outside of interrupt context */
1739 schedule_work(&adapter->reset_task);
1740}
1741
1742static void ixgbe_reset_task(struct work_struct *work)
1743{
1744 struct ixgbe_adapter *adapter;
1745 adapter = container_of(work, struct ixgbe_adapter, reset_task);
1746
1747 adapter->tx_timeout_count++;
1748
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08001749 ixgbe_reinit_locked(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07001750}
1751
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001752static void ixgbe_acquire_msix_vectors(struct ixgbe_adapter *adapter,
1753 int vectors)
1754{
1755 int err, vector_threshold;
1756
1757 /* We'll want at least 3 (vector_threshold):
1758 * 1) TxQ[0] Cleanup
1759 * 2) RxQ[0] Cleanup
1760 * 3) Other (Link Status Change, etc.)
1761 * 4) TCP Timer (optional)
1762 */
1763 vector_threshold = MIN_MSIX_COUNT;
1764
1765 /* The more we get, the more we will assign to Tx/Rx Cleanup
1766 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1767 * Right now, we simply care about how many we'll get; we'll
1768 * set them up later while requesting irq's.
1769 */
1770 while (vectors >= vector_threshold) {
1771 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1772 vectors);
1773 if (!err) /* Success in acquiring all requested vectors. */
1774 break;
1775 else if (err < 0)
1776 vectors = 0; /* Nasty failure, quit now */
1777 else /* err == number of vectors we should try again with */
1778 vectors = err;
1779 }
1780
1781 if (vectors < vector_threshold) {
1782 /* Can't allocate enough MSI-X interrupts? Oh well.
1783 * This just means we'll go with either a single MSI
1784 * vector or fall back to legacy interrupts.
1785 */
1786 DPRINTK(HW, DEBUG, "Unable to allocate MSI-X interrupts\n");
1787 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
1788 kfree(adapter->msix_entries);
1789 adapter->msix_entries = NULL;
1790 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
1791 adapter->num_tx_queues = 1;
1792 adapter->num_rx_queues = 1;
1793 } else {
1794 adapter->flags |= IXGBE_FLAG_MSIX_ENABLED; /* Woot! */
1795 adapter->num_msix_vectors = vectors;
1796 }
1797}
1798
1799static void __devinit ixgbe_set_num_queues(struct ixgbe_adapter *adapter)
1800{
1801 int nrq, ntq;
1802 int feature_mask = 0, rss_i, rss_m;
1803
1804 /* Number of supported queues */
1805 switch (adapter->hw.mac.type) {
1806 case ixgbe_mac_82598EB:
1807 rss_i = adapter->ring_feature[RING_F_RSS].indices;
1808 rss_m = 0;
1809 feature_mask |= IXGBE_FLAG_RSS_ENABLED;
1810
1811 switch (adapter->flags & feature_mask) {
1812 case (IXGBE_FLAG_RSS_ENABLED):
1813 rss_m = 0xF;
1814 nrq = rss_i;
1815 ntq = 1;
1816 break;
1817 case 0:
1818 default:
1819 rss_i = 0;
1820 rss_m = 0;
1821 nrq = 1;
1822 ntq = 1;
1823 break;
1824 }
1825
1826 adapter->ring_feature[RING_F_RSS].indices = rss_i;
1827 adapter->ring_feature[RING_F_RSS].mask = rss_m;
1828 break;
1829 default:
1830 nrq = 1;
1831 ntq = 1;
1832 break;
1833 }
1834
1835 adapter->num_rx_queues = nrq;
1836 adapter->num_tx_queues = ntq;
1837}
1838
1839/**
1840 * ixgbe_cache_ring_register - Descriptor ring to register mapping
1841 * @adapter: board private structure to initialize
1842 *
1843 * Once we know the feature-set enabled for the device, we'll cache
1844 * the register offset the descriptor ring is assigned to.
1845 **/
1846static void __devinit ixgbe_cache_ring_register(struct ixgbe_adapter *adapter)
1847{
1848 /* TODO: Remove all uses of the indices in the cases where multiple
1849 * features are OR'd together, if the feature set makes sense.
1850 */
1851 int feature_mask = 0, rss_i;
1852 int i, txr_idx, rxr_idx;
1853
1854 /* Number of supported queues */
1855 switch (adapter->hw.mac.type) {
1856 case ixgbe_mac_82598EB:
1857 rss_i = adapter->ring_feature[RING_F_RSS].indices;
1858 txr_idx = 0;
1859 rxr_idx = 0;
1860 feature_mask |= IXGBE_FLAG_RSS_ENABLED;
1861 switch (adapter->flags & feature_mask) {
1862 case (IXGBE_FLAG_RSS_ENABLED):
1863 for (i = 0; i < adapter->num_rx_queues; i++)
1864 adapter->rx_ring[i].reg_idx = i;
1865 for (i = 0; i < adapter->num_tx_queues; i++)
1866 adapter->tx_ring[i].reg_idx = i;
1867 break;
1868 case 0:
1869 default:
1870 break;
1871 }
1872 break;
1873 default:
1874 break;
1875 }
1876}
1877
Auke Kok9a799d72007-09-15 14:07:45 -07001878/**
1879 * ixgbe_alloc_queues - Allocate memory for all rings
1880 * @adapter: board private structure to initialize
1881 *
1882 * We allocate one ring per queue at run-time since we don't know the
1883 * number of queues at compile-time. The polling_netdev array is
1884 * intended for Multiqueue, but should work fine with a single queue.
1885 **/
1886static int __devinit ixgbe_alloc_queues(struct ixgbe_adapter *adapter)
1887{
1888 int i;
1889
1890 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1891 sizeof(struct ixgbe_ring), GFP_KERNEL);
1892 if (!adapter->tx_ring)
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001893 goto err_tx_ring_allocation;
Auke Kok9a799d72007-09-15 14:07:45 -07001894
1895 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1896 sizeof(struct ixgbe_ring), GFP_KERNEL);
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001897 if (!adapter->rx_ring)
1898 goto err_rx_ring_allocation;
1899
1900 for (i = 0; i < adapter->num_tx_queues; i++) {
1901 adapter->tx_ring[i].count = IXGBE_DEFAULT_TXD;
1902 adapter->tx_ring[i].queue_index = i;
1903 }
1904 for (i = 0; i < adapter->num_rx_queues; i++) {
1905 adapter->rx_ring[i].count = IXGBE_DEFAULT_RXD;
1906 adapter->rx_ring[i].queue_index = i;
Auke Kok9a799d72007-09-15 14:07:45 -07001907 }
1908
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001909 ixgbe_cache_ring_register(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07001910
1911 return 0;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001912
1913err_rx_ring_allocation:
1914 kfree(adapter->tx_ring);
1915err_tx_ring_allocation:
1916 return -ENOMEM;
1917}
1918
1919/**
1920 * ixgbe_set_interrupt_capability - set MSI-X or MSI if supported
1921 * @adapter: board private structure to initialize
1922 *
1923 * Attempt to configure the interrupts using the best available
1924 * capabilities of the hardware and the kernel.
1925 **/
1926static int __devinit ixgbe_set_interrupt_capability(struct ixgbe_adapter
1927 *adapter)
1928{
1929 int err = 0;
1930 int vector, v_budget;
1931
1932 /*
1933 * It's easy to be greedy for MSI-X vectors, but it really
1934 * doesn't do us much good if we have a lot more vectors
1935 * than CPU's. So let's be conservative and only ask for
1936 * (roughly) twice the number of vectors as there are CPU's.
1937 */
1938 v_budget = min(adapter->num_rx_queues + adapter->num_tx_queues,
1939 (int)(num_online_cpus() * 2)) + NON_Q_VECTORS;
1940
1941 /*
1942 * At the same time, hardware can only support a maximum of
1943 * MAX_MSIX_COUNT vectors. With features such as RSS and VMDq,
1944 * we can easily reach upwards of 64 Rx descriptor queues and
1945 * 32 Tx queues. Thus, we cap it off in those rare cases where
1946 * the cpu count also exceeds our vector limit.
1947 */
1948 v_budget = min(v_budget, MAX_MSIX_COUNT);
1949
1950 /* A failure in MSI-X entry allocation isn't fatal, but it does
1951 * mean we disable MSI-X capabilities of the adapter. */
1952 adapter->msix_entries = kcalloc(v_budget,
1953 sizeof(struct msix_entry), GFP_KERNEL);
1954 if (!adapter->msix_entries) {
1955 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
1956 ixgbe_set_num_queues(adapter);
1957 kfree(adapter->tx_ring);
1958 kfree(adapter->rx_ring);
1959 err = ixgbe_alloc_queues(adapter);
1960 if (err) {
1961 DPRINTK(PROBE, ERR, "Unable to allocate memory "
1962 "for queues\n");
1963 goto out;
1964 }
1965
1966 goto try_msi;
1967 }
1968
1969 for (vector = 0; vector < v_budget; vector++)
1970 adapter->msix_entries[vector].entry = vector;
1971
1972 ixgbe_acquire_msix_vectors(adapter, v_budget);
1973
1974 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
1975 goto out;
1976
1977try_msi:
1978 err = pci_enable_msi(adapter->pdev);
1979 if (!err) {
1980 adapter->flags |= IXGBE_FLAG_MSI_ENABLED;
1981 } else {
1982 DPRINTK(HW, DEBUG, "Unable to allocate MSI interrupt, "
1983 "falling back to legacy. Error: %d\n", err);
1984 /* reset err */
1985 err = 0;
1986 }
1987
1988out:
1989
1990 return err;
1991}
1992
1993static void ixgbe_reset_interrupt_capability(struct ixgbe_adapter *adapter)
1994{
1995 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
1996 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
1997 pci_disable_msix(adapter->pdev);
1998 kfree(adapter->msix_entries);
1999 adapter->msix_entries = NULL;
2000 } else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED) {
2001 adapter->flags &= ~IXGBE_FLAG_MSI_ENABLED;
2002 pci_disable_msi(adapter->pdev);
2003 }
2004 return;
2005}
2006
2007/**
2008 * ixgbe_init_interrupt_scheme - Determine proper interrupt scheme
2009 * @adapter: board private structure to initialize
2010 *
2011 * We determine which interrupt scheme to use based on...
2012 * - Kernel support (MSI, MSI-X)
2013 * - which can be user-defined (via MODULE_PARAM)
2014 * - Hardware queue count (num_*_queues)
2015 * - defined by miscellaneous hardware support/features (RSS, etc.)
2016 **/
2017static int __devinit ixgbe_init_interrupt_scheme(struct ixgbe_adapter *adapter)
2018{
2019 int err;
2020
2021 /* Number of supported queues */
2022 ixgbe_set_num_queues(adapter);
2023
2024 err = ixgbe_alloc_queues(adapter);
2025 if (err) {
2026 DPRINTK(PROBE, ERR, "Unable to allocate memory for queues\n");
2027 goto err_alloc_queues;
2028 }
2029
2030 err = ixgbe_set_interrupt_capability(adapter);
2031 if (err) {
2032 DPRINTK(PROBE, ERR, "Unable to setup interrupt capabilities\n");
2033 goto err_set_interrupt;
2034 }
2035
2036 DPRINTK(DRV, INFO, "Multiqueue %s: Rx Queue count = %u, "
2037 "Tx Queue count = %u\n",
2038 (adapter->num_rx_queues > 1) ? "Enabled" :
2039 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
2040
2041 set_bit(__IXGBE_DOWN, &adapter->state);
2042
2043 return 0;
2044
2045err_set_interrupt:
2046 kfree(adapter->tx_ring);
2047 kfree(adapter->rx_ring);
2048err_alloc_queues:
2049 return err;
Auke Kok9a799d72007-09-15 14:07:45 -07002050}
2051
2052/**
2053 * ixgbe_sw_init - Initialize general software structures (struct ixgbe_adapter)
2054 * @adapter: board private structure to initialize
2055 *
2056 * ixgbe_sw_init initializes the Adapter private data structure.
2057 * Fields are initialized based on PCI device information and
2058 * OS network device settings (MTU size).
2059 **/
2060static int __devinit ixgbe_sw_init(struct ixgbe_adapter *adapter)
2061{
2062 struct ixgbe_hw *hw = &adapter->hw;
2063 struct pci_dev *pdev = adapter->pdev;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002064 unsigned int rss;
2065
2066 /* Set capability flags */
2067 rss = min(IXGBE_MAX_RSS_INDICES, (int)num_online_cpus());
2068 adapter->ring_feature[RING_F_RSS].indices = rss;
2069 adapter->flags |= IXGBE_FLAG_RSS_ENABLED;
Auke Kok9a799d72007-09-15 14:07:45 -07002070
2071 /* default flow control settings */
2072 hw->fc.original_type = ixgbe_fc_full;
2073 hw->fc.type = ixgbe_fc_full;
2074
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002075 /* select 10G link by default */
Auke Kok9a799d72007-09-15 14:07:45 -07002076 hw->mac.link_mode_select = IXGBE_AUTOC_LMS_10G_LINK_NO_AN;
2077 if (hw->mac.ops.reset(hw)) {
2078 dev_err(&pdev->dev, "HW Init failed\n");
2079 return -EIO;
2080 }
Auke Kok3957d632007-10-31 15:22:10 -07002081 if (hw->mac.ops.setup_link_speed(hw, IXGBE_LINK_SPEED_10GB_FULL, true,
2082 false)) {
Auke Kok9a799d72007-09-15 14:07:45 -07002083 dev_err(&pdev->dev, "Link Speed setup failed\n");
2084 return -EIO;
2085 }
2086
2087 /* initialize eeprom parameters */
2088 if (ixgbe_init_eeprom(hw)) {
2089 dev_err(&pdev->dev, "EEPROM initialization failed\n");
2090 return -EIO;
2091 }
2092
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002093 /* enable rx csum by default */
Auke Kok9a799d72007-09-15 14:07:45 -07002094 adapter->flags |= IXGBE_FLAG_RX_CSUM_ENABLED;
2095
Auke Kok9a799d72007-09-15 14:07:45 -07002096 set_bit(__IXGBE_DOWN, &adapter->state);
2097
2098 return 0;
2099}
2100
2101/**
2102 * ixgbe_setup_tx_resources - allocate Tx resources (Descriptors)
2103 * @adapter: board private structure
2104 * @txdr: tx descriptor ring (for a specific queue) to setup
2105 *
2106 * Return 0 on success, negative on failure
2107 **/
2108int ixgbe_setup_tx_resources(struct ixgbe_adapter *adapter,
2109 struct ixgbe_ring *txdr)
2110{
2111 struct pci_dev *pdev = adapter->pdev;
2112 int size;
2113
2114 size = sizeof(struct ixgbe_tx_buffer) * txdr->count;
2115 txdr->tx_buffer_info = vmalloc(size);
2116 if (!txdr->tx_buffer_info) {
2117 DPRINTK(PROBE, ERR,
2118 "Unable to allocate memory for the transmit descriptor ring\n");
2119 return -ENOMEM;
2120 }
2121 memset(txdr->tx_buffer_info, 0, size);
2122
2123 /* round up to nearest 4K */
2124 txdr->size = txdr->count * sizeof(union ixgbe_adv_tx_desc);
2125 txdr->size = ALIGN(txdr->size, 4096);
2126
2127 txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
2128 if (!txdr->desc) {
2129 vfree(txdr->tx_buffer_info);
2130 DPRINTK(PROBE, ERR,
2131 "Memory allocation failed for the tx desc ring\n");
2132 return -ENOMEM;
2133 }
2134
Auke Kok9a799d72007-09-15 14:07:45 -07002135 txdr->next_to_use = 0;
2136 txdr->next_to_clean = 0;
2137 txdr->work_limit = txdr->count;
Auke Kok9a799d72007-09-15 14:07:45 -07002138
2139 return 0;
2140}
2141
2142/**
2143 * ixgbe_setup_rx_resources - allocate Rx resources (Descriptors)
2144 * @adapter: board private structure
2145 * @rxdr: rx descriptor ring (for a specific queue) to setup
2146 *
2147 * Returns 0 on success, negative on failure
2148 **/
2149int ixgbe_setup_rx_resources(struct ixgbe_adapter *adapter,
2150 struct ixgbe_ring *rxdr)
2151{
2152 struct pci_dev *pdev = adapter->pdev;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002153 int size;
Auke Kok9a799d72007-09-15 14:07:45 -07002154
2155 size = sizeof(struct ixgbe_rx_buffer) * rxdr->count;
2156 rxdr->rx_buffer_info = vmalloc(size);
2157 if (!rxdr->rx_buffer_info) {
2158 DPRINTK(PROBE, ERR,
2159 "vmalloc allocation failed for the rx desc ring\n");
2160 return -ENOMEM;
2161 }
2162 memset(rxdr->rx_buffer_info, 0, size);
2163
Auke Kok9a799d72007-09-15 14:07:45 -07002164 /* Round up to nearest 4K */
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002165 rxdr->size = rxdr->count * sizeof(union ixgbe_adv_rx_desc);
Auke Kok9a799d72007-09-15 14:07:45 -07002166 rxdr->size = ALIGN(rxdr->size, 4096);
2167
2168 rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
2169
2170 if (!rxdr->desc) {
2171 DPRINTK(PROBE, ERR,
2172 "Memory allocation failed for the rx desc ring\n");
2173 vfree(rxdr->rx_buffer_info);
2174 return -ENOMEM;
2175 }
2176
2177 rxdr->next_to_clean = 0;
2178 rxdr->next_to_use = 0;
Auke Kok9a799d72007-09-15 14:07:45 -07002179
2180 return 0;
2181}
2182
2183/**
2184 * ixgbe_free_tx_resources - Free Tx Resources per Queue
2185 * @adapter: board private structure
2186 * @tx_ring: Tx descriptor ring for a specific queue
2187 *
2188 * Free all transmit software resources
2189 **/
2190static void ixgbe_free_tx_resources(struct ixgbe_adapter *adapter,
2191 struct ixgbe_ring *tx_ring)
2192{
2193 struct pci_dev *pdev = adapter->pdev;
2194
2195 ixgbe_clean_tx_ring(adapter, tx_ring);
2196
2197 vfree(tx_ring->tx_buffer_info);
2198 tx_ring->tx_buffer_info = NULL;
2199
2200 pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
2201
2202 tx_ring->desc = NULL;
2203}
2204
2205/**
2206 * ixgbe_free_all_tx_resources - Free Tx Resources for All Queues
2207 * @adapter: board private structure
2208 *
2209 * Free all transmit software resources
2210 **/
2211static void ixgbe_free_all_tx_resources(struct ixgbe_adapter *adapter)
2212{
2213 int i;
2214
2215 for (i = 0; i < adapter->num_tx_queues; i++)
2216 ixgbe_free_tx_resources(adapter, &adapter->tx_ring[i]);
2217}
2218
2219/**
2220 * ixgbe_free_rx_resources - Free Rx Resources
2221 * @adapter: board private structure
2222 * @rx_ring: ring to clean the resources from
2223 *
2224 * Free all receive software resources
2225 **/
2226static void ixgbe_free_rx_resources(struct ixgbe_adapter *adapter,
2227 struct ixgbe_ring *rx_ring)
2228{
2229 struct pci_dev *pdev = adapter->pdev;
2230
2231 ixgbe_clean_rx_ring(adapter, rx_ring);
2232
2233 vfree(rx_ring->rx_buffer_info);
2234 rx_ring->rx_buffer_info = NULL;
2235
2236 pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
2237
2238 rx_ring->desc = NULL;
2239}
2240
2241/**
2242 * ixgbe_free_all_rx_resources - Free Rx Resources for All Queues
2243 * @adapter: board private structure
2244 *
2245 * Free all receive software resources
2246 **/
2247static void ixgbe_free_all_rx_resources(struct ixgbe_adapter *adapter)
2248{
2249 int i;
2250
2251 for (i = 0; i < adapter->num_rx_queues; i++)
2252 ixgbe_free_rx_resources(adapter, &adapter->rx_ring[i]);
2253}
2254
2255/**
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002256 * ixgbe_setup_all_tx_resources - allocate all queues Tx resources
Auke Kok9a799d72007-09-15 14:07:45 -07002257 * @adapter: board private structure
2258 *
2259 * If this function returns with an error, then it's possible one or
2260 * more of the rings is populated (while the rest are not). It is the
2261 * callers duty to clean those orphaned rings.
2262 *
2263 * Return 0 on success, negative on failure
2264 **/
2265static int ixgbe_setup_all_tx_resources(struct ixgbe_adapter *adapter)
2266{
2267 int i, err = 0;
2268
2269 for (i = 0; i < adapter->num_tx_queues; i++) {
2270 err = ixgbe_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2271 if (err) {
2272 DPRINTK(PROBE, ERR,
2273 "Allocation for Tx Queue %u failed\n", i);
2274 break;
2275 }
2276 }
2277
2278 return err;
2279}
2280
2281/**
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002282 * ixgbe_setup_all_rx_resources - allocate all queues Rx resources
Auke Kok9a799d72007-09-15 14:07:45 -07002283 * @adapter: board private structure
2284 *
2285 * If this function returns with an error, then it's possible one or
2286 * more of the rings is populated (while the rest are not). It is the
2287 * callers duty to clean those orphaned rings.
2288 *
2289 * Return 0 on success, negative on failure
2290 **/
2291
2292static int ixgbe_setup_all_rx_resources(struct ixgbe_adapter *adapter)
2293{
2294 int i, err = 0;
2295
2296 for (i = 0; i < adapter->num_rx_queues; i++) {
2297 err = ixgbe_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2298 if (err) {
2299 DPRINTK(PROBE, ERR,
2300 "Allocation for Rx Queue %u failed\n", i);
2301 break;
2302 }
2303 }
2304
2305 return err;
2306}
2307
2308/**
2309 * ixgbe_change_mtu - Change the Maximum Transfer Unit
2310 * @netdev: network interface device structure
2311 * @new_mtu: new value for maximum frame size
2312 *
2313 * Returns 0 on success, negative on failure
2314 **/
2315static int ixgbe_change_mtu(struct net_device *netdev, int new_mtu)
2316{
2317 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2318 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2319
2320 if ((max_frame < (ETH_ZLEN + ETH_FCS_LEN)) ||
2321 (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE))
2322 return -EINVAL;
2323
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002324 DPRINTK(PROBE, INFO, "changing MTU from %d to %d\n",
2325 netdev->mtu, new_mtu);
2326 /* must set new MTU before calling down or up */
Auke Kok9a799d72007-09-15 14:07:45 -07002327 netdev->mtu = new_mtu;
2328
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08002329 if (netif_running(netdev))
2330 ixgbe_reinit_locked(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07002331
2332 return 0;
2333}
2334
2335/**
2336 * ixgbe_open - Called when a network interface is made active
2337 * @netdev: network interface device structure
2338 *
2339 * Returns 0 on success, negative value on failure
2340 *
2341 * The open entry point is called when a network interface is made
2342 * active by the system (IFF_UP). At this point all resources needed
2343 * for transmit and receive operations are allocated, the interrupt
2344 * handler is registered with the OS, the watchdog timer is started,
2345 * and the stack is notified that the interface is ready.
2346 **/
2347static int ixgbe_open(struct net_device *netdev)
2348{
2349 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2350 int err;
Auke Kok9a799d72007-09-15 14:07:45 -07002351
Auke Kok4bebfaa2008-02-11 09:26:01 -08002352 /* disallow open during test */
2353 if (test_bit(__IXGBE_TESTING, &adapter->state))
2354 return -EBUSY;
2355
Auke Kok9a799d72007-09-15 14:07:45 -07002356 /* allocate transmit descriptors */
2357 err = ixgbe_setup_all_tx_resources(adapter);
2358 if (err)
2359 goto err_setup_tx;
2360
Auke Kok9a799d72007-09-15 14:07:45 -07002361 /* allocate receive descriptors */
2362 err = ixgbe_setup_all_rx_resources(adapter);
2363 if (err)
2364 goto err_setup_rx;
2365
2366 ixgbe_configure(adapter);
2367
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002368 err = ixgbe_request_irq(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07002369 if (err)
2370 goto err_req_irq;
2371
Auke Kok9a799d72007-09-15 14:07:45 -07002372 err = ixgbe_up_complete(adapter);
2373 if (err)
2374 goto err_up;
2375
2376 return 0;
2377
2378err_up:
Ayyappan Veeraiyan5eba3692008-02-01 15:59:04 -08002379 ixgbe_release_hw_control(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07002380 ixgbe_free_irq(adapter);
2381err_req_irq:
2382 ixgbe_free_all_rx_resources(adapter);
2383err_setup_rx:
2384 ixgbe_free_all_tx_resources(adapter);
2385err_setup_tx:
2386 ixgbe_reset(adapter);
2387
2388 return err;
2389}
2390
2391/**
2392 * ixgbe_close - Disables a network interface
2393 * @netdev: network interface device structure
2394 *
2395 * Returns 0, this is not allowed to fail
2396 *
2397 * The close entry point is called when an interface is de-activated
2398 * by the OS. The hardware is still under the drivers control, but
2399 * needs to be disabled. A global MAC reset is issued to stop the
2400 * hardware, and all transmit and receive resources are freed.
2401 **/
2402static int ixgbe_close(struct net_device *netdev)
2403{
2404 struct ixgbe_adapter *adapter = netdev_priv(netdev);
Auke Kok9a799d72007-09-15 14:07:45 -07002405
2406 ixgbe_down(adapter);
2407 ixgbe_free_irq(adapter);
2408
2409 ixgbe_free_all_tx_resources(adapter);
2410 ixgbe_free_all_rx_resources(adapter);
2411
Ayyappan Veeraiyan5eba3692008-02-01 15:59:04 -08002412 ixgbe_release_hw_control(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07002413
2414 return 0;
2415}
2416
2417/**
2418 * ixgbe_update_stats - Update the board statistics counters.
2419 * @adapter: board private structure
2420 **/
2421void ixgbe_update_stats(struct ixgbe_adapter *adapter)
2422{
2423 struct ixgbe_hw *hw = &adapter->hw;
Ayyappan Veeraiyan6f11eef2008-02-01 15:59:14 -08002424 u64 total_mpc = 0;
2425 u32 i, missed_rx = 0, mpc, bprc, lxon, lxoff, xon_off_tot;
Auke Kok9a799d72007-09-15 14:07:45 -07002426
2427 adapter->stats.crcerrs += IXGBE_READ_REG(hw, IXGBE_CRCERRS);
Ayyappan Veeraiyan6f11eef2008-02-01 15:59:14 -08002428 for (i = 0; i < 8; i++) {
2429 /* for packet buffers not used, the register should read 0 */
2430 mpc = IXGBE_READ_REG(hw, IXGBE_MPC(i));
2431 missed_rx += mpc;
2432 adapter->stats.mpc[i] += mpc;
2433 total_mpc += adapter->stats.mpc[i];
2434 adapter->stats.rnbc[i] += IXGBE_READ_REG(hw, IXGBE_RNBC(i));
2435 }
2436 adapter->stats.gprc += IXGBE_READ_REG(hw, IXGBE_GPRC);
2437 /* work around hardware counting issue */
2438 adapter->stats.gprc -= missed_rx;
Auke Kok9a799d72007-09-15 14:07:45 -07002439
Ayyappan Veeraiyan6f11eef2008-02-01 15:59:14 -08002440 /* 82598 hardware only has a 32 bit counter in the high register */
Auke Kok9a799d72007-09-15 14:07:45 -07002441 adapter->stats.gorc += IXGBE_READ_REG(hw, IXGBE_GORCH);
Ayyappan Veeraiyan6f11eef2008-02-01 15:59:14 -08002442 adapter->stats.gotc += IXGBE_READ_REG(hw, IXGBE_GOTCH);
2443 adapter->stats.tor += IXGBE_READ_REG(hw, IXGBE_TORH);
Auke Kok9a799d72007-09-15 14:07:45 -07002444 bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
2445 adapter->stats.bprc += bprc;
2446 adapter->stats.mprc += IXGBE_READ_REG(hw, IXGBE_MPRC);
2447 adapter->stats.mprc -= bprc;
2448 adapter->stats.roc += IXGBE_READ_REG(hw, IXGBE_ROC);
2449 adapter->stats.prc64 += IXGBE_READ_REG(hw, IXGBE_PRC64);
2450 adapter->stats.prc127 += IXGBE_READ_REG(hw, IXGBE_PRC127);
2451 adapter->stats.prc255 += IXGBE_READ_REG(hw, IXGBE_PRC255);
2452 adapter->stats.prc511 += IXGBE_READ_REG(hw, IXGBE_PRC511);
2453 adapter->stats.prc1023 += IXGBE_READ_REG(hw, IXGBE_PRC1023);
2454 adapter->stats.prc1522 += IXGBE_READ_REG(hw, IXGBE_PRC1522);
Auke Kok9a799d72007-09-15 14:07:45 -07002455 adapter->stats.rlec += IXGBE_READ_REG(hw, IXGBE_RLEC);
2456 adapter->stats.lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
Auke Kok9a799d72007-09-15 14:07:45 -07002457 adapter->stats.lxoffrxc += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
Ayyappan Veeraiyan6f11eef2008-02-01 15:59:14 -08002458 lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
2459 adapter->stats.lxontxc += lxon;
2460 lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
2461 adapter->stats.lxofftxc += lxoff;
Auke Kok9a799d72007-09-15 14:07:45 -07002462 adapter->stats.ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
2463 adapter->stats.gptc += IXGBE_READ_REG(hw, IXGBE_GPTC);
Ayyappan Veeraiyan6f11eef2008-02-01 15:59:14 -08002464 adapter->stats.mptc += IXGBE_READ_REG(hw, IXGBE_MPTC);
2465 /*
2466 * 82598 errata - tx of flow control packets is included in tx counters
2467 */
2468 xon_off_tot = lxon + lxoff;
2469 adapter->stats.gptc -= xon_off_tot;
2470 adapter->stats.mptc -= xon_off_tot;
2471 adapter->stats.gotc -= (xon_off_tot * (ETH_ZLEN + ETH_FCS_LEN));
Auke Kok9a799d72007-09-15 14:07:45 -07002472 adapter->stats.ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
2473 adapter->stats.rfc += IXGBE_READ_REG(hw, IXGBE_RFC);
2474 adapter->stats.rjc += IXGBE_READ_REG(hw, IXGBE_RJC);
Auke Kok9a799d72007-09-15 14:07:45 -07002475 adapter->stats.tpr += IXGBE_READ_REG(hw, IXGBE_TPR);
2476 adapter->stats.ptc64 += IXGBE_READ_REG(hw, IXGBE_PTC64);
Ayyappan Veeraiyan6f11eef2008-02-01 15:59:14 -08002477 adapter->stats.ptc64 -= xon_off_tot;
Auke Kok9a799d72007-09-15 14:07:45 -07002478 adapter->stats.ptc127 += IXGBE_READ_REG(hw, IXGBE_PTC127);
2479 adapter->stats.ptc255 += IXGBE_READ_REG(hw, IXGBE_PTC255);
2480 adapter->stats.ptc511 += IXGBE_READ_REG(hw, IXGBE_PTC511);
2481 adapter->stats.ptc1023 += IXGBE_READ_REG(hw, IXGBE_PTC1023);
2482 adapter->stats.ptc1522 += IXGBE_READ_REG(hw, IXGBE_PTC1522);
Auke Kok9a799d72007-09-15 14:07:45 -07002483 adapter->stats.bptc += IXGBE_READ_REG(hw, IXGBE_BPTC);
2484
2485 /* Fill out the OS statistics structure */
Auke Kok9a799d72007-09-15 14:07:45 -07002486 adapter->net_stats.multicast = adapter->stats.mprc;
2487
2488 /* Rx Errors */
2489 adapter->net_stats.rx_errors = adapter->stats.crcerrs +
2490 adapter->stats.rlec;
2491 adapter->net_stats.rx_dropped = 0;
2492 adapter->net_stats.rx_length_errors = adapter->stats.rlec;
2493 adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
Ayyappan Veeraiyan6f11eef2008-02-01 15:59:14 -08002494 adapter->net_stats.rx_missed_errors = total_mpc;
Auke Kok9a799d72007-09-15 14:07:45 -07002495}
2496
2497/**
2498 * ixgbe_watchdog - Timer Call-back
2499 * @data: pointer to adapter cast into an unsigned long
2500 **/
2501static void ixgbe_watchdog(unsigned long data)
2502{
2503 struct ixgbe_adapter *adapter = (struct ixgbe_adapter *)data;
2504 struct net_device *netdev = adapter->netdev;
2505 bool link_up;
2506 u32 link_speed = 0;
2507
Auke Kok3957d632007-10-31 15:22:10 -07002508 adapter->hw.mac.ops.check_link(&adapter->hw, &(link_speed), &link_up);
Auke Kok9a799d72007-09-15 14:07:45 -07002509
2510 if (link_up) {
2511 if (!netif_carrier_ok(netdev)) {
2512 u32 frctl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
2513 u32 rmcs = IXGBE_READ_REG(&adapter->hw, IXGBE_RMCS);
2514#define FLOW_RX (frctl & IXGBE_FCTRL_RFCE)
2515#define FLOW_TX (rmcs & IXGBE_RMCS_TFCE_802_3X)
2516 DPRINTK(LINK, INFO, "NIC Link is Up %s, "
2517 "Flow Control: %s\n",
2518 (link_speed == IXGBE_LINK_SPEED_10GB_FULL ?
2519 "10 Gbps" :
2520 (link_speed == IXGBE_LINK_SPEED_1GB_FULL ?
Emil Tantilov5a059e92008-03-03 14:37:42 -08002521 "1 Gbps" : "unknown speed")),
Auke Kok9a799d72007-09-15 14:07:45 -07002522 ((FLOW_RX && FLOW_TX) ? "RX/TX" :
2523 (FLOW_RX ? "RX" :
2524 (FLOW_TX ? "TX" : "None"))));
2525
2526 netif_carrier_on(netdev);
2527 netif_wake_queue(netdev);
2528 } else {
2529 /* Force detection of hung controller */
2530 adapter->detect_tx_hung = true;
2531 }
2532 } else {
2533 if (netif_carrier_ok(netdev)) {
2534 DPRINTK(LINK, INFO, "NIC Link is Down\n");
2535 netif_carrier_off(netdev);
2536 netif_stop_queue(netdev);
2537 }
2538 }
2539
2540 ixgbe_update_stats(adapter);
2541
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002542 if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
2543 /* Cause software interrupt to ensure rx rings are cleaned */
2544 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
2545 u32 eics =
2546 (1 << (adapter->num_msix_vectors - NON_Q_VECTORS)) - 1;
2547 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS, eics);
2548 } else {
2549 /* for legacy and MSI interrupts don't set any bits that
2550 * are enabled for EIAM, because this operation would
2551 * set *both* EIMS and EICS for any bit in EIAM */
2552 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS,
2553 (IXGBE_EICS_TCP_TIMER | IXGBE_EICS_OTHER));
2554 }
2555 /* Reset the timer */
Auke Kok9a799d72007-09-15 14:07:45 -07002556 mod_timer(&adapter->watchdog_timer,
2557 round_jiffies(jiffies + 2 * HZ));
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002558 }
Auke Kok9a799d72007-09-15 14:07:45 -07002559}
2560
Auke Kok9a799d72007-09-15 14:07:45 -07002561static int ixgbe_tso(struct ixgbe_adapter *adapter,
2562 struct ixgbe_ring *tx_ring, struct sk_buff *skb,
2563 u32 tx_flags, u8 *hdr_len)
2564{
2565 struct ixgbe_adv_tx_context_desc *context_desc;
2566 unsigned int i;
2567 int err;
2568 struct ixgbe_tx_buffer *tx_buffer_info;
2569 u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
2570 u32 mss_l4len_idx = 0, l4len;
2571 *hdr_len = 0;
2572
2573 if (skb_is_gso(skb)) {
2574 if (skb_header_cloned(skb)) {
2575 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2576 if (err)
2577 return err;
2578 }
2579 l4len = tcp_hdrlen(skb);
2580 *hdr_len += l4len;
2581
Al Viro8327d002007-12-10 18:54:12 +00002582 if (skb->protocol == htons(ETH_P_IP)) {
Auke Kok9a799d72007-09-15 14:07:45 -07002583 struct iphdr *iph = ip_hdr(skb);
2584 iph->tot_len = 0;
2585 iph->check = 0;
2586 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2587 iph->daddr, 0,
2588 IPPROTO_TCP,
2589 0);
2590 adapter->hw_tso_ctxt++;
2591 } else if (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6) {
2592 ipv6_hdr(skb)->payload_len = 0;
2593 tcp_hdr(skb)->check =
2594 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2595 &ipv6_hdr(skb)->daddr,
2596 0, IPPROTO_TCP, 0);
2597 adapter->hw_tso6_ctxt++;
2598 }
2599
2600 i = tx_ring->next_to_use;
2601
2602 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2603 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
2604
2605 /* VLAN MACLEN IPLEN */
2606 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2607 vlan_macip_lens |=
2608 (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
2609 vlan_macip_lens |= ((skb_network_offset(skb)) <<
2610 IXGBE_ADVTXD_MACLEN_SHIFT);
2611 *hdr_len += skb_network_offset(skb);
2612 vlan_macip_lens |=
2613 (skb_transport_header(skb) - skb_network_header(skb));
2614 *hdr_len +=
2615 (skb_transport_header(skb) - skb_network_header(skb));
2616 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2617 context_desc->seqnum_seed = 0;
2618
2619 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2620 type_tucmd_mlhl |= (IXGBE_TXD_CMD_DEXT |
2621 IXGBE_ADVTXD_DTYP_CTXT);
2622
Al Viro8327d002007-12-10 18:54:12 +00002623 if (skb->protocol == htons(ETH_P_IP))
Auke Kok9a799d72007-09-15 14:07:45 -07002624 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
2625 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2626 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
2627
2628 /* MSS L4LEN IDX */
2629 mss_l4len_idx |=
2630 (skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT);
2631 mss_l4len_idx |= (l4len << IXGBE_ADVTXD_L4LEN_SHIFT);
2632 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2633
2634 tx_buffer_info->time_stamp = jiffies;
2635 tx_buffer_info->next_to_watch = i;
2636
2637 i++;
2638 if (i == tx_ring->count)
2639 i = 0;
2640 tx_ring->next_to_use = i;
2641
2642 return true;
2643 }
2644 return false;
2645}
2646
2647static bool ixgbe_tx_csum(struct ixgbe_adapter *adapter,
2648 struct ixgbe_ring *tx_ring,
2649 struct sk_buff *skb, u32 tx_flags)
2650{
2651 struct ixgbe_adv_tx_context_desc *context_desc;
2652 unsigned int i;
2653 struct ixgbe_tx_buffer *tx_buffer_info;
2654 u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
2655
2656 if (skb->ip_summed == CHECKSUM_PARTIAL ||
2657 (tx_flags & IXGBE_TX_FLAGS_VLAN)) {
2658 i = tx_ring->next_to_use;
2659 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2660 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
2661
2662 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2663 vlan_macip_lens |=
2664 (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
2665 vlan_macip_lens |= (skb_network_offset(skb) <<
2666 IXGBE_ADVTXD_MACLEN_SHIFT);
2667 if (skb->ip_summed == CHECKSUM_PARTIAL)
2668 vlan_macip_lens |= (skb_transport_header(skb) -
2669 skb_network_header(skb));
2670
2671 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2672 context_desc->seqnum_seed = 0;
2673
2674 type_tucmd_mlhl |= (IXGBE_TXD_CMD_DEXT |
2675 IXGBE_ADVTXD_DTYP_CTXT);
2676
2677 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Auke Kok41825d72008-02-12 15:20:33 -08002678 switch (skb->protocol) {
2679 case __constant_htons(ETH_P_IP):
Auke Kok9a799d72007-09-15 14:07:45 -07002680 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
Auke Kok41825d72008-02-12 15:20:33 -08002681 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2682 type_tucmd_mlhl |=
2683 IXGBE_ADVTXD_TUCMD_L4T_TCP;
2684 break;
Auke Kok9a799d72007-09-15 14:07:45 -07002685
Auke Kok41825d72008-02-12 15:20:33 -08002686 case __constant_htons(ETH_P_IPV6):
2687 /* XXX what about other V6 headers?? */
2688 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
2689 type_tucmd_mlhl |=
2690 IXGBE_ADVTXD_TUCMD_L4T_TCP;
2691 break;
2692
2693 default:
2694 if (unlikely(net_ratelimit())) {
2695 DPRINTK(PROBE, WARNING,
2696 "partial checksum but proto=%x!\n",
2697 skb->protocol);
2698 }
2699 break;
2700 }
Auke Kok9a799d72007-09-15 14:07:45 -07002701 }
2702
2703 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
2704 context_desc->mss_l4len_idx = 0;
2705
2706 tx_buffer_info->time_stamp = jiffies;
2707 tx_buffer_info->next_to_watch = i;
2708 adapter->hw_csum_tx_good++;
2709 i++;
2710 if (i == tx_ring->count)
2711 i = 0;
2712 tx_ring->next_to_use = i;
2713
2714 return true;
2715 }
2716 return false;
2717}
2718
2719static int ixgbe_tx_map(struct ixgbe_adapter *adapter,
2720 struct ixgbe_ring *tx_ring,
2721 struct sk_buff *skb, unsigned int first)
2722{
2723 struct ixgbe_tx_buffer *tx_buffer_info;
2724 unsigned int len = skb->len;
2725 unsigned int offset = 0, size, count = 0, i;
2726 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2727 unsigned int f;
2728
2729 len -= skb->data_len;
2730
2731 i = tx_ring->next_to_use;
2732
2733 while (len) {
2734 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2735 size = min(len, (uint)IXGBE_MAX_DATA_PER_TXD);
2736
2737 tx_buffer_info->length = size;
2738 tx_buffer_info->dma = pci_map_single(adapter->pdev,
2739 skb->data + offset,
2740 size, PCI_DMA_TODEVICE);
2741 tx_buffer_info->time_stamp = jiffies;
2742 tx_buffer_info->next_to_watch = i;
2743
2744 len -= size;
2745 offset += size;
2746 count++;
2747 i++;
2748 if (i == tx_ring->count)
2749 i = 0;
2750 }
2751
2752 for (f = 0; f < nr_frags; f++) {
2753 struct skb_frag_struct *frag;
2754
2755 frag = &skb_shinfo(skb)->frags[f];
2756 len = frag->size;
2757 offset = frag->page_offset;
2758
2759 while (len) {
2760 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2761 size = min(len, (uint)IXGBE_MAX_DATA_PER_TXD);
2762
2763 tx_buffer_info->length = size;
2764 tx_buffer_info->dma = pci_map_page(adapter->pdev,
2765 frag->page,
2766 offset,
2767 size, PCI_DMA_TODEVICE);
2768 tx_buffer_info->time_stamp = jiffies;
2769 tx_buffer_info->next_to_watch = i;
2770
2771 len -= size;
2772 offset += size;
2773 count++;
2774 i++;
2775 if (i == tx_ring->count)
2776 i = 0;
2777 }
2778 }
2779 if (i == 0)
2780 i = tx_ring->count - 1;
2781 else
2782 i = i - 1;
2783 tx_ring->tx_buffer_info[i].skb = skb;
2784 tx_ring->tx_buffer_info[first].next_to_watch = i;
2785
2786 return count;
2787}
2788
2789static void ixgbe_tx_queue(struct ixgbe_adapter *adapter,
2790 struct ixgbe_ring *tx_ring,
2791 int tx_flags, int count, u32 paylen, u8 hdr_len)
2792{
2793 union ixgbe_adv_tx_desc *tx_desc = NULL;
2794 struct ixgbe_tx_buffer *tx_buffer_info;
2795 u32 olinfo_status = 0, cmd_type_len = 0;
2796 unsigned int i;
2797 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
2798
2799 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
2800
2801 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
2802
2803 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2804 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
2805
2806 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
2807 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
2808
2809 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
2810 IXGBE_ADVTXD_POPTS_SHIFT;
2811
2812 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
2813 olinfo_status |= IXGBE_TXD_POPTS_IXSM <<
2814 IXGBE_ADVTXD_POPTS_SHIFT;
2815
2816 } else if (tx_flags & IXGBE_TX_FLAGS_CSUM)
2817 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
2818 IXGBE_ADVTXD_POPTS_SHIFT;
2819
2820 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
2821
2822 i = tx_ring->next_to_use;
2823 while (count--) {
2824 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2825 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
2826 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
2827 tx_desc->read.cmd_type_len =
2828 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
2829 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2830
2831 i++;
2832 if (i == tx_ring->count)
2833 i = 0;
2834 }
2835
2836 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
2837
2838 /*
2839 * Force memory writes to complete before letting h/w
2840 * know there are new descriptors to fetch. (Only
2841 * applicable for weak-ordered memory model archs,
2842 * such as IA-64).
2843 */
2844 wmb();
2845
2846 tx_ring->next_to_use = i;
2847 writel(i, adapter->hw.hw_addr + tx_ring->tail);
2848}
2849
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -08002850static int __ixgbe_maybe_stop_tx(struct net_device *netdev,
2851 struct ixgbe_ring *tx_ring, int size)
2852{
2853 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2854
2855 netif_stop_queue(netdev);
2856 /* Herbert's original patch had:
2857 * smp_mb__after_netif_stop_queue();
2858 * but since that doesn't exist yet, just open code it. */
2859 smp_mb();
2860
2861 /* We need to check again in a case another CPU has just
2862 * made room available. */
2863 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
2864 return -EBUSY;
2865
2866 /* A reprieve! - use start_queue because it doesn't call schedule */
2867 netif_wake_queue(netdev);
2868 ++adapter->restart_queue;
2869 return 0;
2870}
2871
2872static int ixgbe_maybe_stop_tx(struct net_device *netdev,
2873 struct ixgbe_ring *tx_ring, int size)
2874{
2875 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
2876 return 0;
2877 return __ixgbe_maybe_stop_tx(netdev, tx_ring, size);
2878}
2879
2880
Auke Kok9a799d72007-09-15 14:07:45 -07002881static int ixgbe_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2882{
2883 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2884 struct ixgbe_ring *tx_ring;
2885 unsigned int len = skb->len;
2886 unsigned int first;
2887 unsigned int tx_flags = 0;
Auke Kok9a799d72007-09-15 14:07:45 -07002888 u8 hdr_len;
2889 int tso;
2890 unsigned int mss = 0;
2891 int count = 0;
2892 unsigned int f;
2893 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2894 len -= skb->data_len;
2895
2896 tx_ring = adapter->tx_ring;
2897
2898 if (skb->len <= 0) {
2899 dev_kfree_skb(skb);
2900 return NETDEV_TX_OK;
2901 }
2902 mss = skb_shinfo(skb)->gso_size;
2903
2904 if (mss)
2905 count++;
2906 else if (skb->ip_summed == CHECKSUM_PARTIAL)
2907 count++;
2908
2909 count += TXD_USE_COUNT(len);
2910 for (f = 0; f < nr_frags; f++)
2911 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
2912
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -08002913 if (ixgbe_maybe_stop_tx(netdev, tx_ring, count)) {
Auke Kok9a799d72007-09-15 14:07:45 -07002914 adapter->tx_busy++;
Auke Kok9a799d72007-09-15 14:07:45 -07002915 return NETDEV_TX_BUSY;
2916 }
Auke Kok9a799d72007-09-15 14:07:45 -07002917 if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
2918 tx_flags |= IXGBE_TX_FLAGS_VLAN;
2919 tx_flags |= (vlan_tx_tag_get(skb) << IXGBE_TX_FLAGS_VLAN_SHIFT);
2920 }
2921
Al Viro8327d002007-12-10 18:54:12 +00002922 if (skb->protocol == htons(ETH_P_IP))
Auke Kok9a799d72007-09-15 14:07:45 -07002923 tx_flags |= IXGBE_TX_FLAGS_IPV4;
2924 first = tx_ring->next_to_use;
2925 tso = ixgbe_tso(adapter, tx_ring, skb, tx_flags, &hdr_len);
2926 if (tso < 0) {
2927 dev_kfree_skb_any(skb);
2928 return NETDEV_TX_OK;
2929 }
2930
2931 if (tso)
2932 tx_flags |= IXGBE_TX_FLAGS_TSO;
2933 else if (ixgbe_tx_csum(adapter, tx_ring, skb, tx_flags) &&
2934 (skb->ip_summed == CHECKSUM_PARTIAL))
2935 tx_flags |= IXGBE_TX_FLAGS_CSUM;
2936
2937 ixgbe_tx_queue(adapter, tx_ring, tx_flags,
2938 ixgbe_tx_map(adapter, tx_ring, skb, first),
2939 skb->len, hdr_len);
2940
2941 netdev->trans_start = jiffies;
2942
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -08002943 ixgbe_maybe_stop_tx(netdev, tx_ring, DESC_NEEDED);
Auke Kok9a799d72007-09-15 14:07:45 -07002944
2945 return NETDEV_TX_OK;
2946}
2947
2948/**
2949 * ixgbe_get_stats - Get System Network Statistics
2950 * @netdev: network interface device structure
2951 *
2952 * Returns the address of the device statistics structure.
2953 * The statistics are actually updated from the timer callback.
2954 **/
2955static struct net_device_stats *ixgbe_get_stats(struct net_device *netdev)
2956{
2957 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2958
2959 /* only return the current stats */
2960 return &adapter->net_stats;
2961}
2962
2963/**
2964 * ixgbe_set_mac - Change the Ethernet Address of the NIC
2965 * @netdev: network interface device structure
2966 * @p: pointer to an address structure
2967 *
2968 * Returns 0 on success, negative on failure
2969 **/
2970static int ixgbe_set_mac(struct net_device *netdev, void *p)
2971{
2972 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2973 struct sockaddr *addr = p;
2974
2975 if (!is_valid_ether_addr(addr->sa_data))
2976 return -EADDRNOTAVAIL;
2977
2978 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2979 memcpy(adapter->hw.mac.addr, addr->sa_data, netdev->addr_len);
2980
2981 ixgbe_set_rar(&adapter->hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
2982
2983 return 0;
2984}
2985
2986#ifdef CONFIG_NET_POLL_CONTROLLER
2987/*
2988 * Polling 'interrupt' - used by things like netconsole to send skbs
2989 * without having to re-enable interrupts. It's not called while
2990 * the interrupt routine is executing.
2991 */
2992static void ixgbe_netpoll(struct net_device *netdev)
2993{
2994 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2995
2996 disable_irq(adapter->pdev->irq);
2997 adapter->flags |= IXGBE_FLAG_IN_NETPOLL;
2998 ixgbe_intr(adapter->pdev->irq, netdev);
2999 adapter->flags &= ~IXGBE_FLAG_IN_NETPOLL;
3000 enable_irq(adapter->pdev->irq);
3001}
3002#endif
3003
3004/**
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08003005 * ixgbe_napi_add_all - prep napi structs for use
3006 * @adapter: private struct
3007 * helper function to napi_add each possible q_vector->napi
3008 */
3009static void ixgbe_napi_add_all(struct ixgbe_adapter *adapter)
3010{
3011 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
3012 int (*poll)(struct napi_struct *, int);
3013
3014 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
3015 poll = &ixgbe_clean_rxonly;
3016 } else {
3017 poll = &ixgbe_poll;
3018 /* only one q_vector for legacy modes */
3019 q_vectors = 1;
3020 }
3021
3022 for (i = 0; i < q_vectors; i++) {
3023 struct ixgbe_q_vector *q_vector = &adapter->q_vector[i];
3024 netif_napi_add(adapter->netdev, &q_vector->napi,
3025 (*poll), 64);
3026 }
3027}
3028
3029/**
Auke Kok9a799d72007-09-15 14:07:45 -07003030 * ixgbe_probe - Device Initialization Routine
3031 * @pdev: PCI device information struct
3032 * @ent: entry in ixgbe_pci_tbl
3033 *
3034 * Returns 0 on success, negative on failure
3035 *
3036 * ixgbe_probe initializes an adapter identified by a pci_dev structure.
3037 * The OS initialization, configuring of the adapter private structure,
3038 * and a hardware reset occur.
3039 **/
3040static int __devinit ixgbe_probe(struct pci_dev *pdev,
3041 const struct pci_device_id *ent)
3042{
3043 struct net_device *netdev;
3044 struct ixgbe_adapter *adapter = NULL;
3045 struct ixgbe_hw *hw;
3046 const struct ixgbe_info *ii = ixgbe_info_tbl[ent->driver_data];
3047 unsigned long mmio_start, mmio_len;
3048 static int cards_found;
3049 int i, err, pci_using_dac;
3050 u16 link_status, link_speed, link_width;
3051 u32 part_num;
3052
3053 err = pci_enable_device(pdev);
3054 if (err)
3055 return err;
3056
3057 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK) &&
3058 !pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK)) {
3059 pci_using_dac = 1;
3060 } else {
3061 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
3062 if (err) {
3063 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
3064 if (err) {
3065 dev_err(&pdev->dev, "No usable DMA "
3066 "configuration, aborting\n");
3067 goto err_dma;
3068 }
3069 }
3070 pci_using_dac = 0;
3071 }
3072
3073 err = pci_request_regions(pdev, ixgbe_driver_name);
3074 if (err) {
3075 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3076 goto err_pci_reg;
3077 }
3078
3079 pci_set_master(pdev);
3080
3081 netdev = alloc_etherdev(sizeof(struct ixgbe_adapter));
3082 if (!netdev) {
3083 err = -ENOMEM;
3084 goto err_alloc_etherdev;
3085 }
3086
Auke Kok9a799d72007-09-15 14:07:45 -07003087 SET_NETDEV_DEV(netdev, &pdev->dev);
3088
3089 pci_set_drvdata(pdev, netdev);
3090 adapter = netdev_priv(netdev);
3091
3092 adapter->netdev = netdev;
3093 adapter->pdev = pdev;
3094 hw = &adapter->hw;
3095 hw->back = adapter;
3096 adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
3097
3098 mmio_start = pci_resource_start(pdev, 0);
3099 mmio_len = pci_resource_len(pdev, 0);
3100
3101 hw->hw_addr = ioremap(mmio_start, mmio_len);
3102 if (!hw->hw_addr) {
3103 err = -EIO;
3104 goto err_ioremap;
3105 }
3106
3107 for (i = 1; i <= 5; i++) {
3108 if (pci_resource_len(pdev, i) == 0)
3109 continue;
3110 }
3111
3112 netdev->open = &ixgbe_open;
3113 netdev->stop = &ixgbe_close;
3114 netdev->hard_start_xmit = &ixgbe_xmit_frame;
3115 netdev->get_stats = &ixgbe_get_stats;
3116 netdev->set_multicast_list = &ixgbe_set_multi;
3117 netdev->set_mac_address = &ixgbe_set_mac;
3118 netdev->change_mtu = &ixgbe_change_mtu;
3119 ixgbe_set_ethtool_ops(netdev);
3120 netdev->tx_timeout = &ixgbe_tx_timeout;
3121 netdev->watchdog_timeo = 5 * HZ;
Auke Kok9a799d72007-09-15 14:07:45 -07003122 netdev->vlan_rx_register = ixgbe_vlan_rx_register;
3123 netdev->vlan_rx_add_vid = ixgbe_vlan_rx_add_vid;
3124 netdev->vlan_rx_kill_vid = ixgbe_vlan_rx_kill_vid;
3125#ifdef CONFIG_NET_POLL_CONTROLLER
3126 netdev->poll_controller = ixgbe_netpoll;
3127#endif
3128 strcpy(netdev->name, pci_name(pdev));
3129
3130 netdev->mem_start = mmio_start;
3131 netdev->mem_end = mmio_start + mmio_len;
3132
3133 adapter->bd_number = cards_found;
3134
3135 /* PCI config space info */
3136 hw->vendor_id = pdev->vendor;
3137 hw->device_id = pdev->device;
3138 hw->revision_id = pdev->revision;
3139 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3140 hw->subsystem_device_id = pdev->subsystem_device;
3141
3142 /* Setup hw api */
3143 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08003144 hw->mac.type = ii->mac;
Auke Kok9a799d72007-09-15 14:07:45 -07003145
3146 err = ii->get_invariants(hw);
3147 if (err)
3148 goto err_hw_init;
3149
3150 /* setup the private structure */
3151 err = ixgbe_sw_init(adapter);
3152 if (err)
3153 goto err_sw_init;
3154
3155 netdev->features = NETIF_F_SG |
3156 NETIF_F_HW_CSUM |
3157 NETIF_F_HW_VLAN_TX |
3158 NETIF_F_HW_VLAN_RX |
3159 NETIF_F_HW_VLAN_FILTER;
3160
3161 netdev->features |= NETIF_F_TSO;
3162
3163 netdev->features |= NETIF_F_TSO6;
3164 if (pci_using_dac)
3165 netdev->features |= NETIF_F_HIGHDMA;
3166
3167
3168 /* make sure the EEPROM is good */
3169 if (ixgbe_validate_eeprom_checksum(hw, NULL) < 0) {
3170 dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n");
3171 err = -EIO;
3172 goto err_eeprom;
3173 }
3174
3175 memcpy(netdev->dev_addr, hw->mac.perm_addr, netdev->addr_len);
3176 memcpy(netdev->perm_addr, hw->mac.perm_addr, netdev->addr_len);
3177
3178 if (ixgbe_validate_mac_addr(netdev->dev_addr)) {
3179 err = -EIO;
3180 goto err_eeprom;
3181 }
3182
3183 init_timer(&adapter->watchdog_timer);
3184 adapter->watchdog_timer.function = &ixgbe_watchdog;
3185 adapter->watchdog_timer.data = (unsigned long)adapter;
3186
3187 INIT_WORK(&adapter->reset_task, ixgbe_reset_task);
3188
3189 /* initialize default flow control settings */
3190 hw->fc.original_type = ixgbe_fc_full;
3191 hw->fc.type = ixgbe_fc_full;
3192 hw->fc.high_water = IXGBE_DEFAULT_FCRTH;
3193 hw->fc.low_water = IXGBE_DEFAULT_FCRTL;
3194 hw->fc.pause_time = IXGBE_DEFAULT_FCPAUSE;
3195
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08003196 err = ixgbe_init_interrupt_scheme(adapter);
3197 if (err)
3198 goto err_sw_init;
Auke Kok9a799d72007-09-15 14:07:45 -07003199
3200 /* print bus type/speed/width info */
3201 pci_read_config_word(pdev, IXGBE_PCI_LINK_STATUS, &link_status);
3202 link_speed = link_status & IXGBE_PCI_LINK_SPEED;
3203 link_width = link_status & IXGBE_PCI_LINK_WIDTH;
3204 dev_info(&pdev->dev, "(PCI Express:%s:%s) "
3205 "%02x:%02x:%02x:%02x:%02x:%02x\n",
3206 ((link_speed == IXGBE_PCI_LINK_SPEED_5000) ? "5.0Gb/s" :
3207 (link_speed == IXGBE_PCI_LINK_SPEED_2500) ? "2.5Gb/s" :
3208 "Unknown"),
3209 ((link_width == IXGBE_PCI_LINK_WIDTH_8) ? "Width x8" :
3210 (link_width == IXGBE_PCI_LINK_WIDTH_4) ? "Width x4" :
3211 (link_width == IXGBE_PCI_LINK_WIDTH_2) ? "Width x2" :
3212 (link_width == IXGBE_PCI_LINK_WIDTH_1) ? "Width x1" :
3213 "Unknown"),
3214 netdev->dev_addr[0], netdev->dev_addr[1], netdev->dev_addr[2],
3215 netdev->dev_addr[3], netdev->dev_addr[4], netdev->dev_addr[5]);
3216 ixgbe_read_part_num(hw, &part_num);
3217 dev_info(&pdev->dev, "MAC: %d, PHY: %d, PBA No: %06x-%03x\n",
3218 hw->mac.type, hw->phy.type,
3219 (part_num >> 8), (part_num & 0xff));
3220
Auke Kok0c254d82008-02-11 09:25:56 -08003221 if (link_width <= IXGBE_PCI_LINK_WIDTH_4) {
3222 dev_warn(&pdev->dev, "PCI-Express bandwidth available for "
3223 "this card is not sufficient for optimal "
3224 "performance.\n");
3225 dev_warn(&pdev->dev, "For optimal performance a x8 "
3226 "PCI-Express slot is required.\n");
3227 }
3228
Auke Kok9a799d72007-09-15 14:07:45 -07003229 /* reset the hardware with the new settings */
3230 ixgbe_start_hw(hw);
3231
3232 netif_carrier_off(netdev);
3233 netif_stop_queue(netdev);
3234
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08003235 ixgbe_napi_add_all(adapter);
3236
Auke Kok9a799d72007-09-15 14:07:45 -07003237 strcpy(netdev->name, "eth%d");
3238 err = register_netdev(netdev);
3239 if (err)
3240 goto err_register;
3241
3242
3243 dev_info(&pdev->dev, "Intel(R) 10 Gigabit Network Connection\n");
3244 cards_found++;
3245 return 0;
3246
3247err_register:
Ayyappan Veeraiyan5eba3692008-02-01 15:59:04 -08003248 ixgbe_release_hw_control(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07003249err_hw_init:
3250err_sw_init:
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08003251 ixgbe_reset_interrupt_capability(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07003252err_eeprom:
3253 iounmap(hw->hw_addr);
3254err_ioremap:
3255 free_netdev(netdev);
3256err_alloc_etherdev:
3257 pci_release_regions(pdev);
3258err_pci_reg:
3259err_dma:
3260 pci_disable_device(pdev);
3261 return err;
3262}
3263
3264/**
3265 * ixgbe_remove - Device Removal Routine
3266 * @pdev: PCI device information struct
3267 *
3268 * ixgbe_remove is called by the PCI subsystem to alert the driver
3269 * that it should release a PCI device. The could be caused by a
3270 * Hot-Plug event, or because the driver is going to be removed from
3271 * memory.
3272 **/
3273static void __devexit ixgbe_remove(struct pci_dev *pdev)
3274{
3275 struct net_device *netdev = pci_get_drvdata(pdev);
3276 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3277
3278 set_bit(__IXGBE_DOWN, &adapter->state);
3279 del_timer_sync(&adapter->watchdog_timer);
3280
3281 flush_scheduled_work();
3282
3283 unregister_netdev(netdev);
3284
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08003285 ixgbe_reset_interrupt_capability(adapter);
Ayyappan Veeraiyan5eba3692008-02-01 15:59:04 -08003286
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08003287 ixgbe_release_hw_control(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07003288
3289 iounmap(adapter->hw.hw_addr);
3290 pci_release_regions(pdev);
3291
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08003292 DPRINTK(PROBE, INFO, "complete\n");
3293 kfree(adapter->tx_ring);
3294 kfree(adapter->rx_ring);
3295
Auke Kok9a799d72007-09-15 14:07:45 -07003296 free_netdev(netdev);
3297
3298 pci_disable_device(pdev);
3299}
3300
3301/**
3302 * ixgbe_io_error_detected - called when PCI error is detected
3303 * @pdev: Pointer to PCI device
3304 * @state: The current pci connection state
3305 *
3306 * This function is called after a PCI bus error affecting
3307 * this device has been detected.
3308 */
3309static pci_ers_result_t ixgbe_io_error_detected(struct pci_dev *pdev,
3310 pci_channel_state_t state)
3311{
3312 struct net_device *netdev = pci_get_drvdata(pdev);
3313 struct ixgbe_adapter *adapter = netdev->priv;
3314
3315 netif_device_detach(netdev);
3316
3317 if (netif_running(netdev))
3318 ixgbe_down(adapter);
3319 pci_disable_device(pdev);
3320
3321 /* Request a slot slot reset. */
3322 return PCI_ERS_RESULT_NEED_RESET;
3323}
3324
3325/**
3326 * ixgbe_io_slot_reset - called after the pci bus has been reset.
3327 * @pdev: Pointer to PCI device
3328 *
3329 * Restart the card from scratch, as if from a cold-boot.
3330 */
3331static pci_ers_result_t ixgbe_io_slot_reset(struct pci_dev *pdev)
3332{
3333 struct net_device *netdev = pci_get_drvdata(pdev);
3334 struct ixgbe_adapter *adapter = netdev->priv;
3335
3336 if (pci_enable_device(pdev)) {
3337 DPRINTK(PROBE, ERR,
3338 "Cannot re-enable PCI device after reset.\n");
3339 return PCI_ERS_RESULT_DISCONNECT;
3340 }
3341 pci_set_master(pdev);
3342
3343 pci_enable_wake(pdev, PCI_D3hot, 0);
3344 pci_enable_wake(pdev, PCI_D3cold, 0);
3345
3346 ixgbe_reset(adapter);
3347
3348 return PCI_ERS_RESULT_RECOVERED;
3349}
3350
3351/**
3352 * ixgbe_io_resume - called when traffic can start flowing again.
3353 * @pdev: Pointer to PCI device
3354 *
3355 * This callback is called when the error recovery driver tells us that
3356 * its OK to resume normal operation.
3357 */
3358static void ixgbe_io_resume(struct pci_dev *pdev)
3359{
3360 struct net_device *netdev = pci_get_drvdata(pdev);
3361 struct ixgbe_adapter *adapter = netdev->priv;
3362
3363 if (netif_running(netdev)) {
3364 if (ixgbe_up(adapter)) {
3365 DPRINTK(PROBE, INFO, "ixgbe_up failed after reset\n");
3366 return;
3367 }
3368 }
3369
3370 netif_device_attach(netdev);
3371
3372}
3373
3374static struct pci_error_handlers ixgbe_err_handler = {
3375 .error_detected = ixgbe_io_error_detected,
3376 .slot_reset = ixgbe_io_slot_reset,
3377 .resume = ixgbe_io_resume,
3378};
3379
3380static struct pci_driver ixgbe_driver = {
3381 .name = ixgbe_driver_name,
3382 .id_table = ixgbe_pci_tbl,
3383 .probe = ixgbe_probe,
3384 .remove = __devexit_p(ixgbe_remove),
3385#ifdef CONFIG_PM
3386 .suspend = ixgbe_suspend,
3387 .resume = ixgbe_resume,
3388#endif
3389 .shutdown = ixgbe_shutdown,
3390 .err_handler = &ixgbe_err_handler
3391};
3392
3393/**
3394 * ixgbe_init_module - Driver Registration Routine
3395 *
3396 * ixgbe_init_module is the first routine called when the driver is
3397 * loaded. All it does is register with the PCI subsystem.
3398 **/
3399static int __init ixgbe_init_module(void)
3400{
3401 int ret;
3402 printk(KERN_INFO "%s: %s - version %s\n", ixgbe_driver_name,
3403 ixgbe_driver_string, ixgbe_driver_version);
3404
3405 printk(KERN_INFO "%s: %s\n", ixgbe_driver_name, ixgbe_copyright);
3406
3407 ret = pci_register_driver(&ixgbe_driver);
3408 return ret;
3409}
3410module_init(ixgbe_init_module);
3411
3412/**
3413 * ixgbe_exit_module - Driver Exit Cleanup Routine
3414 *
3415 * ixgbe_exit_module is called just before the driver is removed
3416 * from memory.
3417 **/
3418static void __exit ixgbe_exit_module(void)
3419{
3420 pci_unregister_driver(&ixgbe_driver);
3421}
3422module_exit(ixgbe_exit_module);
3423
3424/* ixgbe_main.c */