blob: 8f04609011538e257b820e9b521359750073cc63 [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
Auke Kokef850042008-03-03 15:04:07 -080051#define DRV_VERSION "1.3.18-k2"
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
Jeb Cramerbd0362d2008-03-03 15:04:02 -080083#ifdef CONFIG_DCA
84static int ixgbe_notify_dca(struct notifier_block *, unsigned long event,
85 void *p);
86static struct notifier_block dca_notifier = {
87 .notifier_call = ixgbe_notify_dca,
88 .next = NULL,
89 .priority = 0
90};
91#endif
92
Auke Kok9a799d72007-09-15 14:07:45 -070093MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
94MODULE_DESCRIPTION("Intel(R) 10 Gigabit PCI Express Network Driver");
95MODULE_LICENSE("GPL");
96MODULE_VERSION(DRV_VERSION);
97
98#define DEFAULT_DEBUG_LEVEL_SHIFT 3
99
Ayyappan Veeraiyan5eba3692008-02-01 15:59:04 -0800100static void ixgbe_release_hw_control(struct ixgbe_adapter *adapter)
101{
102 u32 ctrl_ext;
103
104 /* Let firmware take over control of h/w */
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}
109
110static void ixgbe_get_hw_control(struct ixgbe_adapter *adapter)
111{
112 u32 ctrl_ext;
113
114 /* Let firmware know the driver has taken over */
115 ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
116 IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
117 ctrl_ext | IXGBE_CTRL_EXT_DRV_LOAD);
118}
Auke Kok9a799d72007-09-15 14:07:45 -0700119
120#ifdef DEBUG
121/**
122 * ixgbe_get_hw_dev_name - return device name string
123 * used by hardware layer to print debugging information
124 **/
125char *ixgbe_get_hw_dev_name(struct ixgbe_hw *hw)
126{
127 struct ixgbe_adapter *adapter = hw->back;
128 struct net_device *netdev = adapter->netdev;
129 return netdev->name;
130}
131#endif
132
133static void ixgbe_set_ivar(struct ixgbe_adapter *adapter, u16 int_alloc_entry,
134 u8 msix_vector)
135{
136 u32 ivar, index;
137
138 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
139 index = (int_alloc_entry >> 2) & 0x1F;
140 ivar = IXGBE_READ_REG(&adapter->hw, IXGBE_IVAR(index));
141 ivar &= ~(0xFF << (8 * (int_alloc_entry & 0x3)));
142 ivar |= (msix_vector << (8 * (int_alloc_entry & 0x3)));
143 IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR(index), ivar);
144}
145
146static void ixgbe_unmap_and_free_tx_resource(struct ixgbe_adapter *adapter,
147 struct ixgbe_tx_buffer
148 *tx_buffer_info)
149{
150 if (tx_buffer_info->dma) {
151 pci_unmap_page(adapter->pdev,
152 tx_buffer_info->dma,
153 tx_buffer_info->length, PCI_DMA_TODEVICE);
154 tx_buffer_info->dma = 0;
155 }
156 if (tx_buffer_info->skb) {
157 dev_kfree_skb_any(tx_buffer_info->skb);
158 tx_buffer_info->skb = NULL;
159 }
160 /* tx_buffer_info must be completely set up in the transmit path */
161}
162
163static inline bool ixgbe_check_tx_hang(struct ixgbe_adapter *adapter,
164 struct ixgbe_ring *tx_ring,
165 unsigned int eop,
166 union ixgbe_adv_tx_desc *eop_desc)
167{
168 /* Detect a transmit hang in hardware, this serializes the
169 * check with the clearing of time_stamp and movement of i */
170 adapter->detect_tx_hung = false;
171 if (tx_ring->tx_buffer_info[eop].dma &&
172 time_after(jiffies, tx_ring->tx_buffer_info[eop].time_stamp + HZ) &&
173 !(IXGBE_READ_REG(&adapter->hw, IXGBE_TFCS) & IXGBE_TFCS_TXOFF)) {
174 /* detected Tx unit hang */
175 DPRINTK(DRV, ERR, "Detected Tx Unit Hang\n"
176 " TDH <%x>\n"
177 " TDT <%x>\n"
178 " next_to_use <%x>\n"
179 " next_to_clean <%x>\n"
180 "tx_buffer_info[next_to_clean]\n"
181 " time_stamp <%lx>\n"
182 " next_to_watch <%x>\n"
183 " jiffies <%lx>\n"
184 " next_to_watch.status <%x>\n",
185 readl(adapter->hw.hw_addr + tx_ring->head),
186 readl(adapter->hw.hw_addr + tx_ring->tail),
187 tx_ring->next_to_use,
188 tx_ring->next_to_clean,
189 tx_ring->tx_buffer_info[eop].time_stamp,
190 eop, jiffies, eop_desc->wb.status);
191 return true;
192 }
193
194 return false;
195}
196
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800197#define IXGBE_MAX_TXD_PWR 14
198#define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
199
200/* Tx Descriptors needed, worst case */
201#define TXD_USE_COUNT(S) (((S) >> IXGBE_MAX_TXD_PWR) + \
202 (((S) & (IXGBE_MAX_DATA_PER_TXD - 1)) ? 1 : 0))
203#define DESC_NEEDED (TXD_USE_COUNT(IXGBE_MAX_DATA_PER_TXD) /* skb->data */ + \
204 MAX_SKB_FRAGS * TXD_USE_COUNT(PAGE_SIZE) + 1) /* for context */
205
Auke Kok9a799d72007-09-15 14:07:45 -0700206/**
207 * ixgbe_clean_tx_irq - Reclaim resources after transmit completes
208 * @adapter: board private structure
209 **/
210static bool ixgbe_clean_tx_irq(struct ixgbe_adapter *adapter,
211 struct ixgbe_ring *tx_ring)
212{
213 struct net_device *netdev = adapter->netdev;
214 union ixgbe_adv_tx_desc *tx_desc, *eop_desc;
215 struct ixgbe_tx_buffer *tx_buffer_info;
216 unsigned int i, eop;
217 bool cleaned = false;
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800218 unsigned int total_tx_bytes = 0, total_tx_packets = 0;
Auke Kok9a799d72007-09-15 14:07:45 -0700219
220 i = tx_ring->next_to_clean;
221 eop = tx_ring->tx_buffer_info[i].next_to_watch;
222 eop_desc = IXGBE_TX_DESC_ADV(*tx_ring, eop);
223 while (eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)) {
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800224 cleaned = false;
225 while (!cleaned) {
Auke Kok9a799d72007-09-15 14:07:45 -0700226 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
227 tx_buffer_info = &tx_ring->tx_buffer_info[i];
228 cleaned = (i == eop);
229
230 tx_ring->stats.bytes += tx_buffer_info->length;
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800231 if (cleaned) {
232 struct sk_buff *skb = tx_buffer_info->skb;
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800233 unsigned int segs, bytecount;
234 segs = skb_shinfo(skb)->gso_segs ?: 1;
235 /* multiply data chunks by size of headers */
236 bytecount = ((segs - 1) * skb_headlen(skb)) +
237 skb->len;
238 total_tx_packets += segs;
239 total_tx_bytes += bytecount;
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800240 }
Auke Kok9a799d72007-09-15 14:07:45 -0700241 ixgbe_unmap_and_free_tx_resource(adapter,
242 tx_buffer_info);
243 tx_desc->wb.status = 0;
244
245 i++;
246 if (i == tx_ring->count)
247 i = 0;
248 }
249
250 tx_ring->stats.packets++;
251
252 eop = tx_ring->tx_buffer_info[i].next_to_watch;
253 eop_desc = IXGBE_TX_DESC_ADV(*tx_ring, eop);
254
255 /* weight of a sort for tx, avoid endless transmit cleanup */
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800256 if (total_tx_packets >= tx_ring->work_limit)
Auke Kok9a799d72007-09-15 14:07:45 -0700257 break;
258 }
259
260 tx_ring->next_to_clean = i;
261
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800262#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
263 if (total_tx_packets && netif_carrier_ok(netdev) &&
264 (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD)) {
265 /* Make sure that anybody stopping the queue after this
266 * sees the new next_to_clean.
267 */
268 smp_mb();
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -0800269#ifdef CONFIG_NETDEVICES_MULTIQUEUE
270 if (__netif_subqueue_stopped(netdev, tx_ring->queue_index) &&
271 !test_bit(__IXGBE_DOWN, &adapter->state)) {
272 netif_wake_subqueue(netdev, tx_ring->queue_index);
273 adapter->restart_queue++;
274 }
275#else
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800276 if (netif_queue_stopped(netdev) &&
277 !test_bit(__IXGBE_DOWN, &adapter->state)) {
278 netif_wake_queue(netdev);
279 adapter->restart_queue++;
280 }
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -0800281#endif
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800282 }
Auke Kok9a799d72007-09-15 14:07:45 -0700283
284 if (adapter->detect_tx_hung)
285 if (ixgbe_check_tx_hang(adapter, tx_ring, eop, eop_desc))
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -0800286#ifdef CONFIG_NETDEVICES_MULTIQUEUE
287 netif_stop_subqueue(netdev, tx_ring->queue_index);
288#else
Auke Kok9a799d72007-09-15 14:07:45 -0700289 netif_stop_queue(netdev);
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -0800290#endif
Auke Kok9a799d72007-09-15 14:07:45 -0700291
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800292 if (total_tx_packets >= tx_ring->work_limit)
Auke Kok9a799d72007-09-15 14:07:45 -0700293 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS, tx_ring->eims_value);
294
Ayyappan Veeraiyanf494e8f2008-03-03 15:03:57 -0800295 tx_ring->total_bytes += total_tx_bytes;
296 tx_ring->total_packets += total_tx_packets;
Ayyappan Veeraiyand2f4fbe2008-02-01 15:59:19 -0800297 adapter->net_stats.tx_bytes += total_tx_bytes;
298 adapter->net_stats.tx_packets += total_tx_packets;
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -0800299 cleaned = total_tx_packets ? true : false;
Auke Kok9a799d72007-09-15 14:07:45 -0700300 return cleaned;
301}
302
Jeb Cramerbd0362d2008-03-03 15:04:02 -0800303#ifdef CONFIG_DCA
304static void ixgbe_update_rx_dca(struct ixgbe_adapter *adapter,
305 struct ixgbe_ring *rxr)
306{
307 u32 rxctrl;
308 int cpu = get_cpu();
309 int q = rxr - adapter->rx_ring;
310
311 if (rxr->cpu != cpu) {
312 rxctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_DCA_RXCTRL(q));
313 rxctrl &= ~IXGBE_DCA_RXCTRL_CPUID_MASK;
314 rxctrl |= dca_get_tag(cpu);
315 rxctrl |= IXGBE_DCA_RXCTRL_DESC_DCA_EN;
316 rxctrl |= IXGBE_DCA_RXCTRL_HEAD_DCA_EN;
317 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_RXCTRL(q), rxctrl);
318 rxr->cpu = cpu;
319 }
320 put_cpu();
321}
322
323static void ixgbe_update_tx_dca(struct ixgbe_adapter *adapter,
324 struct ixgbe_ring *txr)
325{
326 u32 txctrl;
327 int cpu = get_cpu();
328 int q = txr - adapter->tx_ring;
329
330 if (txr->cpu != cpu) {
331 txctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_DCA_TXCTRL(q));
332 txctrl &= ~IXGBE_DCA_TXCTRL_CPUID_MASK;
333 txctrl |= dca_get_tag(cpu);
334 txctrl |= IXGBE_DCA_TXCTRL_DESC_DCA_EN;
335 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_TXCTRL(q), txctrl);
336 txr->cpu = cpu;
337 }
338 put_cpu();
339}
340
341static void ixgbe_setup_dca(struct ixgbe_adapter *adapter)
342{
343 int i;
344
345 if (!(adapter->flags & IXGBE_FLAG_DCA_ENABLED))
346 return;
347
348 for (i = 0; i < adapter->num_tx_queues; i++) {
349 adapter->tx_ring[i].cpu = -1;
350 ixgbe_update_tx_dca(adapter, &adapter->tx_ring[i]);
351 }
352 for (i = 0; i < adapter->num_rx_queues; i++) {
353 adapter->rx_ring[i].cpu = -1;
354 ixgbe_update_rx_dca(adapter, &adapter->rx_ring[i]);
355 }
356}
357
358static int __ixgbe_notify_dca(struct device *dev, void *data)
359{
360 struct net_device *netdev = dev_get_drvdata(dev);
361 struct ixgbe_adapter *adapter = netdev_priv(netdev);
362 unsigned long event = *(unsigned long *)data;
363
364 switch (event) {
365 case DCA_PROVIDER_ADD:
366 adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
367 /* Always use CB2 mode, difference is masked
368 * in the CB driver. */
369 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 2);
Denis V. Lunev652f0932008-03-27 14:39:17 +0300370 if (dca_add_requester(dev) == 0) {
Jeb Cramerbd0362d2008-03-03 15:04:02 -0800371 ixgbe_setup_dca(adapter);
372 break;
373 }
374 /* Fall Through since DCA is disabled. */
375 case DCA_PROVIDER_REMOVE:
376 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
377 dca_remove_requester(dev);
378 adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
379 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 1);
380 }
381 break;
382 }
383
Denis V. Lunev652f0932008-03-27 14:39:17 +0300384 return 0;
Jeb Cramerbd0362d2008-03-03 15:04:02 -0800385}
386
387#endif /* CONFIG_DCA */
Auke Kok9a799d72007-09-15 14:07:45 -0700388/**
389 * ixgbe_receive_skb - Send a completed packet up the stack
390 * @adapter: board private structure
391 * @skb: packet to send up
392 * @is_vlan: packet has a VLAN tag
393 * @tag: VLAN tag from descriptor
394 **/
395static void ixgbe_receive_skb(struct ixgbe_adapter *adapter,
396 struct sk_buff *skb, bool is_vlan,
397 u16 tag)
398{
399 if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL)) {
400 if (adapter->vlgrp && is_vlan)
401 vlan_hwaccel_receive_skb(skb, adapter->vlgrp, tag);
402 else
403 netif_receive_skb(skb);
404 } else {
405
406 if (adapter->vlgrp && is_vlan)
407 vlan_hwaccel_rx(skb, adapter->vlgrp, tag);
408 else
409 netif_rx(skb);
410 }
411}
412
Ayyappan Veeraiyane59bd252008-02-01 15:59:09 -0800413/**
414 * ixgbe_rx_checksum - indicate in skb if hw indicated a good cksum
415 * @adapter: address of board private structure
416 * @status_err: hardware indication of status of receive
417 * @skb: skb currently being received and modified
418 **/
Auke Kok9a799d72007-09-15 14:07:45 -0700419static inline void ixgbe_rx_checksum(struct ixgbe_adapter *adapter,
420 u32 status_err,
421 struct sk_buff *skb)
422{
423 skb->ip_summed = CHECKSUM_NONE;
424
Ayyappan Veeraiyane59bd252008-02-01 15:59:09 -0800425 /* Ignore Checksum bit is set, or rx csum disabled */
Auke Kok9a799d72007-09-15 14:07:45 -0700426 if ((status_err & IXGBE_RXD_STAT_IXSM) ||
Ayyappan Veeraiyane59bd252008-02-01 15:59:09 -0800427 !(adapter->flags & IXGBE_FLAG_RX_CSUM_ENABLED))
Auke Kok9a799d72007-09-15 14:07:45 -0700428 return;
Ayyappan Veeraiyane59bd252008-02-01 15:59:09 -0800429
430 /* if IP and error */
431 if ((status_err & IXGBE_RXD_STAT_IPCS) &&
432 (status_err & IXGBE_RXDADV_ERR_IPE)) {
Auke Kok9a799d72007-09-15 14:07:45 -0700433 adapter->hw_csum_rx_error++;
434 return;
435 }
Ayyappan Veeraiyane59bd252008-02-01 15:59:09 -0800436
437 if (!(status_err & IXGBE_RXD_STAT_L4CS))
438 return;
439
440 if (status_err & IXGBE_RXDADV_ERR_TCPE) {
441 adapter->hw_csum_rx_error++;
442 return;
443 }
444
Auke Kok9a799d72007-09-15 14:07:45 -0700445 /* It must be a TCP or UDP packet with a valid checksum */
Ayyappan Veeraiyane59bd252008-02-01 15:59:09 -0800446 skb->ip_summed = CHECKSUM_UNNECESSARY;
Auke Kok9a799d72007-09-15 14:07:45 -0700447 adapter->hw_csum_rx_good++;
448}
449
450/**
451 * ixgbe_alloc_rx_buffers - Replace used receive buffers; packet split
452 * @adapter: address of board private structure
453 **/
454static void ixgbe_alloc_rx_buffers(struct ixgbe_adapter *adapter,
455 struct ixgbe_ring *rx_ring,
456 int cleaned_count)
457{
458 struct net_device *netdev = adapter->netdev;
459 struct pci_dev *pdev = adapter->pdev;
460 union ixgbe_adv_rx_desc *rx_desc;
461 struct ixgbe_rx_buffer *rx_buffer_info;
462 struct sk_buff *skb;
463 unsigned int i;
464 unsigned int bufsz = adapter->rx_buf_len + NET_IP_ALIGN;
465
466 i = rx_ring->next_to_use;
467 rx_buffer_info = &rx_ring->rx_buffer_info[i];
468
469 while (cleaned_count--) {
470 rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
471
472 if (!rx_buffer_info->page &&
473 (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED)) {
474 rx_buffer_info->page = alloc_page(GFP_ATOMIC);
475 if (!rx_buffer_info->page) {
476 adapter->alloc_rx_page_failed++;
477 goto no_buffers;
478 }
479 rx_buffer_info->page_dma =
480 pci_map_page(pdev, rx_buffer_info->page,
481 0, PAGE_SIZE, PCI_DMA_FROMDEVICE);
482 }
483
484 if (!rx_buffer_info->skb) {
485 skb = netdev_alloc_skb(netdev, bufsz);
486
487 if (!skb) {
488 adapter->alloc_rx_buff_failed++;
489 goto no_buffers;
490 }
491
492 /*
493 * Make buffer alignment 2 beyond a 16 byte boundary
494 * this will result in a 16 byte aligned IP header after
495 * the 14 byte MAC header is removed
496 */
497 skb_reserve(skb, NET_IP_ALIGN);
498
499 rx_buffer_info->skb = skb;
500 rx_buffer_info->dma = pci_map_single(pdev, skb->data,
501 bufsz,
502 PCI_DMA_FROMDEVICE);
503 }
504 /* Refresh the desc even if buffer_addrs didn't change because
505 * each write-back erases this info. */
506 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
507 rx_desc->read.pkt_addr =
508 cpu_to_le64(rx_buffer_info->page_dma);
509 rx_desc->read.hdr_addr =
510 cpu_to_le64(rx_buffer_info->dma);
511 } else {
512 rx_desc->read.pkt_addr =
513 cpu_to_le64(rx_buffer_info->dma);
514 }
515
516 i++;
517 if (i == rx_ring->count)
518 i = 0;
519 rx_buffer_info = &rx_ring->rx_buffer_info[i];
520 }
521no_buffers:
522 if (rx_ring->next_to_use != i) {
523 rx_ring->next_to_use = i;
524 if (i-- == 0)
525 i = (rx_ring->count - 1);
526
527 /*
528 * Force memory writes to complete before letting h/w
529 * know there are new descriptors to fetch. (Only
530 * applicable for weak-ordered memory model archs,
531 * such as IA-64).
532 */
533 wmb();
534 writel(i, adapter->hw.hw_addr + rx_ring->tail);
535 }
536}
537
538static bool ixgbe_clean_rx_irq(struct ixgbe_adapter *adapter,
539 struct ixgbe_ring *rx_ring,
540 int *work_done, int work_to_do)
541{
542 struct net_device *netdev = adapter->netdev;
543 struct pci_dev *pdev = adapter->pdev;
544 union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
545 struct ixgbe_rx_buffer *rx_buffer_info, *next_buffer;
546 struct sk_buff *skb;
547 unsigned int i;
548 u32 upper_len, len, staterr;
549 u16 hdr_info, vlan_tag;
550 bool is_vlan, cleaned = false;
551 int cleaned_count = 0;
Ayyappan Veeraiyand2f4fbe2008-02-01 15:59:19 -0800552 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
Auke Kok9a799d72007-09-15 14:07:45 -0700553
554 i = rx_ring->next_to_clean;
555 upper_len = 0;
556 rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
557 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
558 rx_buffer_info = &rx_ring->rx_buffer_info[i];
559 is_vlan = (staterr & IXGBE_RXD_STAT_VP);
560 vlan_tag = le16_to_cpu(rx_desc->wb.upper.vlan);
561
562 while (staterr & IXGBE_RXD_STAT_DD) {
563 if (*work_done >= work_to_do)
564 break;
565 (*work_done)++;
566
567 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
568 hdr_info =
569 le16_to_cpu(rx_desc->wb.lower.lo_dword.hdr_info);
570 len =
571 ((hdr_info & IXGBE_RXDADV_HDRBUFLEN_MASK) >>
572 IXGBE_RXDADV_HDRBUFLEN_SHIFT);
573 if (hdr_info & IXGBE_RXDADV_SPH)
574 adapter->rx_hdr_split++;
575 if (len > IXGBE_RX_HDR_SIZE)
576 len = IXGBE_RX_HDR_SIZE;
577 upper_len = le16_to_cpu(rx_desc->wb.upper.length);
578 } else
579 len = le16_to_cpu(rx_desc->wb.upper.length);
580
581 cleaned = true;
582 skb = rx_buffer_info->skb;
583 prefetch(skb->data - NET_IP_ALIGN);
584 rx_buffer_info->skb = NULL;
585
586 if (len && !skb_shinfo(skb)->nr_frags) {
587 pci_unmap_single(pdev, rx_buffer_info->dma,
588 adapter->rx_buf_len + NET_IP_ALIGN,
589 PCI_DMA_FROMDEVICE);
590 skb_put(skb, len);
591 }
592
593 if (upper_len) {
594 pci_unmap_page(pdev, rx_buffer_info->page_dma,
595 PAGE_SIZE, PCI_DMA_FROMDEVICE);
596 rx_buffer_info->page_dma = 0;
597 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
598 rx_buffer_info->page, 0, upper_len);
599 rx_buffer_info->page = NULL;
600
601 skb->len += upper_len;
602 skb->data_len += upper_len;
603 skb->truesize += upper_len;
604 }
605
606 i++;
607 if (i == rx_ring->count)
608 i = 0;
609 next_buffer = &rx_ring->rx_buffer_info[i];
610
611 next_rxd = IXGBE_RX_DESC_ADV(*rx_ring, i);
612 prefetch(next_rxd);
613
614 cleaned_count++;
615 if (staterr & IXGBE_RXD_STAT_EOP) {
616 rx_ring->stats.packets++;
617 rx_ring->stats.bytes += skb->len;
618 } else {
619 rx_buffer_info->skb = next_buffer->skb;
620 rx_buffer_info->dma = next_buffer->dma;
621 next_buffer->skb = skb;
622 adapter->non_eop_descs++;
623 goto next_desc;
624 }
625
626 if (staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK) {
627 dev_kfree_skb_irq(skb);
628 goto next_desc;
629 }
630
631 ixgbe_rx_checksum(adapter, staterr, skb);
Ayyappan Veeraiyand2f4fbe2008-02-01 15:59:19 -0800632
633 /* probably a little skewed due to removing CRC */
634 total_rx_bytes += skb->len;
635 total_rx_packets++;
636
Auke Kok9a799d72007-09-15 14:07:45 -0700637 skb->protocol = eth_type_trans(skb, netdev);
638 ixgbe_receive_skb(adapter, skb, is_vlan, vlan_tag);
639 netdev->last_rx = jiffies;
640
641next_desc:
642 rx_desc->wb.upper.status_error = 0;
643
644 /* return some buffers to hardware, one at a time is too slow */
645 if (cleaned_count >= IXGBE_RX_BUFFER_WRITE) {
646 ixgbe_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
647 cleaned_count = 0;
648 }
649
650 /* use prefetched values */
651 rx_desc = next_rxd;
652 rx_buffer_info = next_buffer;
653
654 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
655 is_vlan = (staterr & IXGBE_RXD_STAT_VP);
656 vlan_tag = le16_to_cpu(rx_desc->wb.upper.vlan);
657 }
658
659 rx_ring->next_to_clean = i;
660 cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
661
662 if (cleaned_count)
663 ixgbe_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
664
Ayyappan Veeraiyand2f4fbe2008-02-01 15:59:19 -0800665 adapter->net_stats.rx_bytes += total_rx_bytes;
666 adapter->net_stats.rx_packets += total_rx_packets;
667
Ayyappan Veeraiyanf494e8f2008-03-03 15:03:57 -0800668 rx_ring->total_packets += total_rx_packets;
669 rx_ring->total_bytes += total_rx_bytes;
670 adapter->net_stats.rx_bytes += total_rx_bytes;
671 adapter->net_stats.rx_packets += total_rx_packets;
672
Auke Kok9a799d72007-09-15 14:07:45 -0700673 return cleaned;
674}
675
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800676static int ixgbe_clean_rxonly(struct napi_struct *, int);
Auke Kok9a799d72007-09-15 14:07:45 -0700677/**
678 * ixgbe_configure_msix - Configure MSI-X hardware
679 * @adapter: board private structure
680 *
681 * ixgbe_configure_msix sets up the hardware to properly generate MSI-X
682 * interrupts.
683 **/
684static void ixgbe_configure_msix(struct ixgbe_adapter *adapter)
685{
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800686 struct ixgbe_q_vector *q_vector;
687 int i, j, q_vectors, v_idx, r_idx;
688 u32 mask;
Auke Kok9a799d72007-09-15 14:07:45 -0700689
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800690 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
691
692 /* Populate the IVAR table and set the ITR values to the
693 * corresponding register.
694 */
695 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
696 q_vector = &adapter->q_vector[v_idx];
697 /* XXX for_each_bit(...) */
698 r_idx = find_first_bit(q_vector->rxr_idx,
699 adapter->num_rx_queues);
700
701 for (i = 0; i < q_vector->rxr_count; i++) {
702 j = adapter->rx_ring[r_idx].reg_idx;
703 ixgbe_set_ivar(adapter, IXGBE_IVAR_RX_QUEUE(j), v_idx);
704 r_idx = find_next_bit(q_vector->rxr_idx,
705 adapter->num_rx_queues,
706 r_idx + 1);
707 }
708 r_idx = find_first_bit(q_vector->txr_idx,
709 adapter->num_tx_queues);
710
711 for (i = 0; i < q_vector->txr_count; i++) {
712 j = adapter->tx_ring[r_idx].reg_idx;
713 ixgbe_set_ivar(adapter, IXGBE_IVAR_TX_QUEUE(j), v_idx);
714 r_idx = find_next_bit(q_vector->txr_idx,
715 adapter->num_tx_queues,
716 r_idx + 1);
717 }
718
719 /* if this is a tx only vector use half the irq (tx) rate */
720 if (q_vector->txr_count && !q_vector->rxr_count)
721 q_vector->eitr = adapter->tx_eitr;
722 else
723 /* rx only or mixed */
724 q_vector->eitr = adapter->rx_eitr;
725
726 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(v_idx),
727 EITR_INTS_PER_SEC_TO_REG(q_vector->eitr));
Auke Kok9a799d72007-09-15 14:07:45 -0700728 }
729
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800730 ixgbe_set_ivar(adapter, IXGBE_IVAR_OTHER_CAUSES_INDEX, v_idx);
731 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(v_idx), 1950);
Auke Kok9a799d72007-09-15 14:07:45 -0700732
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800733 /* set up to autoclear timer, lsc, and the vectors */
734 mask = IXGBE_EIMS_ENABLE_MASK;
735 mask &= ~IXGBE_EIMS_OTHER;
736 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC, mask);
Auke Kok9a799d72007-09-15 14:07:45 -0700737}
738
Ayyappan Veeraiyanf494e8f2008-03-03 15:03:57 -0800739enum latency_range {
740 lowest_latency = 0,
741 low_latency = 1,
742 bulk_latency = 2,
743 latency_invalid = 255
744};
745
746/**
747 * ixgbe_update_itr - update the dynamic ITR value based on statistics
748 * @adapter: pointer to adapter
749 * @eitr: eitr setting (ints per sec) to give last timeslice
750 * @itr_setting: current throttle rate in ints/second
751 * @packets: the number of packets during this measurement interval
752 * @bytes: the number of bytes during this measurement interval
753 *
754 * Stores a new ITR value based on packets and byte
755 * counts during the last interrupt. The advantage of per interrupt
756 * computation is faster updates and more accurate ITR for the current
757 * traffic pattern. Constants in this function were computed
758 * based on theoretical maximum wire speed and thresholds were set based
759 * on testing data as well as attempting to minimize response time
760 * while increasing bulk throughput.
761 * this functionality is controlled by the InterruptThrottleRate module
762 * parameter (see ixgbe_param.c)
763 **/
764static u8 ixgbe_update_itr(struct ixgbe_adapter *adapter,
765 u32 eitr, u8 itr_setting,
766 int packets, int bytes)
767{
768 unsigned int retval = itr_setting;
769 u32 timepassed_us;
770 u64 bytes_perint;
771
772 if (packets == 0)
773 goto update_itr_done;
774
775
776 /* simple throttlerate management
777 * 0-20MB/s lowest (100000 ints/s)
778 * 20-100MB/s low (20000 ints/s)
779 * 100-1249MB/s bulk (8000 ints/s)
780 */
781 /* what was last interrupt timeslice? */
782 timepassed_us = 1000000/eitr;
783 bytes_perint = bytes / timepassed_us; /* bytes/usec */
784
785 switch (itr_setting) {
786 case lowest_latency:
787 if (bytes_perint > adapter->eitr_low)
788 retval = low_latency;
789 break;
790 case low_latency:
791 if (bytes_perint > adapter->eitr_high)
792 retval = bulk_latency;
793 else if (bytes_perint <= adapter->eitr_low)
794 retval = lowest_latency;
795 break;
796 case bulk_latency:
797 if (bytes_perint <= adapter->eitr_high)
798 retval = low_latency;
799 break;
800 }
801
802update_itr_done:
803 return retval;
804}
805
806static void ixgbe_set_itr_msix(struct ixgbe_q_vector *q_vector)
807{
808 struct ixgbe_adapter *adapter = q_vector->adapter;
809 struct ixgbe_hw *hw = &adapter->hw;
810 u32 new_itr;
811 u8 current_itr, ret_itr;
812 int i, r_idx, v_idx = ((void *)q_vector - (void *)(adapter->q_vector)) /
813 sizeof(struct ixgbe_q_vector);
814 struct ixgbe_ring *rx_ring, *tx_ring;
815
816 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
817 for (i = 0; i < q_vector->txr_count; i++) {
818 tx_ring = &(adapter->tx_ring[r_idx]);
819 ret_itr = ixgbe_update_itr(adapter, q_vector->eitr,
820 q_vector->tx_eitr,
821 tx_ring->total_packets,
822 tx_ring->total_bytes);
823 /* if the result for this queue would decrease interrupt
824 * rate for this vector then use that result */
825 q_vector->tx_eitr = ((q_vector->tx_eitr > ret_itr) ?
826 q_vector->tx_eitr - 1 : ret_itr);
827 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
828 r_idx + 1);
829 }
830
831 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
832 for (i = 0; i < q_vector->rxr_count; i++) {
833 rx_ring = &(adapter->rx_ring[r_idx]);
834 ret_itr = ixgbe_update_itr(adapter, q_vector->eitr,
835 q_vector->rx_eitr,
836 rx_ring->total_packets,
837 rx_ring->total_bytes);
838 /* if the result for this queue would decrease interrupt
839 * rate for this vector then use that result */
840 q_vector->rx_eitr = ((q_vector->rx_eitr > ret_itr) ?
841 q_vector->rx_eitr - 1 : ret_itr);
842 r_idx = find_next_bit(q_vector->rxr_idx, adapter->num_rx_queues,
843 r_idx + 1);
844 }
845
846 current_itr = max(q_vector->rx_eitr, q_vector->tx_eitr);
847
848 switch (current_itr) {
849 /* counts and packets in update_itr are dependent on these numbers */
850 case lowest_latency:
851 new_itr = 100000;
852 break;
853 case low_latency:
854 new_itr = 20000; /* aka hwitr = ~200 */
855 break;
856 case bulk_latency:
857 default:
858 new_itr = 8000;
859 break;
860 }
861
862 if (new_itr != q_vector->eitr) {
863 u32 itr_reg;
864 /* do an exponential smoothing */
865 new_itr = ((q_vector->eitr * 90)/100) + ((new_itr * 10)/100);
866 q_vector->eitr = new_itr;
867 itr_reg = EITR_INTS_PER_SEC_TO_REG(new_itr);
868 /* must write high and low 16 bits to reset counter */
869 DPRINTK(TX_ERR, DEBUG, "writing eitr(%d): %08X\n", v_idx,
870 itr_reg);
871 IXGBE_WRITE_REG(hw, IXGBE_EITR(v_idx), itr_reg | (itr_reg)<<16);
872 }
873
874 return;
875}
876
Auke Kok9a799d72007-09-15 14:07:45 -0700877static irqreturn_t ixgbe_msix_lsc(int irq, void *data)
878{
879 struct net_device *netdev = data;
880 struct ixgbe_adapter *adapter = netdev_priv(netdev);
881 struct ixgbe_hw *hw = &adapter->hw;
882 u32 eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
883
884 if (eicr & IXGBE_EICR_LSC) {
885 adapter->lsc_int++;
886 if (!test_bit(__IXGBE_DOWN, &adapter->state))
887 mod_timer(&adapter->watchdog_timer, jiffies);
888 }
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -0800889
890 if (!test_bit(__IXGBE_DOWN, &adapter->state))
891 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_OTHER);
Auke Kok9a799d72007-09-15 14:07:45 -0700892
893 return IRQ_HANDLED;
894}
895
896static irqreturn_t ixgbe_msix_clean_tx(int irq, void *data)
897{
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800898 struct ixgbe_q_vector *q_vector = data;
899 struct ixgbe_adapter *adapter = q_vector->adapter;
900 struct ixgbe_ring *txr;
901 int i, r_idx;
Auke Kok9a799d72007-09-15 14:07:45 -0700902
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800903 if (!q_vector->txr_count)
904 return IRQ_HANDLED;
905
906 r_idx = find_first_bit(q_vector->txr_idx, adapter->num_tx_queues);
907 for (i = 0; i < q_vector->txr_count; i++) {
908 txr = &(adapter->tx_ring[r_idx]);
Jeb Cramerbd0362d2008-03-03 15:04:02 -0800909#ifdef CONFIG_DCA
910 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
911 ixgbe_update_tx_dca(adapter, txr);
912#endif
Ayyappan Veeraiyanf494e8f2008-03-03 15:03:57 -0800913 txr->total_bytes = 0;
914 txr->total_packets = 0;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800915 ixgbe_clean_tx_irq(adapter, txr);
916 r_idx = find_next_bit(q_vector->txr_idx, adapter->num_tx_queues,
917 r_idx + 1);
918 }
919
Auke Kok9a799d72007-09-15 14:07:45 -0700920 return IRQ_HANDLED;
921}
922
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800923/**
924 * ixgbe_msix_clean_rx - single unshared vector rx clean (all queues)
925 * @irq: unused
926 * @data: pointer to our q_vector struct for this interrupt vector
927 **/
Auke Kok9a799d72007-09-15 14:07:45 -0700928static irqreturn_t ixgbe_msix_clean_rx(int irq, void *data)
929{
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800930 struct ixgbe_q_vector *q_vector = data;
931 struct ixgbe_adapter *adapter = q_vector->adapter;
932 struct ixgbe_ring *rxr;
933 int r_idx;
Auke Kok9a799d72007-09-15 14:07:45 -0700934
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800935 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
936 if (!q_vector->rxr_count)
937 return IRQ_HANDLED;
938
939 rxr = &(adapter->rx_ring[r_idx]);
940 /* disable interrupts on this vector only */
941 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, rxr->v_idx);
Ayyappan Veeraiyanf494e8f2008-03-03 15:03:57 -0800942 rxr->total_bytes = 0;
943 rxr->total_packets = 0;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800944 netif_rx_schedule(adapter->netdev, &q_vector->napi);
945
Auke Kok9a799d72007-09-15 14:07:45 -0700946 return IRQ_HANDLED;
947}
948
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800949static irqreturn_t ixgbe_msix_clean_many(int irq, void *data)
950{
951 ixgbe_msix_clean_rx(irq, data);
952 ixgbe_msix_clean_tx(irq, data);
953
954 return IRQ_HANDLED;
955}
956
957/**
958 * ixgbe_clean_rxonly - msix (aka one shot) rx clean routine
959 * @napi: napi struct with our devices info in it
960 * @budget: amount of work driver is allowed to do this pass, in packets
961 *
962 **/
Auke Kok9a799d72007-09-15 14:07:45 -0700963static int ixgbe_clean_rxonly(struct napi_struct *napi, int budget)
964{
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800965 struct ixgbe_q_vector *q_vector =
966 container_of(napi, struct ixgbe_q_vector, napi);
967 struct ixgbe_adapter *adapter = q_vector->adapter;
968 struct ixgbe_ring *rxr;
Auke Kok9a799d72007-09-15 14:07:45 -0700969 int work_done = 0;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800970 long r_idx;
Auke Kok9a799d72007-09-15 14:07:45 -0700971
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800972 r_idx = find_first_bit(q_vector->rxr_idx, adapter->num_rx_queues);
973 rxr = &(adapter->rx_ring[r_idx]);
Jeb Cramerbd0362d2008-03-03 15:04:02 -0800974#ifdef CONFIG_DCA
975 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED)
976 ixgbe_update_rx_dca(adapter, rxr);
977#endif
Auke Kok9a799d72007-09-15 14:07:45 -0700978
979 ixgbe_clean_rx_irq(adapter, rxr, &work_done, budget);
980
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800981 /* If all Rx work done, exit the polling mode */
982 if (work_done < budget) {
983 netif_rx_complete(adapter->netdev, napi);
Ayyappan Veeraiyanf494e8f2008-03-03 15:03:57 -0800984 if (adapter->rx_eitr < IXGBE_MIN_ITR_USECS)
985 ixgbe_set_itr_msix(q_vector);
Auke Kok9a799d72007-09-15 14:07:45 -0700986 if (!test_bit(__IXGBE_DOWN, &adapter->state))
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800987 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, rxr->v_idx);
Auke Kok9a799d72007-09-15 14:07:45 -0700988 }
989
990 return work_done;
991}
992
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800993static inline void map_vector_to_rxq(struct ixgbe_adapter *a, int v_idx,
994 int r_idx)
Auke Kok9a799d72007-09-15 14:07:45 -0700995{
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -0800996 a->q_vector[v_idx].adapter = a;
997 set_bit(r_idx, a->q_vector[v_idx].rxr_idx);
998 a->q_vector[v_idx].rxr_count++;
999 a->rx_ring[r_idx].v_idx = 1 << v_idx;
1000}
Auke Kok9a799d72007-09-15 14:07:45 -07001001
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001002static inline void map_vector_to_txq(struct ixgbe_adapter *a, int v_idx,
1003 int r_idx)
1004{
1005 a->q_vector[v_idx].adapter = a;
1006 set_bit(r_idx, a->q_vector[v_idx].txr_idx);
1007 a->q_vector[v_idx].txr_count++;
1008 a->tx_ring[r_idx].v_idx = 1 << v_idx;
1009}
Auke Kok9a799d72007-09-15 14:07:45 -07001010
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001011/**
1012 * ixgbe_map_rings_to_vectors - Maps descriptor rings to vectors
1013 * @adapter: board private structure to initialize
1014 * @vectors: allotted vector count for descriptor rings
1015 *
1016 * This function maps descriptor rings to the queue-specific vectors
1017 * we were allotted through the MSI-X enabling code. Ideally, we'd have
1018 * one vector per ring/queue, but on a constrained vector budget, we
1019 * group the rings as "efficiently" as possible. You would add new
1020 * mapping configurations in here.
1021 **/
1022static int ixgbe_map_rings_to_vectors(struct ixgbe_adapter *adapter,
1023 int vectors)
1024{
1025 int v_start = 0;
1026 int rxr_idx = 0, txr_idx = 0;
1027 int rxr_remaining = adapter->num_rx_queues;
1028 int txr_remaining = adapter->num_tx_queues;
1029 int i, j;
1030 int rqpv, tqpv;
1031 int err = 0;
Auke Kok9a799d72007-09-15 14:07:45 -07001032
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001033 /* No mapping required if MSI-X is disabled. */
1034 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
Auke Kok9a799d72007-09-15 14:07:45 -07001035 goto out;
1036
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001037 /*
1038 * The ideal configuration...
1039 * We have enough vectors to map one per queue.
1040 */
1041 if (vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
1042 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
1043 map_vector_to_rxq(adapter, v_start, rxr_idx);
1044
1045 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
1046 map_vector_to_txq(adapter, v_start, txr_idx);
1047
1048 goto out;
1049 }
1050
1051 /*
1052 * If we don't have enough vectors for a 1-to-1
1053 * mapping, we'll have to group them so there are
1054 * multiple queues per vector.
1055 */
1056 /* Re-adjusting *qpv takes care of the remainder. */
1057 for (i = v_start; i < vectors; i++) {
1058 rqpv = DIV_ROUND_UP(rxr_remaining, vectors - i);
1059 for (j = 0; j < rqpv; j++) {
1060 map_vector_to_rxq(adapter, i, rxr_idx);
1061 rxr_idx++;
1062 rxr_remaining--;
Auke Kok9a799d72007-09-15 14:07:45 -07001063 }
Auke Kok9a799d72007-09-15 14:07:45 -07001064 }
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001065 for (i = v_start; i < vectors; i++) {
1066 tqpv = DIV_ROUND_UP(txr_remaining, vectors - i);
1067 for (j = 0; j < tqpv; j++) {
1068 map_vector_to_txq(adapter, i, txr_idx);
1069 txr_idx++;
1070 txr_remaining--;
Auke Kok9a799d72007-09-15 14:07:45 -07001071 }
Auke Kok9a799d72007-09-15 14:07:45 -07001072 }
1073
Auke Kok9a799d72007-09-15 14:07:45 -07001074out:
Auke Kok9a799d72007-09-15 14:07:45 -07001075 return err;
1076}
1077
1078/**
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001079 * ixgbe_request_msix_irqs - Initialize MSI-X interrupts
1080 * @adapter: board private structure
1081 *
1082 * ixgbe_request_msix_irqs allocates MSI-X vectors and requests
1083 * interrupts from the kernel.
1084 **/
1085static int ixgbe_request_msix_irqs(struct ixgbe_adapter *adapter)
1086{
1087 struct net_device *netdev = adapter->netdev;
1088 irqreturn_t (*handler)(int, void *);
1089 int i, vector, q_vectors, err;
1090
1091 /* Decrement for Other and TCP Timer vectors */
1092 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1093
1094 /* Map the Tx/Rx rings to the vectors we were allotted. */
1095 err = ixgbe_map_rings_to_vectors(adapter, q_vectors);
1096 if (err)
1097 goto out;
1098
1099#define SET_HANDLER(_v) ((!(_v)->rxr_count) ? &ixgbe_msix_clean_tx : \
1100 (!(_v)->txr_count) ? &ixgbe_msix_clean_rx : \
1101 &ixgbe_msix_clean_many)
1102 for (vector = 0; vector < q_vectors; vector++) {
1103 handler = SET_HANDLER(&adapter->q_vector[vector]);
1104 sprintf(adapter->name[vector], "%s:v%d-%s",
1105 netdev->name, vector,
1106 (handler == &ixgbe_msix_clean_rx) ? "Rx" :
1107 ((handler == &ixgbe_msix_clean_tx) ? "Tx" : "TxRx"));
1108 err = request_irq(adapter->msix_entries[vector].vector,
1109 handler, 0, adapter->name[vector],
1110 &(adapter->q_vector[vector]));
1111 if (err) {
1112 DPRINTK(PROBE, ERR,
1113 "request_irq failed for MSIX interrupt "
1114 "Error: %d\n", err);
1115 goto free_queue_irqs;
1116 }
1117 }
1118
1119 sprintf(adapter->name[vector], "%s:lsc", netdev->name);
1120 err = request_irq(adapter->msix_entries[vector].vector,
1121 &ixgbe_msix_lsc, 0, adapter->name[vector], netdev);
1122 if (err) {
1123 DPRINTK(PROBE, ERR,
1124 "request_irq for msix_lsc failed: %d\n", err);
1125 goto free_queue_irqs;
1126 }
1127
1128 return 0;
1129
1130free_queue_irqs:
1131 for (i = vector - 1; i >= 0; i--)
1132 free_irq(adapter->msix_entries[--vector].vector,
1133 &(adapter->q_vector[i]));
1134 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
1135 pci_disable_msix(adapter->pdev);
1136 kfree(adapter->msix_entries);
1137 adapter->msix_entries = NULL;
1138out:
1139 return err;
1140}
1141
Ayyappan Veeraiyanf494e8f2008-03-03 15:03:57 -08001142static void ixgbe_set_itr(struct ixgbe_adapter *adapter)
1143{
1144 struct ixgbe_hw *hw = &adapter->hw;
1145 struct ixgbe_q_vector *q_vector = adapter->q_vector;
1146 u8 current_itr;
1147 u32 new_itr = q_vector->eitr;
1148 struct ixgbe_ring *rx_ring = &adapter->rx_ring[0];
1149 struct ixgbe_ring *tx_ring = &adapter->tx_ring[0];
1150
1151 q_vector->tx_eitr = ixgbe_update_itr(adapter, new_itr,
1152 q_vector->tx_eitr,
1153 tx_ring->total_packets,
1154 tx_ring->total_bytes);
1155 q_vector->rx_eitr = ixgbe_update_itr(adapter, new_itr,
1156 q_vector->rx_eitr,
1157 rx_ring->total_packets,
1158 rx_ring->total_bytes);
1159
1160 current_itr = max(q_vector->rx_eitr, q_vector->tx_eitr);
1161
1162 switch (current_itr) {
1163 /* counts and packets in update_itr are dependent on these numbers */
1164 case lowest_latency:
1165 new_itr = 100000;
1166 break;
1167 case low_latency:
1168 new_itr = 20000; /* aka hwitr = ~200 */
1169 break;
1170 case bulk_latency:
1171 new_itr = 8000;
1172 break;
1173 default:
1174 break;
1175 }
1176
1177 if (new_itr != q_vector->eitr) {
1178 u32 itr_reg;
1179 /* do an exponential smoothing */
1180 new_itr = ((q_vector->eitr * 90)/100) + ((new_itr * 10)/100);
1181 q_vector->eitr = new_itr;
1182 itr_reg = EITR_INTS_PER_SEC_TO_REG(new_itr);
1183 /* must write high and low 16 bits to reset counter */
1184 IXGBE_WRITE_REG(hw, IXGBE_EITR(0), itr_reg | (itr_reg)<<16);
1185 }
1186
1187 return;
1188}
1189
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001190static inline void ixgbe_irq_enable(struct ixgbe_adapter *adapter);
1191
1192/**
1193 * ixgbe_intr - legacy mode Interrupt Handler
Auke Kok9a799d72007-09-15 14:07:45 -07001194 * @irq: interrupt number
1195 * @data: pointer to a network interface device structure
1196 * @pt_regs: CPU registers structure
1197 **/
1198static irqreturn_t ixgbe_intr(int irq, void *data)
1199{
1200 struct net_device *netdev = data;
1201 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1202 struct ixgbe_hw *hw = &adapter->hw;
1203 u32 eicr;
1204
Auke Kok9a799d72007-09-15 14:07:45 -07001205
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001206 /* for NAPI, using EIAM to auto-mask tx/rx interrupt bits on read
1207 * therefore no explict interrupt disable is necessary */
1208 eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
Auke Kok9a799d72007-09-15 14:07:45 -07001209 if (!eicr)
1210 return IRQ_NONE; /* Not our interrupt */
1211
1212 if (eicr & IXGBE_EICR_LSC) {
1213 adapter->lsc_int++;
1214 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1215 mod_timer(&adapter->watchdog_timer, jiffies);
1216 }
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001217
1218
1219 if (netif_rx_schedule_prep(netdev, &adapter->q_vector[0].napi)) {
Ayyappan Veeraiyanf494e8f2008-03-03 15:03:57 -08001220 adapter->tx_ring[0].total_packets = 0;
1221 adapter->tx_ring[0].total_bytes = 0;
1222 adapter->rx_ring[0].total_packets = 0;
1223 adapter->rx_ring[0].total_bytes = 0;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001224 /* would disable interrupts here but EIAM disabled it */
1225 __netif_rx_schedule(netdev, &adapter->q_vector[0].napi);
Auke Kok9a799d72007-09-15 14:07:45 -07001226 }
1227
1228 return IRQ_HANDLED;
1229}
1230
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001231static inline void ixgbe_reset_q_vectors(struct ixgbe_adapter *adapter)
1232{
1233 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1234
1235 for (i = 0; i < q_vectors; i++) {
1236 struct ixgbe_q_vector *q_vector = &adapter->q_vector[i];
1237 bitmap_zero(q_vector->rxr_idx, MAX_RX_QUEUES);
1238 bitmap_zero(q_vector->txr_idx, MAX_TX_QUEUES);
1239 q_vector->rxr_count = 0;
1240 q_vector->txr_count = 0;
1241 }
1242}
1243
Auke Kok9a799d72007-09-15 14:07:45 -07001244/**
1245 * ixgbe_request_irq - initialize interrupts
1246 * @adapter: board private structure
1247 *
1248 * Attempts to configure interrupts using the best available
1249 * capabilities of the hardware and kernel.
1250 **/
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001251static int ixgbe_request_irq(struct ixgbe_adapter *adapter)
Auke Kok9a799d72007-09-15 14:07:45 -07001252{
1253 struct net_device *netdev = adapter->netdev;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001254 int err;
Auke Kok9a799d72007-09-15 14:07:45 -07001255
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001256 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
1257 err = ixgbe_request_msix_irqs(adapter);
1258 } else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED) {
1259 err = request_irq(adapter->pdev->irq, &ixgbe_intr, 0,
1260 netdev->name, netdev);
1261 } else {
1262 err = request_irq(adapter->pdev->irq, &ixgbe_intr, IRQF_SHARED,
1263 netdev->name, netdev);
Auke Kok9a799d72007-09-15 14:07:45 -07001264 }
1265
Auke Kok9a799d72007-09-15 14:07:45 -07001266 if (err)
1267 DPRINTK(PROBE, ERR, "request_irq failed, Error %d\n", err);
1268
Auke Kok9a799d72007-09-15 14:07:45 -07001269 return err;
1270}
1271
1272static void ixgbe_free_irq(struct ixgbe_adapter *adapter)
1273{
1274 struct net_device *netdev = adapter->netdev;
1275
1276 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001277 int i, q_vectors;
Auke Kok9a799d72007-09-15 14:07:45 -07001278
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001279 q_vectors = adapter->num_msix_vectors;
1280
1281 i = q_vectors - 1;
Auke Kok9a799d72007-09-15 14:07:45 -07001282 free_irq(adapter->msix_entries[i].vector, netdev);
Auke Kok9a799d72007-09-15 14:07:45 -07001283
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001284 i--;
1285 for (; i >= 0; i--) {
1286 free_irq(adapter->msix_entries[i].vector,
1287 &(adapter->q_vector[i]));
1288 }
1289
1290 ixgbe_reset_q_vectors(adapter);
1291 } else {
1292 free_irq(adapter->pdev->irq, netdev);
Auke Kok9a799d72007-09-15 14:07:45 -07001293 }
1294}
1295
1296/**
1297 * ixgbe_irq_disable - Mask off interrupt generation on the NIC
1298 * @adapter: board private structure
1299 **/
1300static inline void ixgbe_irq_disable(struct ixgbe_adapter *adapter)
1301{
Auke Kok9a799d72007-09-15 14:07:45 -07001302 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~0);
1303 IXGBE_WRITE_FLUSH(&adapter->hw);
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001304 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
1305 int i;
1306 for (i = 0; i < adapter->num_msix_vectors; i++)
1307 synchronize_irq(adapter->msix_entries[i].vector);
1308 } else {
1309 synchronize_irq(adapter->pdev->irq);
1310 }
Auke Kok9a799d72007-09-15 14:07:45 -07001311}
1312
1313/**
1314 * ixgbe_irq_enable - Enable default interrupt generation settings
1315 * @adapter: board private structure
1316 **/
1317static inline void ixgbe_irq_enable(struct ixgbe_adapter *adapter)
1318{
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001319 u32 mask;
1320 mask = IXGBE_EIMS_ENABLE_MASK;
1321 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, mask);
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08001322 IXGBE_WRITE_FLUSH(&adapter->hw);
Auke Kok9a799d72007-09-15 14:07:45 -07001323}
1324
1325/**
1326 * ixgbe_configure_msi_and_legacy - Initialize PIN (INTA...) and MSI interrupts
1327 *
1328 **/
1329static void ixgbe_configure_msi_and_legacy(struct ixgbe_adapter *adapter)
1330{
Auke Kok9a799d72007-09-15 14:07:45 -07001331 struct ixgbe_hw *hw = &adapter->hw;
1332
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001333 IXGBE_WRITE_REG(hw, IXGBE_EITR(0),
1334 EITR_INTS_PER_SEC_TO_REG(adapter->rx_eitr));
Auke Kok9a799d72007-09-15 14:07:45 -07001335
1336 ixgbe_set_ivar(adapter, IXGBE_IVAR_RX_QUEUE(0), 0);
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001337 ixgbe_set_ivar(adapter, IXGBE_IVAR_TX_QUEUE(0), 0);
1338
1339 map_vector_to_rxq(adapter, 0, 0);
1340 map_vector_to_txq(adapter, 0, 0);
1341
1342 DPRINTK(HW, INFO, "Legacy interrupt IVAR setup done\n");
Auke Kok9a799d72007-09-15 14:07:45 -07001343}
1344
1345/**
1346 * ixgbe_configure_tx - Configure 8254x Transmit Unit after Reset
1347 * @adapter: board private structure
1348 *
1349 * Configure the Tx unit of the MAC after a reset.
1350 **/
1351static void ixgbe_configure_tx(struct ixgbe_adapter *adapter)
1352{
1353 u64 tdba;
1354 struct ixgbe_hw *hw = &adapter->hw;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001355 u32 i, j, tdlen, txctrl;
Auke Kok9a799d72007-09-15 14:07:45 -07001356
1357 /* Setup the HW Tx Head and Tail descriptor pointers */
1358 for (i = 0; i < adapter->num_tx_queues; i++) {
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001359 j = adapter->tx_ring[i].reg_idx;
Auke Kok9a799d72007-09-15 14:07:45 -07001360 tdba = adapter->tx_ring[i].dma;
1361 tdlen = adapter->tx_ring[i].count *
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001362 sizeof(union ixgbe_adv_tx_desc);
1363 IXGBE_WRITE_REG(hw, IXGBE_TDBAL(j),
1364 (tdba & DMA_32BIT_MASK));
1365 IXGBE_WRITE_REG(hw, IXGBE_TDBAH(j), (tdba >> 32));
1366 IXGBE_WRITE_REG(hw, IXGBE_TDLEN(j), tdlen);
1367 IXGBE_WRITE_REG(hw, IXGBE_TDH(j), 0);
1368 IXGBE_WRITE_REG(hw, IXGBE_TDT(j), 0);
1369 adapter->tx_ring[i].head = IXGBE_TDH(j);
1370 adapter->tx_ring[i].tail = IXGBE_TDT(j);
1371 /* Disable Tx Head Writeback RO bit, since this hoses
1372 * bookkeeping if things aren't delivered in order.
1373 */
1374 txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(i));
1375 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1376 IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(i), txctrl);
Auke Kok9a799d72007-09-15 14:07:45 -07001377 }
Auke Kok9a799d72007-09-15 14:07:45 -07001378}
1379
1380#define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
1381 (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
1382
1383#define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1384/**
1385 * ixgbe_configure_rx - Configure 8254x Receive Unit after Reset
1386 * @adapter: board private structure
1387 *
1388 * Configure the Rx unit of the MAC after a reset.
1389 **/
1390static void ixgbe_configure_rx(struct ixgbe_adapter *adapter)
1391{
1392 u64 rdba;
1393 struct ixgbe_hw *hw = &adapter->hw;
1394 struct net_device *netdev = adapter->netdev;
1395 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001396 int i, j;
Auke Kok9a799d72007-09-15 14:07:45 -07001397 u32 rdlen, rxctrl, rxcsum;
1398 u32 random[10];
Auke Kok9a799d72007-09-15 14:07:45 -07001399 u32 fctrl, hlreg0;
Auke Kok9a799d72007-09-15 14:07:45 -07001400 u32 pages;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001401 u32 reta = 0, mrqc, srrctl;
Auke Kok9a799d72007-09-15 14:07:45 -07001402
1403 /* Decide whether to use packet split mode or not */
1404 if (netdev->mtu > ETH_DATA_LEN)
1405 adapter->flags |= IXGBE_FLAG_RX_PS_ENABLED;
1406 else
1407 adapter->flags &= ~IXGBE_FLAG_RX_PS_ENABLED;
1408
1409 /* Set the RX buffer length according to the mode */
1410 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
1411 adapter->rx_buf_len = IXGBE_RX_HDR_SIZE;
1412 } else {
1413 if (netdev->mtu <= ETH_DATA_LEN)
1414 adapter->rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1415 else
1416 adapter->rx_buf_len = ALIGN(max_frame, 1024);
1417 }
1418
1419 fctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
1420 fctrl |= IXGBE_FCTRL_BAM;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001421 fctrl |= IXGBE_FCTRL_DPF; /* discard pause frames when FC enabled */
Auke Kok9a799d72007-09-15 14:07:45 -07001422 IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, fctrl);
1423
1424 hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
1425 if (adapter->netdev->mtu <= ETH_DATA_LEN)
1426 hlreg0 &= ~IXGBE_HLREG0_JUMBOEN;
1427 else
1428 hlreg0 |= IXGBE_HLREG0_JUMBOEN;
1429 IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
1430
1431 pages = PAGE_USE_COUNT(adapter->netdev->mtu);
1432
1433 srrctl = IXGBE_READ_REG(&adapter->hw, IXGBE_SRRCTL(0));
1434 srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
1435 srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
1436
1437 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
1438 srrctl |= PAGE_SIZE >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1439 srrctl |= IXGBE_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
1440 srrctl |= ((IXGBE_RX_HDR_SIZE <<
1441 IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT) &
1442 IXGBE_SRRCTL_BSIZEHDR_MASK);
1443 } else {
1444 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1445
1446 if (adapter->rx_buf_len == MAXIMUM_ETHERNET_VLAN_SIZE)
1447 srrctl |=
1448 IXGBE_RXBUFFER_2048 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1449 else
1450 srrctl |=
1451 adapter->rx_buf_len >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1452 }
1453 IXGBE_WRITE_REG(&adapter->hw, IXGBE_SRRCTL(0), srrctl);
1454
1455 rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1456 /* disable receives while setting up the descriptors */
1457 rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
1458 IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl & ~IXGBE_RXCTRL_RXEN);
1459
1460 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1461 * the Base and Length of the Rx Descriptor Ring */
1462 for (i = 0; i < adapter->num_rx_queues; i++) {
1463 rdba = adapter->rx_ring[i].dma;
1464 IXGBE_WRITE_REG(hw, IXGBE_RDBAL(i), (rdba & DMA_32BIT_MASK));
1465 IXGBE_WRITE_REG(hw, IXGBE_RDBAH(i), (rdba >> 32));
1466 IXGBE_WRITE_REG(hw, IXGBE_RDLEN(i), rdlen);
1467 IXGBE_WRITE_REG(hw, IXGBE_RDH(i), 0);
1468 IXGBE_WRITE_REG(hw, IXGBE_RDT(i), 0);
1469 adapter->rx_ring[i].head = IXGBE_RDH(i);
1470 adapter->rx_ring[i].tail = IXGBE_RDT(i);
1471 }
1472
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001473 if (adapter->flags & IXGBE_FLAG_RSS_ENABLED) {
Auke Kok9a799d72007-09-15 14:07:45 -07001474 /* Fill out redirection table */
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001475 for (i = 0, j = 0; i < 128; i++, j++) {
1476 if (j == adapter->ring_feature[RING_F_RSS].indices)
1477 j = 0;
1478 /* reta = 4-byte sliding window of
1479 * 0x00..(indices-1)(indices-1)00..etc. */
1480 reta = (reta << 8) | (j * 0x11);
1481 if ((i & 3) == 3)
1482 IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
Auke Kok9a799d72007-09-15 14:07:45 -07001483 }
1484
1485 /* Fill out hash function seeds */
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001486 /* XXX use a random constant here to glue certain flows */
1487 get_random_bytes(&random[0], 40);
Auke Kok9a799d72007-09-15 14:07:45 -07001488 for (i = 0; i < 10; i++)
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001489 IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), random[i]);
Auke Kok9a799d72007-09-15 14:07:45 -07001490
1491 mrqc = IXGBE_MRQC_RSSEN
1492 /* Perform hash on these packet types */
1493 | IXGBE_MRQC_RSS_FIELD_IPV4
1494 | IXGBE_MRQC_RSS_FIELD_IPV4_TCP
1495 | IXGBE_MRQC_RSS_FIELD_IPV4_UDP
1496 | IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP
1497 | IXGBE_MRQC_RSS_FIELD_IPV6_EX
1498 | IXGBE_MRQC_RSS_FIELD_IPV6
1499 | IXGBE_MRQC_RSS_FIELD_IPV6_TCP
1500 | IXGBE_MRQC_RSS_FIELD_IPV6_UDP
1501 | IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP;
1502 IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
Auke Kok9a799d72007-09-15 14:07:45 -07001503 }
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001504
1505 rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
1506
1507 if (adapter->flags & IXGBE_FLAG_RSS_ENABLED ||
1508 adapter->flags & IXGBE_FLAG_RX_CSUM_ENABLED) {
1509 /* Disable indicating checksum in descriptor, enables
1510 * RSS hash */
1511 rxcsum |= IXGBE_RXCSUM_PCSD;
1512 }
1513 if (!(rxcsum & IXGBE_RXCSUM_PCSD)) {
1514 /* Enable IPv4 payload checksum for UDP fragments
1515 * if PCSD is not set */
1516 rxcsum |= IXGBE_RXCSUM_IPPCSE;
1517 }
1518
1519 IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
Auke Kok9a799d72007-09-15 14:07:45 -07001520}
1521
1522static void ixgbe_vlan_rx_register(struct net_device *netdev,
1523 struct vlan_group *grp)
1524{
1525 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1526 u32 ctrl;
1527
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08001528 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1529 ixgbe_irq_disable(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07001530 adapter->vlgrp = grp;
1531
1532 if (grp) {
1533 /* enable VLAN tag insert/strip */
1534 ctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_VLNCTRL);
1535 ctrl |= IXGBE_VLNCTRL_VME | IXGBE_VLNCTRL_VFE;
1536 ctrl &= ~IXGBE_VLNCTRL_CFIEN;
1537 IXGBE_WRITE_REG(&adapter->hw, IXGBE_VLNCTRL, ctrl);
1538 }
1539
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08001540 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1541 ixgbe_irq_enable(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07001542}
1543
1544static void ixgbe_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1545{
1546 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1547
1548 /* add VID to filter table */
1549 ixgbe_set_vfta(&adapter->hw, vid, 0, true);
1550}
1551
1552static void ixgbe_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1553{
1554 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1555
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08001556 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1557 ixgbe_irq_disable(adapter);
1558
Auke Kok9a799d72007-09-15 14:07:45 -07001559 vlan_group_set_device(adapter->vlgrp, vid, NULL);
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08001560
1561 if (!test_bit(__IXGBE_DOWN, &adapter->state))
1562 ixgbe_irq_enable(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07001563
1564 /* remove VID from filter table */
1565 ixgbe_set_vfta(&adapter->hw, vid, 0, false);
1566}
1567
1568static void ixgbe_restore_vlan(struct ixgbe_adapter *adapter)
1569{
1570 ixgbe_vlan_rx_register(adapter->netdev, adapter->vlgrp);
1571
1572 if (adapter->vlgrp) {
1573 u16 vid;
1574 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
1575 if (!vlan_group_get_device(adapter->vlgrp, vid))
1576 continue;
1577 ixgbe_vlan_rx_add_vid(adapter->netdev, vid);
1578 }
1579 }
1580}
1581
1582/**
1583 * ixgbe_set_multi - Multicast and Promiscuous mode set
1584 * @netdev: network interface device structure
1585 *
1586 * The set_multi entry point is called whenever the multicast address
1587 * list or the network interface flags are updated. This routine is
1588 * responsible for configuring the hardware for proper multicast,
1589 * promiscuous mode, and all-multi behavior.
1590 **/
1591static void ixgbe_set_multi(struct net_device *netdev)
1592{
1593 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1594 struct ixgbe_hw *hw = &adapter->hw;
1595 struct dev_mc_list *mc_ptr;
1596 u8 *mta_list;
1597 u32 fctrl;
1598 int i;
1599
1600 /* Check for Promiscuous and All Multicast modes */
1601
1602 fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
1603
1604 if (netdev->flags & IFF_PROMISC) {
1605 fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
1606 } else if (netdev->flags & IFF_ALLMULTI) {
1607 fctrl |= IXGBE_FCTRL_MPE;
1608 fctrl &= ~IXGBE_FCTRL_UPE;
1609 } else {
1610 fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
1611 }
1612
1613 IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
1614
1615 if (netdev->mc_count) {
1616 mta_list = kcalloc(netdev->mc_count, ETH_ALEN, GFP_ATOMIC);
1617 if (!mta_list)
1618 return;
1619
1620 /* Shared function expects packed array of only addresses. */
1621 mc_ptr = netdev->mc_list;
1622
1623 for (i = 0; i < netdev->mc_count; i++) {
1624 if (!mc_ptr)
1625 break;
1626 memcpy(mta_list + (i * ETH_ALEN), mc_ptr->dmi_addr,
1627 ETH_ALEN);
1628 mc_ptr = mc_ptr->next;
1629 }
1630
1631 ixgbe_update_mc_addr_list(hw, mta_list, i, 0);
1632 kfree(mta_list);
1633 } else {
1634 ixgbe_update_mc_addr_list(hw, NULL, 0, 0);
1635 }
1636
1637}
1638
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001639static void ixgbe_napi_enable_all(struct ixgbe_adapter *adapter)
1640{
1641 int q_idx;
1642 struct ixgbe_q_vector *q_vector;
1643 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1644
1645 /* legacy and MSI only use one vector */
1646 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
1647 q_vectors = 1;
1648
1649 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1650 q_vector = &adapter->q_vector[q_idx];
1651 if (!q_vector->rxr_count)
1652 continue;
1653 napi_enable(&q_vector->napi);
1654 }
1655}
1656
1657static void ixgbe_napi_disable_all(struct ixgbe_adapter *adapter)
1658{
1659 int q_idx;
1660 struct ixgbe_q_vector *q_vector;
1661 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1662
1663 /* legacy and MSI only use one vector */
1664 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED))
1665 q_vectors = 1;
1666
1667 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1668 q_vector = &adapter->q_vector[q_idx];
1669 if (!q_vector->rxr_count)
1670 continue;
1671 napi_disable(&q_vector->napi);
1672 }
1673}
1674
Auke Kok9a799d72007-09-15 14:07:45 -07001675static void ixgbe_configure(struct ixgbe_adapter *adapter)
1676{
1677 struct net_device *netdev = adapter->netdev;
1678 int i;
1679
1680 ixgbe_set_multi(netdev);
1681
1682 ixgbe_restore_vlan(adapter);
1683
1684 ixgbe_configure_tx(adapter);
1685 ixgbe_configure_rx(adapter);
1686 for (i = 0; i < adapter->num_rx_queues; i++)
1687 ixgbe_alloc_rx_buffers(adapter, &adapter->rx_ring[i],
1688 (adapter->rx_ring[i].count - 1));
1689}
1690
1691static int ixgbe_up_complete(struct ixgbe_adapter *adapter)
1692{
1693 struct net_device *netdev = adapter->netdev;
Auke Kok9a799d72007-09-15 14:07:45 -07001694 struct ixgbe_hw *hw = &adapter->hw;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001695 int i, j = 0;
Auke Kok9a799d72007-09-15 14:07:45 -07001696 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001697 u32 txdctl, rxdctl, mhadd;
1698 u32 gpie;
Auke Kok9a799d72007-09-15 14:07:45 -07001699
Ayyappan Veeraiyan5eba3692008-02-01 15:59:04 -08001700 ixgbe_get_hw_control(adapter);
1701
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001702 if ((adapter->flags & IXGBE_FLAG_MSIX_ENABLED) ||
1703 (adapter->flags & IXGBE_FLAG_MSI_ENABLED)) {
Auke Kok9a799d72007-09-15 14:07:45 -07001704 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
1705 gpie = (IXGBE_GPIE_MSIX_MODE | IXGBE_GPIE_EIAME |
1706 IXGBE_GPIE_PBA_SUPPORT | IXGBE_GPIE_OCD);
1707 } else {
1708 /* MSI only */
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001709 gpie = 0;
Auke Kok9a799d72007-09-15 14:07:45 -07001710 }
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001711 /* XXX: to interrupt immediately for EICS writes, enable this */
1712 /* gpie |= IXGBE_GPIE_EIMEN; */
1713 IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
1714 }
1715
1716 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED)) {
1717 /* legacy interrupts, use EIAM to auto-mask when reading EICR,
1718 * specifically only auto mask tx and rx interrupts */
1719 IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
Auke Kok9a799d72007-09-15 14:07:45 -07001720 }
1721
1722 mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
Auke Kok9a799d72007-09-15 14:07:45 -07001723 if (max_frame != (mhadd >> IXGBE_MHADD_MFS_SHIFT)) {
1724 mhadd &= ~IXGBE_MHADD_MFS_MASK;
1725 mhadd |= max_frame << IXGBE_MHADD_MFS_SHIFT;
1726
1727 IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
1728 }
1729
1730 for (i = 0; i < adapter->num_tx_queues; i++) {
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001731 j = adapter->tx_ring[i].reg_idx;
1732 txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(j));
Auke Kok9a799d72007-09-15 14:07:45 -07001733 txdctl |= IXGBE_TXDCTL_ENABLE;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001734 IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(j), txdctl);
Auke Kok9a799d72007-09-15 14:07:45 -07001735 }
1736
1737 for (i = 0; i < adapter->num_rx_queues; i++) {
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001738 j = adapter->rx_ring[i].reg_idx;
1739 rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(j));
1740 /* enable PTHRESH=32 descriptors (half the internal cache)
1741 * and HTHRESH=0 descriptors (to minimize latency on fetch),
1742 * this also removes a pesky rx_no_buffer_count increment */
1743 rxdctl |= 0x0020;
Auke Kok9a799d72007-09-15 14:07:45 -07001744 rxdctl |= IXGBE_RXDCTL_ENABLE;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001745 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(j), rxdctl);
Auke Kok9a799d72007-09-15 14:07:45 -07001746 }
1747 /* enable all receives */
1748 rxdctl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
1749 rxdctl |= (IXGBE_RXCTRL_DMBYPS | IXGBE_RXCTRL_RXEN);
1750 IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxdctl);
1751
1752 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
1753 ixgbe_configure_msix(adapter);
1754 else
1755 ixgbe_configure_msi_and_legacy(adapter);
1756
1757 clear_bit(__IXGBE_DOWN, &adapter->state);
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001758 ixgbe_napi_enable_all(adapter);
1759
1760 /* clear any pending interrupts, may auto mask */
1761 IXGBE_READ_REG(hw, IXGBE_EICR);
1762
Auke Kok9a799d72007-09-15 14:07:45 -07001763 ixgbe_irq_enable(adapter);
1764
1765 /* bring the link up in the watchdog, this could race with our first
1766 * link up interrupt but shouldn't be a problem */
1767 mod_timer(&adapter->watchdog_timer, jiffies);
1768 return 0;
1769}
1770
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08001771void ixgbe_reinit_locked(struct ixgbe_adapter *adapter)
1772{
1773 WARN_ON(in_interrupt());
1774 while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
1775 msleep(1);
1776 ixgbe_down(adapter);
1777 ixgbe_up(adapter);
1778 clear_bit(__IXGBE_RESETTING, &adapter->state);
1779}
1780
Auke Kok9a799d72007-09-15 14:07:45 -07001781int ixgbe_up(struct ixgbe_adapter *adapter)
1782{
1783 /* hardware has been reset, we need to reload some things */
1784 ixgbe_configure(adapter);
1785
1786 return ixgbe_up_complete(adapter);
1787}
1788
1789void ixgbe_reset(struct ixgbe_adapter *adapter)
1790{
1791 if (ixgbe_init_hw(&adapter->hw))
1792 DPRINTK(PROBE, ERR, "Hardware Error\n");
1793
1794 /* reprogram the RAR[0] in case user changed it. */
1795 ixgbe_set_rar(&adapter->hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
1796
1797}
1798
1799#ifdef CONFIG_PM
1800static int ixgbe_resume(struct pci_dev *pdev)
1801{
1802 struct net_device *netdev = pci_get_drvdata(pdev);
1803 struct ixgbe_adapter *adapter = netdev_priv(netdev);
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001804 u32 err;
Auke Kok9a799d72007-09-15 14:07:45 -07001805
1806 pci_set_power_state(pdev, PCI_D0);
1807 pci_restore_state(pdev);
1808 err = pci_enable_device(pdev);
1809 if (err) {
1810 printk(KERN_ERR "ixgbe: Cannot enable PCI device from " \
1811 "suspend\n");
1812 return err;
1813 }
1814 pci_set_master(pdev);
1815
1816 pci_enable_wake(pdev, PCI_D3hot, 0);
1817 pci_enable_wake(pdev, PCI_D3cold, 0);
1818
1819 if (netif_running(netdev)) {
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001820 err = ixgbe_request_irq(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07001821 if (err)
1822 return err;
1823 }
1824
1825 ixgbe_reset(adapter);
1826
1827 if (netif_running(netdev))
1828 ixgbe_up(adapter);
1829
1830 netif_device_attach(netdev);
1831
1832 return 0;
1833}
1834#endif
1835
1836/**
1837 * ixgbe_clean_rx_ring - Free Rx Buffers per Queue
1838 * @adapter: board private structure
1839 * @rx_ring: ring to free buffers from
1840 **/
1841static void ixgbe_clean_rx_ring(struct ixgbe_adapter *adapter,
1842 struct ixgbe_ring *rx_ring)
1843{
1844 struct pci_dev *pdev = adapter->pdev;
1845 unsigned long size;
1846 unsigned int i;
1847
1848 /* Free all the Rx ring sk_buffs */
1849
1850 for (i = 0; i < rx_ring->count; i++) {
1851 struct ixgbe_rx_buffer *rx_buffer_info;
1852
1853 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1854 if (rx_buffer_info->dma) {
1855 pci_unmap_single(pdev, rx_buffer_info->dma,
1856 adapter->rx_buf_len,
1857 PCI_DMA_FROMDEVICE);
1858 rx_buffer_info->dma = 0;
1859 }
1860 if (rx_buffer_info->skb) {
1861 dev_kfree_skb(rx_buffer_info->skb);
1862 rx_buffer_info->skb = NULL;
1863 }
1864 if (!rx_buffer_info->page)
1865 continue;
1866 pci_unmap_page(pdev, rx_buffer_info->page_dma, PAGE_SIZE,
1867 PCI_DMA_FROMDEVICE);
1868 rx_buffer_info->page_dma = 0;
1869
1870 put_page(rx_buffer_info->page);
1871 rx_buffer_info->page = NULL;
1872 }
1873
1874 size = sizeof(struct ixgbe_rx_buffer) * rx_ring->count;
1875 memset(rx_ring->rx_buffer_info, 0, size);
1876
1877 /* Zero out the descriptor ring */
1878 memset(rx_ring->desc, 0, rx_ring->size);
1879
1880 rx_ring->next_to_clean = 0;
1881 rx_ring->next_to_use = 0;
1882
1883 writel(0, adapter->hw.hw_addr + rx_ring->head);
1884 writel(0, adapter->hw.hw_addr + rx_ring->tail);
1885}
1886
1887/**
1888 * ixgbe_clean_tx_ring - Free Tx Buffers
1889 * @adapter: board private structure
1890 * @tx_ring: ring to be cleaned
1891 **/
1892static void ixgbe_clean_tx_ring(struct ixgbe_adapter *adapter,
1893 struct ixgbe_ring *tx_ring)
1894{
1895 struct ixgbe_tx_buffer *tx_buffer_info;
1896 unsigned long size;
1897 unsigned int i;
1898
1899 /* Free all the Tx ring sk_buffs */
1900
1901 for (i = 0; i < tx_ring->count; i++) {
1902 tx_buffer_info = &tx_ring->tx_buffer_info[i];
1903 ixgbe_unmap_and_free_tx_resource(adapter, tx_buffer_info);
1904 }
1905
1906 size = sizeof(struct ixgbe_tx_buffer) * tx_ring->count;
1907 memset(tx_ring->tx_buffer_info, 0, size);
1908
1909 /* Zero out the descriptor ring */
1910 memset(tx_ring->desc, 0, tx_ring->size);
1911
1912 tx_ring->next_to_use = 0;
1913 tx_ring->next_to_clean = 0;
1914
1915 writel(0, adapter->hw.hw_addr + tx_ring->head);
1916 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1917}
1918
1919/**
Auke Kok9a799d72007-09-15 14:07:45 -07001920 * ixgbe_clean_all_rx_rings - Free Rx Buffers for all queues
1921 * @adapter: board private structure
1922 **/
1923static void ixgbe_clean_all_rx_rings(struct ixgbe_adapter *adapter)
1924{
1925 int i;
1926
1927 for (i = 0; i < adapter->num_rx_queues; i++)
1928 ixgbe_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1929}
1930
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001931/**
1932 * ixgbe_clean_all_tx_rings - Free Tx Buffers for all queues
1933 * @adapter: board private structure
1934 **/
1935static void ixgbe_clean_all_tx_rings(struct ixgbe_adapter *adapter)
1936{
1937 int i;
1938
1939 for (i = 0; i < adapter->num_tx_queues; i++)
1940 ixgbe_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1941}
1942
Auke Kok9a799d72007-09-15 14:07:45 -07001943void ixgbe_down(struct ixgbe_adapter *adapter)
1944{
1945 struct net_device *netdev = adapter->netdev;
1946 u32 rxctrl;
1947
1948 /* signal that we are down to the interrupt handler */
1949 set_bit(__IXGBE_DOWN, &adapter->state);
1950
1951 /* disable receives */
1952 rxctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_RXCTRL);
1953 IXGBE_WRITE_REG(&adapter->hw, IXGBE_RXCTRL,
1954 rxctrl & ~IXGBE_RXCTRL_RXEN);
1955
1956 netif_tx_disable(netdev);
1957
1958 /* disable transmits in the hardware */
1959
1960 /* flush both disables */
1961 IXGBE_WRITE_FLUSH(&adapter->hw);
1962 msleep(10);
1963
1964 ixgbe_irq_disable(adapter);
1965
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08001966 ixgbe_napi_disable_all(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07001967 del_timer_sync(&adapter->watchdog_timer);
1968
1969 netif_carrier_off(netdev);
1970 netif_stop_queue(netdev);
1971
Paul Larson6f4a0e42008-06-24 17:00:56 -07001972 if (!pci_channel_offline(adapter->pdev))
1973 ixgbe_reset(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07001974 ixgbe_clean_all_tx_rings(adapter);
1975 ixgbe_clean_all_rx_rings(adapter);
1976
1977}
1978
1979static int ixgbe_suspend(struct pci_dev *pdev, pm_message_t state)
1980{
1981 struct net_device *netdev = pci_get_drvdata(pdev);
1982 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1983#ifdef CONFIG_PM
1984 int retval = 0;
1985#endif
1986
1987 netif_device_detach(netdev);
1988
1989 if (netif_running(netdev)) {
1990 ixgbe_down(adapter);
1991 ixgbe_free_irq(adapter);
1992 }
1993
1994#ifdef CONFIG_PM
1995 retval = pci_save_state(pdev);
1996 if (retval)
1997 return retval;
1998#endif
1999
2000 pci_enable_wake(pdev, PCI_D3hot, 0);
2001 pci_enable_wake(pdev, PCI_D3cold, 0);
2002
Ayyappan Veeraiyan5eba3692008-02-01 15:59:04 -08002003 ixgbe_release_hw_control(adapter);
2004
Auke Kok9a799d72007-09-15 14:07:45 -07002005 pci_disable_device(pdev);
2006
2007 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2008
2009 return 0;
2010}
2011
2012static void ixgbe_shutdown(struct pci_dev *pdev)
2013{
2014 ixgbe_suspend(pdev, PMSG_SUSPEND);
2015}
2016
2017/**
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002018 * ixgbe_poll - NAPI Rx polling callback
2019 * @napi: structure for representing this polling device
2020 * @budget: how many packets driver is allowed to clean
2021 *
2022 * This function is used for legacy and MSI, NAPI mode
Auke Kok9a799d72007-09-15 14:07:45 -07002023 **/
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002024static int ixgbe_poll(struct napi_struct *napi, int budget)
Auke Kok9a799d72007-09-15 14:07:45 -07002025{
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002026 struct ixgbe_q_vector *q_vector = container_of(napi,
2027 struct ixgbe_q_vector, napi);
2028 struct ixgbe_adapter *adapter = q_vector->adapter;
David S. Millerd2c7ddd2008-01-15 22:43:24 -08002029 int tx_cleaned = 0, work_done = 0;
Auke Kok9a799d72007-09-15 14:07:45 -07002030
Jeb Cramerbd0362d2008-03-03 15:04:02 -08002031#ifdef CONFIG_DCA
2032 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
2033 ixgbe_update_tx_dca(adapter, adapter->tx_ring);
2034 ixgbe_update_rx_dca(adapter, adapter->rx_ring);
2035 }
2036#endif
2037
David S. Millerd2c7ddd2008-01-15 22:43:24 -08002038 tx_cleaned = ixgbe_clean_tx_irq(adapter, adapter->tx_ring);
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002039 ixgbe_clean_rx_irq(adapter, adapter->rx_ring, &work_done, budget);
Auke Kok9a799d72007-09-15 14:07:45 -07002040
David S. Millerd2c7ddd2008-01-15 22:43:24 -08002041 if (tx_cleaned)
2042 work_done = budget;
2043
David S. Miller53e52c72008-01-07 21:06:12 -08002044 /* If budget not fully consumed, exit the polling mode */
2045 if (work_done < budget) {
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002046 netif_rx_complete(adapter->netdev, napi);
Ayyappan Veeraiyanf494e8f2008-03-03 15:03:57 -08002047 if (adapter->rx_eitr < IXGBE_MIN_ITR_USECS)
2048 ixgbe_set_itr(adapter);
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08002049 if (!test_bit(__IXGBE_DOWN, &adapter->state))
2050 ixgbe_irq_enable(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07002051 }
2052
2053 return work_done;
2054}
2055
2056/**
2057 * ixgbe_tx_timeout - Respond to a Tx Hang
2058 * @netdev: network interface device structure
2059 **/
2060static void ixgbe_tx_timeout(struct net_device *netdev)
2061{
2062 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2063
2064 /* Do the reset outside of interrupt context */
2065 schedule_work(&adapter->reset_task);
2066}
2067
2068static void ixgbe_reset_task(struct work_struct *work)
2069{
2070 struct ixgbe_adapter *adapter;
2071 adapter = container_of(work, struct ixgbe_adapter, reset_task);
2072
2073 adapter->tx_timeout_count++;
2074
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08002075 ixgbe_reinit_locked(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07002076}
2077
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002078static void ixgbe_acquire_msix_vectors(struct ixgbe_adapter *adapter,
2079 int vectors)
2080{
2081 int err, vector_threshold;
2082
2083 /* We'll want at least 3 (vector_threshold):
2084 * 1) TxQ[0] Cleanup
2085 * 2) RxQ[0] Cleanup
2086 * 3) Other (Link Status Change, etc.)
2087 * 4) TCP Timer (optional)
2088 */
2089 vector_threshold = MIN_MSIX_COUNT;
2090
2091 /* The more we get, the more we will assign to Tx/Rx Cleanup
2092 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
2093 * Right now, we simply care about how many we'll get; we'll
2094 * set them up later while requesting irq's.
2095 */
2096 while (vectors >= vector_threshold) {
2097 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
2098 vectors);
2099 if (!err) /* Success in acquiring all requested vectors. */
2100 break;
2101 else if (err < 0)
2102 vectors = 0; /* Nasty failure, quit now */
2103 else /* err == number of vectors we should try again with */
2104 vectors = err;
2105 }
2106
2107 if (vectors < vector_threshold) {
2108 /* Can't allocate enough MSI-X interrupts? Oh well.
2109 * This just means we'll go with either a single MSI
2110 * vector or fall back to legacy interrupts.
2111 */
2112 DPRINTK(HW, DEBUG, "Unable to allocate MSI-X interrupts\n");
2113 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
2114 kfree(adapter->msix_entries);
2115 adapter->msix_entries = NULL;
2116 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
2117 adapter->num_tx_queues = 1;
2118 adapter->num_rx_queues = 1;
2119 } else {
2120 adapter->flags |= IXGBE_FLAG_MSIX_ENABLED; /* Woot! */
2121 adapter->num_msix_vectors = vectors;
2122 }
2123}
2124
2125static void __devinit ixgbe_set_num_queues(struct ixgbe_adapter *adapter)
2126{
2127 int nrq, ntq;
2128 int feature_mask = 0, rss_i, rss_m;
2129
2130 /* Number of supported queues */
2131 switch (adapter->hw.mac.type) {
2132 case ixgbe_mac_82598EB:
2133 rss_i = adapter->ring_feature[RING_F_RSS].indices;
2134 rss_m = 0;
2135 feature_mask |= IXGBE_FLAG_RSS_ENABLED;
2136
2137 switch (adapter->flags & feature_mask) {
2138 case (IXGBE_FLAG_RSS_ENABLED):
2139 rss_m = 0xF;
2140 nrq = rss_i;
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -08002141#ifdef CONFIG_NETDEVICES_MULTIQUEUE
2142 ntq = rss_i;
2143#else
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002144 ntq = 1;
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -08002145#endif
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002146 break;
2147 case 0:
2148 default:
2149 rss_i = 0;
2150 rss_m = 0;
2151 nrq = 1;
2152 ntq = 1;
2153 break;
2154 }
2155
2156 adapter->ring_feature[RING_F_RSS].indices = rss_i;
2157 adapter->ring_feature[RING_F_RSS].mask = rss_m;
2158 break;
2159 default:
2160 nrq = 1;
2161 ntq = 1;
2162 break;
2163 }
2164
2165 adapter->num_rx_queues = nrq;
2166 adapter->num_tx_queues = ntq;
2167}
2168
2169/**
2170 * ixgbe_cache_ring_register - Descriptor ring to register mapping
2171 * @adapter: board private structure to initialize
2172 *
2173 * Once we know the feature-set enabled for the device, we'll cache
2174 * the register offset the descriptor ring is assigned to.
2175 **/
2176static void __devinit ixgbe_cache_ring_register(struct ixgbe_adapter *adapter)
2177{
2178 /* TODO: Remove all uses of the indices in the cases where multiple
2179 * features are OR'd together, if the feature set makes sense.
2180 */
2181 int feature_mask = 0, rss_i;
2182 int i, txr_idx, rxr_idx;
2183
2184 /* Number of supported queues */
2185 switch (adapter->hw.mac.type) {
2186 case ixgbe_mac_82598EB:
2187 rss_i = adapter->ring_feature[RING_F_RSS].indices;
2188 txr_idx = 0;
2189 rxr_idx = 0;
2190 feature_mask |= IXGBE_FLAG_RSS_ENABLED;
2191 switch (adapter->flags & feature_mask) {
2192 case (IXGBE_FLAG_RSS_ENABLED):
2193 for (i = 0; i < adapter->num_rx_queues; i++)
2194 adapter->rx_ring[i].reg_idx = i;
2195 for (i = 0; i < adapter->num_tx_queues; i++)
2196 adapter->tx_ring[i].reg_idx = i;
2197 break;
2198 case 0:
2199 default:
2200 break;
2201 }
2202 break;
2203 default:
2204 break;
2205 }
2206}
2207
Auke Kok9a799d72007-09-15 14:07:45 -07002208/**
2209 * ixgbe_alloc_queues - Allocate memory for all rings
2210 * @adapter: board private structure to initialize
2211 *
2212 * We allocate one ring per queue at run-time since we don't know the
2213 * number of queues at compile-time. The polling_netdev array is
2214 * intended for Multiqueue, but should work fine with a single queue.
2215 **/
2216static int __devinit ixgbe_alloc_queues(struct ixgbe_adapter *adapter)
2217{
2218 int i;
2219
2220 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
2221 sizeof(struct ixgbe_ring), GFP_KERNEL);
2222 if (!adapter->tx_ring)
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002223 goto err_tx_ring_allocation;
Auke Kok9a799d72007-09-15 14:07:45 -07002224
2225 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
2226 sizeof(struct ixgbe_ring), GFP_KERNEL);
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002227 if (!adapter->rx_ring)
2228 goto err_rx_ring_allocation;
2229
2230 for (i = 0; i < adapter->num_tx_queues; i++) {
2231 adapter->tx_ring[i].count = IXGBE_DEFAULT_TXD;
2232 adapter->tx_ring[i].queue_index = i;
2233 }
2234 for (i = 0; i < adapter->num_rx_queues; i++) {
2235 adapter->rx_ring[i].count = IXGBE_DEFAULT_RXD;
2236 adapter->rx_ring[i].queue_index = i;
Auke Kok9a799d72007-09-15 14:07:45 -07002237 }
2238
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002239 ixgbe_cache_ring_register(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07002240
2241 return 0;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002242
2243err_rx_ring_allocation:
2244 kfree(adapter->tx_ring);
2245err_tx_ring_allocation:
2246 return -ENOMEM;
2247}
2248
2249/**
2250 * ixgbe_set_interrupt_capability - set MSI-X or MSI if supported
2251 * @adapter: board private structure to initialize
2252 *
2253 * Attempt to configure the interrupts using the best available
2254 * capabilities of the hardware and the kernel.
2255 **/
2256static int __devinit ixgbe_set_interrupt_capability(struct ixgbe_adapter
2257 *adapter)
2258{
2259 int err = 0;
2260 int vector, v_budget;
2261
2262 /*
2263 * It's easy to be greedy for MSI-X vectors, but it really
2264 * doesn't do us much good if we have a lot more vectors
2265 * than CPU's. So let's be conservative and only ask for
2266 * (roughly) twice the number of vectors as there are CPU's.
2267 */
2268 v_budget = min(adapter->num_rx_queues + adapter->num_tx_queues,
2269 (int)(num_online_cpus() * 2)) + NON_Q_VECTORS;
2270
2271 /*
2272 * At the same time, hardware can only support a maximum of
2273 * MAX_MSIX_COUNT vectors. With features such as RSS and VMDq,
2274 * we can easily reach upwards of 64 Rx descriptor queues and
2275 * 32 Tx queues. Thus, we cap it off in those rare cases where
2276 * the cpu count also exceeds our vector limit.
2277 */
2278 v_budget = min(v_budget, MAX_MSIX_COUNT);
2279
2280 /* A failure in MSI-X entry allocation isn't fatal, but it does
2281 * mean we disable MSI-X capabilities of the adapter. */
2282 adapter->msix_entries = kcalloc(v_budget,
2283 sizeof(struct msix_entry), GFP_KERNEL);
2284 if (!adapter->msix_entries) {
2285 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
2286 ixgbe_set_num_queues(adapter);
2287 kfree(adapter->tx_ring);
2288 kfree(adapter->rx_ring);
2289 err = ixgbe_alloc_queues(adapter);
2290 if (err) {
2291 DPRINTK(PROBE, ERR, "Unable to allocate memory "
2292 "for queues\n");
2293 goto out;
2294 }
2295
2296 goto try_msi;
2297 }
2298
2299 for (vector = 0; vector < v_budget; vector++)
2300 adapter->msix_entries[vector].entry = vector;
2301
2302 ixgbe_acquire_msix_vectors(adapter, v_budget);
2303
2304 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
2305 goto out;
2306
2307try_msi:
2308 err = pci_enable_msi(adapter->pdev);
2309 if (!err) {
2310 adapter->flags |= IXGBE_FLAG_MSI_ENABLED;
2311 } else {
2312 DPRINTK(HW, DEBUG, "Unable to allocate MSI interrupt, "
2313 "falling back to legacy. Error: %d\n", err);
2314 /* reset err */
2315 err = 0;
2316 }
2317
2318out:
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -08002319#ifdef CONFIG_NETDEVICES_MULTIQUEUE
2320 /* Notify the stack of the (possibly) reduced Tx Queue count. */
2321 adapter->netdev->egress_subqueue_count = adapter->num_tx_queues;
2322#endif
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002323
2324 return err;
2325}
2326
2327static void ixgbe_reset_interrupt_capability(struct ixgbe_adapter *adapter)
2328{
2329 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
2330 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
2331 pci_disable_msix(adapter->pdev);
2332 kfree(adapter->msix_entries);
2333 adapter->msix_entries = NULL;
2334 } else if (adapter->flags & IXGBE_FLAG_MSI_ENABLED) {
2335 adapter->flags &= ~IXGBE_FLAG_MSI_ENABLED;
2336 pci_disable_msi(adapter->pdev);
2337 }
2338 return;
2339}
2340
2341/**
2342 * ixgbe_init_interrupt_scheme - Determine proper interrupt scheme
2343 * @adapter: board private structure to initialize
2344 *
2345 * We determine which interrupt scheme to use based on...
2346 * - Kernel support (MSI, MSI-X)
2347 * - which can be user-defined (via MODULE_PARAM)
2348 * - Hardware queue count (num_*_queues)
2349 * - defined by miscellaneous hardware support/features (RSS, etc.)
2350 **/
2351static int __devinit ixgbe_init_interrupt_scheme(struct ixgbe_adapter *adapter)
2352{
2353 int err;
2354
2355 /* Number of supported queues */
2356 ixgbe_set_num_queues(adapter);
2357
2358 err = ixgbe_alloc_queues(adapter);
2359 if (err) {
2360 DPRINTK(PROBE, ERR, "Unable to allocate memory for queues\n");
2361 goto err_alloc_queues;
2362 }
2363
2364 err = ixgbe_set_interrupt_capability(adapter);
2365 if (err) {
2366 DPRINTK(PROBE, ERR, "Unable to setup interrupt capabilities\n");
2367 goto err_set_interrupt;
2368 }
2369
2370 DPRINTK(DRV, INFO, "Multiqueue %s: Rx Queue count = %u, "
2371 "Tx Queue count = %u\n",
2372 (adapter->num_rx_queues > 1) ? "Enabled" :
2373 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
2374
2375 set_bit(__IXGBE_DOWN, &adapter->state);
2376
2377 return 0;
2378
2379err_set_interrupt:
2380 kfree(adapter->tx_ring);
2381 kfree(adapter->rx_ring);
2382err_alloc_queues:
2383 return err;
Auke Kok9a799d72007-09-15 14:07:45 -07002384}
2385
2386/**
2387 * ixgbe_sw_init - Initialize general software structures (struct ixgbe_adapter)
2388 * @adapter: board private structure to initialize
2389 *
2390 * ixgbe_sw_init initializes the Adapter private data structure.
2391 * Fields are initialized based on PCI device information and
2392 * OS network device settings (MTU size).
2393 **/
2394static int __devinit ixgbe_sw_init(struct ixgbe_adapter *adapter)
2395{
2396 struct ixgbe_hw *hw = &adapter->hw;
2397 struct pci_dev *pdev = adapter->pdev;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002398 unsigned int rss;
2399
2400 /* Set capability flags */
2401 rss = min(IXGBE_MAX_RSS_INDICES, (int)num_online_cpus());
2402 adapter->ring_feature[RING_F_RSS].indices = rss;
2403 adapter->flags |= IXGBE_FLAG_RSS_ENABLED;
Auke Kok9a799d72007-09-15 14:07:45 -07002404
Ayyappan Veeraiyanf494e8f2008-03-03 15:03:57 -08002405 /* Enable Dynamic interrupt throttling by default */
2406 adapter->rx_eitr = 1;
2407 adapter->tx_eitr = 1;
2408
Auke Kok9a799d72007-09-15 14:07:45 -07002409 /* default flow control settings */
2410 hw->fc.original_type = ixgbe_fc_full;
2411 hw->fc.type = ixgbe_fc_full;
2412
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002413 /* select 10G link by default */
Auke Kok9a799d72007-09-15 14:07:45 -07002414 hw->mac.link_mode_select = IXGBE_AUTOC_LMS_10G_LINK_NO_AN;
2415 if (hw->mac.ops.reset(hw)) {
2416 dev_err(&pdev->dev, "HW Init failed\n");
2417 return -EIO;
2418 }
Auke Kok3957d632007-10-31 15:22:10 -07002419 if (hw->mac.ops.setup_link_speed(hw, IXGBE_LINK_SPEED_10GB_FULL, true,
2420 false)) {
Auke Kok9a799d72007-09-15 14:07:45 -07002421 dev_err(&pdev->dev, "Link Speed setup failed\n");
2422 return -EIO;
2423 }
2424
2425 /* initialize eeprom parameters */
2426 if (ixgbe_init_eeprom(hw)) {
2427 dev_err(&pdev->dev, "EEPROM initialization failed\n");
2428 return -EIO;
2429 }
2430
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002431 /* enable rx csum by default */
Auke Kok9a799d72007-09-15 14:07:45 -07002432 adapter->flags |= IXGBE_FLAG_RX_CSUM_ENABLED;
2433
Auke Kok9a799d72007-09-15 14:07:45 -07002434 set_bit(__IXGBE_DOWN, &adapter->state);
2435
2436 return 0;
2437}
2438
2439/**
2440 * ixgbe_setup_tx_resources - allocate Tx resources (Descriptors)
2441 * @adapter: board private structure
2442 * @txdr: tx descriptor ring (for a specific queue) to setup
2443 *
2444 * Return 0 on success, negative on failure
2445 **/
2446int ixgbe_setup_tx_resources(struct ixgbe_adapter *adapter,
2447 struct ixgbe_ring *txdr)
2448{
2449 struct pci_dev *pdev = adapter->pdev;
2450 int size;
2451
2452 size = sizeof(struct ixgbe_tx_buffer) * txdr->count;
2453 txdr->tx_buffer_info = vmalloc(size);
2454 if (!txdr->tx_buffer_info) {
2455 DPRINTK(PROBE, ERR,
2456 "Unable to allocate memory for the transmit descriptor ring\n");
2457 return -ENOMEM;
2458 }
2459 memset(txdr->tx_buffer_info, 0, size);
2460
2461 /* round up to nearest 4K */
2462 txdr->size = txdr->count * sizeof(union ixgbe_adv_tx_desc);
2463 txdr->size = ALIGN(txdr->size, 4096);
2464
2465 txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
2466 if (!txdr->desc) {
2467 vfree(txdr->tx_buffer_info);
2468 DPRINTK(PROBE, ERR,
2469 "Memory allocation failed for the tx desc ring\n");
2470 return -ENOMEM;
2471 }
2472
Auke Kok9a799d72007-09-15 14:07:45 -07002473 txdr->next_to_use = 0;
2474 txdr->next_to_clean = 0;
2475 txdr->work_limit = txdr->count;
Auke Kok9a799d72007-09-15 14:07:45 -07002476
2477 return 0;
2478}
2479
2480/**
2481 * ixgbe_setup_rx_resources - allocate Rx resources (Descriptors)
2482 * @adapter: board private structure
2483 * @rxdr: rx descriptor ring (for a specific queue) to setup
2484 *
2485 * Returns 0 on success, negative on failure
2486 **/
2487int ixgbe_setup_rx_resources(struct ixgbe_adapter *adapter,
2488 struct ixgbe_ring *rxdr)
2489{
2490 struct pci_dev *pdev = adapter->pdev;
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002491 int size;
Auke Kok9a799d72007-09-15 14:07:45 -07002492
2493 size = sizeof(struct ixgbe_rx_buffer) * rxdr->count;
2494 rxdr->rx_buffer_info = vmalloc(size);
2495 if (!rxdr->rx_buffer_info) {
2496 DPRINTK(PROBE, ERR,
2497 "vmalloc allocation failed for the rx desc ring\n");
2498 return -ENOMEM;
2499 }
2500 memset(rxdr->rx_buffer_info, 0, size);
2501
Auke Kok9a799d72007-09-15 14:07:45 -07002502 /* Round up to nearest 4K */
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002503 rxdr->size = rxdr->count * sizeof(union ixgbe_adv_rx_desc);
Auke Kok9a799d72007-09-15 14:07:45 -07002504 rxdr->size = ALIGN(rxdr->size, 4096);
2505
2506 rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
2507
2508 if (!rxdr->desc) {
2509 DPRINTK(PROBE, ERR,
2510 "Memory allocation failed for the rx desc ring\n");
2511 vfree(rxdr->rx_buffer_info);
2512 return -ENOMEM;
2513 }
2514
2515 rxdr->next_to_clean = 0;
2516 rxdr->next_to_use = 0;
Auke Kok9a799d72007-09-15 14:07:45 -07002517
2518 return 0;
2519}
2520
2521/**
2522 * ixgbe_free_tx_resources - Free Tx Resources per Queue
2523 * @adapter: board private structure
2524 * @tx_ring: Tx descriptor ring for a specific queue
2525 *
2526 * Free all transmit software resources
2527 **/
2528static void ixgbe_free_tx_resources(struct ixgbe_adapter *adapter,
2529 struct ixgbe_ring *tx_ring)
2530{
2531 struct pci_dev *pdev = adapter->pdev;
2532
2533 ixgbe_clean_tx_ring(adapter, tx_ring);
2534
2535 vfree(tx_ring->tx_buffer_info);
2536 tx_ring->tx_buffer_info = NULL;
2537
2538 pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
2539
2540 tx_ring->desc = NULL;
2541}
2542
2543/**
2544 * ixgbe_free_all_tx_resources - Free Tx Resources for All Queues
2545 * @adapter: board private structure
2546 *
2547 * Free all transmit software resources
2548 **/
2549static void ixgbe_free_all_tx_resources(struct ixgbe_adapter *adapter)
2550{
2551 int i;
2552
2553 for (i = 0; i < adapter->num_tx_queues; i++)
2554 ixgbe_free_tx_resources(adapter, &adapter->tx_ring[i]);
2555}
2556
2557/**
2558 * ixgbe_free_rx_resources - Free Rx Resources
2559 * @adapter: board private structure
2560 * @rx_ring: ring to clean the resources from
2561 *
2562 * Free all receive software resources
2563 **/
2564static void ixgbe_free_rx_resources(struct ixgbe_adapter *adapter,
2565 struct ixgbe_ring *rx_ring)
2566{
2567 struct pci_dev *pdev = adapter->pdev;
2568
2569 ixgbe_clean_rx_ring(adapter, rx_ring);
2570
2571 vfree(rx_ring->rx_buffer_info);
2572 rx_ring->rx_buffer_info = NULL;
2573
2574 pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
2575
2576 rx_ring->desc = NULL;
2577}
2578
2579/**
2580 * ixgbe_free_all_rx_resources - Free Rx Resources for All Queues
2581 * @adapter: board private structure
2582 *
2583 * Free all receive software resources
2584 **/
2585static void ixgbe_free_all_rx_resources(struct ixgbe_adapter *adapter)
2586{
2587 int i;
2588
2589 for (i = 0; i < adapter->num_rx_queues; i++)
2590 ixgbe_free_rx_resources(adapter, &adapter->rx_ring[i]);
2591}
2592
2593/**
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002594 * ixgbe_setup_all_tx_resources - allocate all queues Tx resources
Auke Kok9a799d72007-09-15 14:07:45 -07002595 * @adapter: board private structure
2596 *
2597 * If this function returns with an error, then it's possible one or
2598 * more of the rings is populated (while the rest are not). It is the
2599 * callers duty to clean those orphaned rings.
2600 *
2601 * Return 0 on success, negative on failure
2602 **/
2603static int ixgbe_setup_all_tx_resources(struct ixgbe_adapter *adapter)
2604{
2605 int i, err = 0;
2606
2607 for (i = 0; i < adapter->num_tx_queues; i++) {
2608 err = ixgbe_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2609 if (err) {
2610 DPRINTK(PROBE, ERR,
2611 "Allocation for Tx Queue %u failed\n", i);
2612 break;
2613 }
2614 }
2615
2616 return err;
2617}
2618
2619/**
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002620 * ixgbe_setup_all_rx_resources - allocate all queues Rx resources
Auke Kok9a799d72007-09-15 14:07:45 -07002621 * @adapter: board private structure
2622 *
2623 * If this function returns with an error, then it's possible one or
2624 * more of the rings is populated (while the rest are not). It is the
2625 * callers duty to clean those orphaned rings.
2626 *
2627 * Return 0 on success, negative on failure
2628 **/
2629
2630static int ixgbe_setup_all_rx_resources(struct ixgbe_adapter *adapter)
2631{
2632 int i, err = 0;
2633
2634 for (i = 0; i < adapter->num_rx_queues; i++) {
2635 err = ixgbe_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2636 if (err) {
2637 DPRINTK(PROBE, ERR,
2638 "Allocation for Rx Queue %u failed\n", i);
2639 break;
2640 }
2641 }
2642
2643 return err;
2644}
2645
2646/**
2647 * ixgbe_change_mtu - Change the Maximum Transfer Unit
2648 * @netdev: network interface device structure
2649 * @new_mtu: new value for maximum frame size
2650 *
2651 * Returns 0 on success, negative on failure
2652 **/
2653static int ixgbe_change_mtu(struct net_device *netdev, int new_mtu)
2654{
2655 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2656 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2657
2658 if ((max_frame < (ETH_ZLEN + ETH_FCS_LEN)) ||
2659 (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE))
2660 return -EINVAL;
2661
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002662 DPRINTK(PROBE, INFO, "changing MTU from %d to %d\n",
2663 netdev->mtu, new_mtu);
2664 /* must set new MTU before calling down or up */
Auke Kok9a799d72007-09-15 14:07:45 -07002665 netdev->mtu = new_mtu;
2666
Ayyappan Veeraiyand4f80882008-02-01 15:58:41 -08002667 if (netif_running(netdev))
2668 ixgbe_reinit_locked(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07002669
2670 return 0;
2671}
2672
2673/**
2674 * ixgbe_open - Called when a network interface is made active
2675 * @netdev: network interface device structure
2676 *
2677 * Returns 0 on success, negative value on failure
2678 *
2679 * The open entry point is called when a network interface is made
2680 * active by the system (IFF_UP). At this point all resources needed
2681 * for transmit and receive operations are allocated, the interrupt
2682 * handler is registered with the OS, the watchdog timer is started,
2683 * and the stack is notified that the interface is ready.
2684 **/
2685static int ixgbe_open(struct net_device *netdev)
2686{
2687 struct ixgbe_adapter *adapter = netdev_priv(netdev);
2688 int err;
Auke Kok9a799d72007-09-15 14:07:45 -07002689
Auke Kok4bebfaa2008-02-11 09:26:01 -08002690 /* disallow open during test */
2691 if (test_bit(__IXGBE_TESTING, &adapter->state))
2692 return -EBUSY;
2693
Auke Kok9a799d72007-09-15 14:07:45 -07002694 /* allocate transmit descriptors */
2695 err = ixgbe_setup_all_tx_resources(adapter);
2696 if (err)
2697 goto err_setup_tx;
2698
Auke Kok9a799d72007-09-15 14:07:45 -07002699 /* allocate receive descriptors */
2700 err = ixgbe_setup_all_rx_resources(adapter);
2701 if (err)
2702 goto err_setup_rx;
2703
2704 ixgbe_configure(adapter);
2705
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002706 err = ixgbe_request_irq(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07002707 if (err)
2708 goto err_req_irq;
2709
Auke Kok9a799d72007-09-15 14:07:45 -07002710 err = ixgbe_up_complete(adapter);
2711 if (err)
2712 goto err_up;
2713
2714 return 0;
2715
2716err_up:
Ayyappan Veeraiyan5eba3692008-02-01 15:59:04 -08002717 ixgbe_release_hw_control(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07002718 ixgbe_free_irq(adapter);
2719err_req_irq:
2720 ixgbe_free_all_rx_resources(adapter);
2721err_setup_rx:
2722 ixgbe_free_all_tx_resources(adapter);
2723err_setup_tx:
2724 ixgbe_reset(adapter);
2725
2726 return err;
2727}
2728
2729/**
2730 * ixgbe_close - Disables a network interface
2731 * @netdev: network interface device structure
2732 *
2733 * Returns 0, this is not allowed to fail
2734 *
2735 * The close entry point is called when an interface is de-activated
2736 * by the OS. The hardware is still under the drivers control, but
2737 * needs to be disabled. A global MAC reset is issued to stop the
2738 * hardware, and all transmit and receive resources are freed.
2739 **/
2740static int ixgbe_close(struct net_device *netdev)
2741{
2742 struct ixgbe_adapter *adapter = netdev_priv(netdev);
Auke Kok9a799d72007-09-15 14:07:45 -07002743
2744 ixgbe_down(adapter);
2745 ixgbe_free_irq(adapter);
2746
2747 ixgbe_free_all_tx_resources(adapter);
2748 ixgbe_free_all_rx_resources(adapter);
2749
Ayyappan Veeraiyan5eba3692008-02-01 15:59:04 -08002750 ixgbe_release_hw_control(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07002751
2752 return 0;
2753}
2754
2755/**
2756 * ixgbe_update_stats - Update the board statistics counters.
2757 * @adapter: board private structure
2758 **/
2759void ixgbe_update_stats(struct ixgbe_adapter *adapter)
2760{
2761 struct ixgbe_hw *hw = &adapter->hw;
Ayyappan Veeraiyan6f11eef2008-02-01 15:59:14 -08002762 u64 total_mpc = 0;
2763 u32 i, missed_rx = 0, mpc, bprc, lxon, lxoff, xon_off_tot;
Auke Kok9a799d72007-09-15 14:07:45 -07002764
2765 adapter->stats.crcerrs += IXGBE_READ_REG(hw, IXGBE_CRCERRS);
Ayyappan Veeraiyan6f11eef2008-02-01 15:59:14 -08002766 for (i = 0; i < 8; i++) {
2767 /* for packet buffers not used, the register should read 0 */
2768 mpc = IXGBE_READ_REG(hw, IXGBE_MPC(i));
2769 missed_rx += mpc;
2770 adapter->stats.mpc[i] += mpc;
2771 total_mpc += adapter->stats.mpc[i];
2772 adapter->stats.rnbc[i] += IXGBE_READ_REG(hw, IXGBE_RNBC(i));
2773 }
2774 adapter->stats.gprc += IXGBE_READ_REG(hw, IXGBE_GPRC);
2775 /* work around hardware counting issue */
2776 adapter->stats.gprc -= missed_rx;
Auke Kok9a799d72007-09-15 14:07:45 -07002777
Ayyappan Veeraiyan6f11eef2008-02-01 15:59:14 -08002778 /* 82598 hardware only has a 32 bit counter in the high register */
Auke Kok9a799d72007-09-15 14:07:45 -07002779 adapter->stats.gorc += IXGBE_READ_REG(hw, IXGBE_GORCH);
Ayyappan Veeraiyan6f11eef2008-02-01 15:59:14 -08002780 adapter->stats.gotc += IXGBE_READ_REG(hw, IXGBE_GOTCH);
2781 adapter->stats.tor += IXGBE_READ_REG(hw, IXGBE_TORH);
Auke Kok9a799d72007-09-15 14:07:45 -07002782 bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
2783 adapter->stats.bprc += bprc;
2784 adapter->stats.mprc += IXGBE_READ_REG(hw, IXGBE_MPRC);
2785 adapter->stats.mprc -= bprc;
2786 adapter->stats.roc += IXGBE_READ_REG(hw, IXGBE_ROC);
2787 adapter->stats.prc64 += IXGBE_READ_REG(hw, IXGBE_PRC64);
2788 adapter->stats.prc127 += IXGBE_READ_REG(hw, IXGBE_PRC127);
2789 adapter->stats.prc255 += IXGBE_READ_REG(hw, IXGBE_PRC255);
2790 adapter->stats.prc511 += IXGBE_READ_REG(hw, IXGBE_PRC511);
2791 adapter->stats.prc1023 += IXGBE_READ_REG(hw, IXGBE_PRC1023);
2792 adapter->stats.prc1522 += IXGBE_READ_REG(hw, IXGBE_PRC1522);
Auke Kok9a799d72007-09-15 14:07:45 -07002793 adapter->stats.rlec += IXGBE_READ_REG(hw, IXGBE_RLEC);
2794 adapter->stats.lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
Auke Kok9a799d72007-09-15 14:07:45 -07002795 adapter->stats.lxoffrxc += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
Ayyappan Veeraiyan6f11eef2008-02-01 15:59:14 -08002796 lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
2797 adapter->stats.lxontxc += lxon;
2798 lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
2799 adapter->stats.lxofftxc += lxoff;
Auke Kok9a799d72007-09-15 14:07:45 -07002800 adapter->stats.ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
2801 adapter->stats.gptc += IXGBE_READ_REG(hw, IXGBE_GPTC);
Ayyappan Veeraiyan6f11eef2008-02-01 15:59:14 -08002802 adapter->stats.mptc += IXGBE_READ_REG(hw, IXGBE_MPTC);
2803 /*
2804 * 82598 errata - tx of flow control packets is included in tx counters
2805 */
2806 xon_off_tot = lxon + lxoff;
2807 adapter->stats.gptc -= xon_off_tot;
2808 adapter->stats.mptc -= xon_off_tot;
2809 adapter->stats.gotc -= (xon_off_tot * (ETH_ZLEN + ETH_FCS_LEN));
Auke Kok9a799d72007-09-15 14:07:45 -07002810 adapter->stats.ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
2811 adapter->stats.rfc += IXGBE_READ_REG(hw, IXGBE_RFC);
2812 adapter->stats.rjc += IXGBE_READ_REG(hw, IXGBE_RJC);
Auke Kok9a799d72007-09-15 14:07:45 -07002813 adapter->stats.tpr += IXGBE_READ_REG(hw, IXGBE_TPR);
2814 adapter->stats.ptc64 += IXGBE_READ_REG(hw, IXGBE_PTC64);
Ayyappan Veeraiyan6f11eef2008-02-01 15:59:14 -08002815 adapter->stats.ptc64 -= xon_off_tot;
Auke Kok9a799d72007-09-15 14:07:45 -07002816 adapter->stats.ptc127 += IXGBE_READ_REG(hw, IXGBE_PTC127);
2817 adapter->stats.ptc255 += IXGBE_READ_REG(hw, IXGBE_PTC255);
2818 adapter->stats.ptc511 += IXGBE_READ_REG(hw, IXGBE_PTC511);
2819 adapter->stats.ptc1023 += IXGBE_READ_REG(hw, IXGBE_PTC1023);
2820 adapter->stats.ptc1522 += IXGBE_READ_REG(hw, IXGBE_PTC1522);
Auke Kok9a799d72007-09-15 14:07:45 -07002821 adapter->stats.bptc += IXGBE_READ_REG(hw, IXGBE_BPTC);
2822
2823 /* Fill out the OS statistics structure */
Auke Kok9a799d72007-09-15 14:07:45 -07002824 adapter->net_stats.multicast = adapter->stats.mprc;
2825
2826 /* Rx Errors */
2827 adapter->net_stats.rx_errors = adapter->stats.crcerrs +
2828 adapter->stats.rlec;
2829 adapter->net_stats.rx_dropped = 0;
2830 adapter->net_stats.rx_length_errors = adapter->stats.rlec;
2831 adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
Ayyappan Veeraiyan6f11eef2008-02-01 15:59:14 -08002832 adapter->net_stats.rx_missed_errors = total_mpc;
Auke Kok9a799d72007-09-15 14:07:45 -07002833}
2834
2835/**
2836 * ixgbe_watchdog - Timer Call-back
2837 * @data: pointer to adapter cast into an unsigned long
2838 **/
2839static void ixgbe_watchdog(unsigned long data)
2840{
2841 struct ixgbe_adapter *adapter = (struct ixgbe_adapter *)data;
2842 struct net_device *netdev = adapter->netdev;
2843 bool link_up;
2844 u32 link_speed = 0;
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -08002845#ifdef CONFIG_NETDEVICES_MULTIQUEUE
2846 int i;
2847#endif
Auke Kok9a799d72007-09-15 14:07:45 -07002848
Auke Kok3957d632007-10-31 15:22:10 -07002849 adapter->hw.mac.ops.check_link(&adapter->hw, &(link_speed), &link_up);
Auke Kok9a799d72007-09-15 14:07:45 -07002850
2851 if (link_up) {
2852 if (!netif_carrier_ok(netdev)) {
2853 u32 frctl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
2854 u32 rmcs = IXGBE_READ_REG(&adapter->hw, IXGBE_RMCS);
2855#define FLOW_RX (frctl & IXGBE_FCTRL_RFCE)
2856#define FLOW_TX (rmcs & IXGBE_RMCS_TFCE_802_3X)
2857 DPRINTK(LINK, INFO, "NIC Link is Up %s, "
2858 "Flow Control: %s\n",
2859 (link_speed == IXGBE_LINK_SPEED_10GB_FULL ?
2860 "10 Gbps" :
2861 (link_speed == IXGBE_LINK_SPEED_1GB_FULL ?
Emil Tantilov5a059e92008-03-03 14:37:42 -08002862 "1 Gbps" : "unknown speed")),
Auke Kok9a799d72007-09-15 14:07:45 -07002863 ((FLOW_RX && FLOW_TX) ? "RX/TX" :
2864 (FLOW_RX ? "RX" :
2865 (FLOW_TX ? "TX" : "None"))));
2866
2867 netif_carrier_on(netdev);
2868 netif_wake_queue(netdev);
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -08002869#ifdef CONFIG_NETDEVICES_MULTIQUEUE
2870 for (i = 0; i < adapter->num_tx_queues; i++)
2871 netif_wake_subqueue(netdev, i);
2872#endif
Auke Kok9a799d72007-09-15 14:07:45 -07002873 } else {
2874 /* Force detection of hung controller */
2875 adapter->detect_tx_hung = true;
2876 }
2877 } else {
2878 if (netif_carrier_ok(netdev)) {
2879 DPRINTK(LINK, INFO, "NIC Link is Down\n");
2880 netif_carrier_off(netdev);
2881 netif_stop_queue(netdev);
2882 }
2883 }
2884
2885 ixgbe_update_stats(adapter);
2886
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002887 if (!test_bit(__IXGBE_DOWN, &adapter->state)) {
2888 /* Cause software interrupt to ensure rx rings are cleaned */
2889 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
2890 u32 eics =
2891 (1 << (adapter->num_msix_vectors - NON_Q_VECTORS)) - 1;
2892 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS, eics);
2893 } else {
2894 /* for legacy and MSI interrupts don't set any bits that
2895 * are enabled for EIAM, because this operation would
2896 * set *both* EIMS and EICS for any bit in EIAM */
2897 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS,
2898 (IXGBE_EICS_TCP_TIMER | IXGBE_EICS_OTHER));
2899 }
2900 /* Reset the timer */
Auke Kok9a799d72007-09-15 14:07:45 -07002901 mod_timer(&adapter->watchdog_timer,
2902 round_jiffies(jiffies + 2 * HZ));
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08002903 }
Auke Kok9a799d72007-09-15 14:07:45 -07002904}
2905
Auke Kok9a799d72007-09-15 14:07:45 -07002906static int ixgbe_tso(struct ixgbe_adapter *adapter,
2907 struct ixgbe_ring *tx_ring, struct sk_buff *skb,
2908 u32 tx_flags, u8 *hdr_len)
2909{
2910 struct ixgbe_adv_tx_context_desc *context_desc;
2911 unsigned int i;
2912 int err;
2913 struct ixgbe_tx_buffer *tx_buffer_info;
2914 u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
2915 u32 mss_l4len_idx = 0, l4len;
Auke Kok9a799d72007-09-15 14:07:45 -07002916
2917 if (skb_is_gso(skb)) {
2918 if (skb_header_cloned(skb)) {
2919 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2920 if (err)
2921 return err;
2922 }
2923 l4len = tcp_hdrlen(skb);
2924 *hdr_len += l4len;
2925
Al Viro8327d002007-12-10 18:54:12 +00002926 if (skb->protocol == htons(ETH_P_IP)) {
Auke Kok9a799d72007-09-15 14:07:45 -07002927 struct iphdr *iph = ip_hdr(skb);
2928 iph->tot_len = 0;
2929 iph->check = 0;
2930 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2931 iph->daddr, 0,
2932 IPPROTO_TCP,
2933 0);
2934 adapter->hw_tso_ctxt++;
2935 } else if (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6) {
2936 ipv6_hdr(skb)->payload_len = 0;
2937 tcp_hdr(skb)->check =
2938 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2939 &ipv6_hdr(skb)->daddr,
2940 0, IPPROTO_TCP, 0);
2941 adapter->hw_tso6_ctxt++;
2942 }
2943
2944 i = tx_ring->next_to_use;
2945
2946 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2947 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
2948
2949 /* VLAN MACLEN IPLEN */
2950 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2951 vlan_macip_lens |=
2952 (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
2953 vlan_macip_lens |= ((skb_network_offset(skb)) <<
2954 IXGBE_ADVTXD_MACLEN_SHIFT);
2955 *hdr_len += skb_network_offset(skb);
2956 vlan_macip_lens |=
2957 (skb_transport_header(skb) - skb_network_header(skb));
2958 *hdr_len +=
2959 (skb_transport_header(skb) - skb_network_header(skb));
2960 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2961 context_desc->seqnum_seed = 0;
2962
2963 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2964 type_tucmd_mlhl |= (IXGBE_TXD_CMD_DEXT |
2965 IXGBE_ADVTXD_DTYP_CTXT);
2966
Al Viro8327d002007-12-10 18:54:12 +00002967 if (skb->protocol == htons(ETH_P_IP))
Auke Kok9a799d72007-09-15 14:07:45 -07002968 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
2969 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2970 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
2971
2972 /* MSS L4LEN IDX */
2973 mss_l4len_idx |=
2974 (skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT);
2975 mss_l4len_idx |= (l4len << IXGBE_ADVTXD_L4LEN_SHIFT);
2976 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2977
2978 tx_buffer_info->time_stamp = jiffies;
2979 tx_buffer_info->next_to_watch = i;
2980
2981 i++;
2982 if (i == tx_ring->count)
2983 i = 0;
2984 tx_ring->next_to_use = i;
2985
2986 return true;
2987 }
2988 return false;
2989}
2990
2991static bool ixgbe_tx_csum(struct ixgbe_adapter *adapter,
2992 struct ixgbe_ring *tx_ring,
2993 struct sk_buff *skb, u32 tx_flags)
2994{
2995 struct ixgbe_adv_tx_context_desc *context_desc;
2996 unsigned int i;
2997 struct ixgbe_tx_buffer *tx_buffer_info;
2998 u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
2999
3000 if (skb->ip_summed == CHECKSUM_PARTIAL ||
3001 (tx_flags & IXGBE_TX_FLAGS_VLAN)) {
3002 i = tx_ring->next_to_use;
3003 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3004 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
3005
3006 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3007 vlan_macip_lens |=
3008 (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
3009 vlan_macip_lens |= (skb_network_offset(skb) <<
3010 IXGBE_ADVTXD_MACLEN_SHIFT);
3011 if (skb->ip_summed == CHECKSUM_PARTIAL)
3012 vlan_macip_lens |= (skb_transport_header(skb) -
3013 skb_network_header(skb));
3014
3015 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
3016 context_desc->seqnum_seed = 0;
3017
3018 type_tucmd_mlhl |= (IXGBE_TXD_CMD_DEXT |
3019 IXGBE_ADVTXD_DTYP_CTXT);
3020
3021 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Auke Kok41825d72008-02-12 15:20:33 -08003022 switch (skb->protocol) {
3023 case __constant_htons(ETH_P_IP):
Auke Kok9a799d72007-09-15 14:07:45 -07003024 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
Auke Kok41825d72008-02-12 15:20:33 -08003025 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
3026 type_tucmd_mlhl |=
3027 IXGBE_ADVTXD_TUCMD_L4T_TCP;
3028 break;
Auke Kok9a799d72007-09-15 14:07:45 -07003029
Auke Kok41825d72008-02-12 15:20:33 -08003030 case __constant_htons(ETH_P_IPV6):
3031 /* XXX what about other V6 headers?? */
3032 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
3033 type_tucmd_mlhl |=
3034 IXGBE_ADVTXD_TUCMD_L4T_TCP;
3035 break;
3036
3037 default:
3038 if (unlikely(net_ratelimit())) {
3039 DPRINTK(PROBE, WARNING,
3040 "partial checksum but proto=%x!\n",
3041 skb->protocol);
3042 }
3043 break;
3044 }
Auke Kok9a799d72007-09-15 14:07:45 -07003045 }
3046
3047 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
3048 context_desc->mss_l4len_idx = 0;
3049
3050 tx_buffer_info->time_stamp = jiffies;
3051 tx_buffer_info->next_to_watch = i;
3052 adapter->hw_csum_tx_good++;
3053 i++;
3054 if (i == tx_ring->count)
3055 i = 0;
3056 tx_ring->next_to_use = i;
3057
3058 return true;
3059 }
3060 return false;
3061}
3062
3063static int ixgbe_tx_map(struct ixgbe_adapter *adapter,
3064 struct ixgbe_ring *tx_ring,
3065 struct sk_buff *skb, unsigned int first)
3066{
3067 struct ixgbe_tx_buffer *tx_buffer_info;
3068 unsigned int len = skb->len;
3069 unsigned int offset = 0, size, count = 0, i;
3070 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
3071 unsigned int f;
3072
3073 len -= skb->data_len;
3074
3075 i = tx_ring->next_to_use;
3076
3077 while (len) {
3078 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3079 size = min(len, (uint)IXGBE_MAX_DATA_PER_TXD);
3080
3081 tx_buffer_info->length = size;
3082 tx_buffer_info->dma = pci_map_single(adapter->pdev,
3083 skb->data + offset,
3084 size, PCI_DMA_TODEVICE);
3085 tx_buffer_info->time_stamp = jiffies;
3086 tx_buffer_info->next_to_watch = i;
3087
3088 len -= size;
3089 offset += size;
3090 count++;
3091 i++;
3092 if (i == tx_ring->count)
3093 i = 0;
3094 }
3095
3096 for (f = 0; f < nr_frags; f++) {
3097 struct skb_frag_struct *frag;
3098
3099 frag = &skb_shinfo(skb)->frags[f];
3100 len = frag->size;
3101 offset = frag->page_offset;
3102
3103 while (len) {
3104 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3105 size = min(len, (uint)IXGBE_MAX_DATA_PER_TXD);
3106
3107 tx_buffer_info->length = size;
3108 tx_buffer_info->dma = pci_map_page(adapter->pdev,
3109 frag->page,
3110 offset,
3111 size, PCI_DMA_TODEVICE);
3112 tx_buffer_info->time_stamp = jiffies;
3113 tx_buffer_info->next_to_watch = i;
3114
3115 len -= size;
3116 offset += size;
3117 count++;
3118 i++;
3119 if (i == tx_ring->count)
3120 i = 0;
3121 }
3122 }
3123 if (i == 0)
3124 i = tx_ring->count - 1;
3125 else
3126 i = i - 1;
3127 tx_ring->tx_buffer_info[i].skb = skb;
3128 tx_ring->tx_buffer_info[first].next_to_watch = i;
3129
3130 return count;
3131}
3132
3133static void ixgbe_tx_queue(struct ixgbe_adapter *adapter,
3134 struct ixgbe_ring *tx_ring,
3135 int tx_flags, int count, u32 paylen, u8 hdr_len)
3136{
3137 union ixgbe_adv_tx_desc *tx_desc = NULL;
3138 struct ixgbe_tx_buffer *tx_buffer_info;
3139 u32 olinfo_status = 0, cmd_type_len = 0;
3140 unsigned int i;
3141 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
3142
3143 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
3144
3145 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
3146
3147 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3148 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
3149
3150 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
3151 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
3152
3153 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
3154 IXGBE_ADVTXD_POPTS_SHIFT;
3155
3156 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
3157 olinfo_status |= IXGBE_TXD_POPTS_IXSM <<
3158 IXGBE_ADVTXD_POPTS_SHIFT;
3159
3160 } else if (tx_flags & IXGBE_TX_FLAGS_CSUM)
3161 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
3162 IXGBE_ADVTXD_POPTS_SHIFT;
3163
3164 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
3165
3166 i = tx_ring->next_to_use;
3167 while (count--) {
3168 tx_buffer_info = &tx_ring->tx_buffer_info[i];
3169 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
3170 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
3171 tx_desc->read.cmd_type_len =
3172 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
3173 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
3174
3175 i++;
3176 if (i == tx_ring->count)
3177 i = 0;
3178 }
3179
3180 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
3181
3182 /*
3183 * Force memory writes to complete before letting h/w
3184 * know there are new descriptors to fetch. (Only
3185 * applicable for weak-ordered memory model archs,
3186 * such as IA-64).
3187 */
3188 wmb();
3189
3190 tx_ring->next_to_use = i;
3191 writel(i, adapter->hw.hw_addr + tx_ring->tail);
3192}
3193
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -08003194static int __ixgbe_maybe_stop_tx(struct net_device *netdev,
3195 struct ixgbe_ring *tx_ring, int size)
3196{
3197 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3198
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -08003199#ifdef CONFIG_NETDEVICES_MULTIQUEUE
3200 netif_stop_subqueue(netdev, tx_ring->queue_index);
3201#else
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -08003202 netif_stop_queue(netdev);
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -08003203#endif
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -08003204 /* Herbert's original patch had:
3205 * smp_mb__after_netif_stop_queue();
3206 * but since that doesn't exist yet, just open code it. */
3207 smp_mb();
3208
3209 /* We need to check again in a case another CPU has just
3210 * made room available. */
3211 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
3212 return -EBUSY;
3213
3214 /* A reprieve! - use start_queue because it doesn't call schedule */
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -08003215#ifdef CONFIG_NETDEVICES_MULTIQUEUE
3216 netif_wake_subqueue(netdev, tx_ring->queue_index);
3217#else
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -08003218 netif_wake_queue(netdev);
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -08003219#endif
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -08003220 ++adapter->restart_queue;
3221 return 0;
3222}
3223
3224static int ixgbe_maybe_stop_tx(struct net_device *netdev,
3225 struct ixgbe_ring *tx_ring, int size)
3226{
3227 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
3228 return 0;
3229 return __ixgbe_maybe_stop_tx(netdev, tx_ring, size);
3230}
3231
3232
Auke Kok9a799d72007-09-15 14:07:45 -07003233static int ixgbe_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3234{
3235 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3236 struct ixgbe_ring *tx_ring;
3237 unsigned int len = skb->len;
3238 unsigned int first;
3239 unsigned int tx_flags = 0;
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -08003240 u8 hdr_len = 0;
3241 int r_idx = 0, tso;
Auke Kok9a799d72007-09-15 14:07:45 -07003242 unsigned int mss = 0;
3243 int count = 0;
3244 unsigned int f;
3245 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
3246 len -= skb->data_len;
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -08003247#ifdef CONFIG_NETDEVICES_MULTIQUEUE
3248 r_idx = (adapter->num_tx_queues - 1) & skb->queue_mapping;
3249#endif
3250 tx_ring = &adapter->tx_ring[r_idx];
Auke Kok9a799d72007-09-15 14:07:45 -07003251
Auke Kok9a799d72007-09-15 14:07:45 -07003252
3253 if (skb->len <= 0) {
3254 dev_kfree_skb(skb);
3255 return NETDEV_TX_OK;
3256 }
3257 mss = skb_shinfo(skb)->gso_size;
3258
3259 if (mss)
3260 count++;
3261 else if (skb->ip_summed == CHECKSUM_PARTIAL)
3262 count++;
3263
3264 count += TXD_USE_COUNT(len);
3265 for (f = 0; f < nr_frags; f++)
3266 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
3267
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -08003268 if (ixgbe_maybe_stop_tx(netdev, tx_ring, count)) {
Auke Kok9a799d72007-09-15 14:07:45 -07003269 adapter->tx_busy++;
Auke Kok9a799d72007-09-15 14:07:45 -07003270 return NETDEV_TX_BUSY;
3271 }
Auke Kok9a799d72007-09-15 14:07:45 -07003272 if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
3273 tx_flags |= IXGBE_TX_FLAGS_VLAN;
3274 tx_flags |= (vlan_tx_tag_get(skb) << IXGBE_TX_FLAGS_VLAN_SHIFT);
3275 }
3276
Al Viro8327d002007-12-10 18:54:12 +00003277 if (skb->protocol == htons(ETH_P_IP))
Auke Kok9a799d72007-09-15 14:07:45 -07003278 tx_flags |= IXGBE_TX_FLAGS_IPV4;
3279 first = tx_ring->next_to_use;
3280 tso = ixgbe_tso(adapter, tx_ring, skb, tx_flags, &hdr_len);
3281 if (tso < 0) {
3282 dev_kfree_skb_any(skb);
3283 return NETDEV_TX_OK;
3284 }
3285
3286 if (tso)
3287 tx_flags |= IXGBE_TX_FLAGS_TSO;
3288 else if (ixgbe_tx_csum(adapter, tx_ring, skb, tx_flags) &&
3289 (skb->ip_summed == CHECKSUM_PARTIAL))
3290 tx_flags |= IXGBE_TX_FLAGS_CSUM;
3291
3292 ixgbe_tx_queue(adapter, tx_ring, tx_flags,
3293 ixgbe_tx_map(adapter, tx_ring, skb, first),
3294 skb->len, hdr_len);
3295
3296 netdev->trans_start = jiffies;
3297
Ayyappan Veeraiyane092be62008-02-01 15:58:49 -08003298 ixgbe_maybe_stop_tx(netdev, tx_ring, DESC_NEEDED);
Auke Kok9a799d72007-09-15 14:07:45 -07003299
3300 return NETDEV_TX_OK;
3301}
3302
3303/**
3304 * ixgbe_get_stats - Get System Network Statistics
3305 * @netdev: network interface device structure
3306 *
3307 * Returns the address of the device statistics structure.
3308 * The statistics are actually updated from the timer callback.
3309 **/
3310static struct net_device_stats *ixgbe_get_stats(struct net_device *netdev)
3311{
3312 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3313
3314 /* only return the current stats */
3315 return &adapter->net_stats;
3316}
3317
3318/**
3319 * ixgbe_set_mac - Change the Ethernet Address of the NIC
3320 * @netdev: network interface device structure
3321 * @p: pointer to an address structure
3322 *
3323 * Returns 0 on success, negative on failure
3324 **/
3325static int ixgbe_set_mac(struct net_device *netdev, void *p)
3326{
3327 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3328 struct sockaddr *addr = p;
3329
3330 if (!is_valid_ether_addr(addr->sa_data))
3331 return -EADDRNOTAVAIL;
3332
3333 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3334 memcpy(adapter->hw.mac.addr, addr->sa_data, netdev->addr_len);
3335
3336 ixgbe_set_rar(&adapter->hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
3337
3338 return 0;
3339}
3340
3341#ifdef CONFIG_NET_POLL_CONTROLLER
3342/*
3343 * Polling 'interrupt' - used by things like netconsole to send skbs
3344 * without having to re-enable interrupts. It's not called while
3345 * the interrupt routine is executing.
3346 */
3347static void ixgbe_netpoll(struct net_device *netdev)
3348{
3349 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3350
3351 disable_irq(adapter->pdev->irq);
3352 adapter->flags |= IXGBE_FLAG_IN_NETPOLL;
3353 ixgbe_intr(adapter->pdev->irq, netdev);
3354 adapter->flags &= ~IXGBE_FLAG_IN_NETPOLL;
3355 enable_irq(adapter->pdev->irq);
3356}
3357#endif
3358
3359/**
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08003360 * ixgbe_napi_add_all - prep napi structs for use
3361 * @adapter: private struct
3362 * helper function to napi_add each possible q_vector->napi
3363 */
3364static void ixgbe_napi_add_all(struct ixgbe_adapter *adapter)
3365{
3366 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
3367 int (*poll)(struct napi_struct *, int);
3368
3369 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
3370 poll = &ixgbe_clean_rxonly;
3371 } else {
3372 poll = &ixgbe_poll;
3373 /* only one q_vector for legacy modes */
3374 q_vectors = 1;
3375 }
3376
3377 for (i = 0; i < q_vectors; i++) {
3378 struct ixgbe_q_vector *q_vector = &adapter->q_vector[i];
3379 netif_napi_add(adapter->netdev, &q_vector->napi,
3380 (*poll), 64);
3381 }
3382}
3383
3384/**
Auke Kok9a799d72007-09-15 14:07:45 -07003385 * ixgbe_probe - Device Initialization Routine
3386 * @pdev: PCI device information struct
3387 * @ent: entry in ixgbe_pci_tbl
3388 *
3389 * Returns 0 on success, negative on failure
3390 *
3391 * ixgbe_probe initializes an adapter identified by a pci_dev structure.
3392 * The OS initialization, configuring of the adapter private structure,
3393 * and a hardware reset occur.
3394 **/
3395static int __devinit ixgbe_probe(struct pci_dev *pdev,
3396 const struct pci_device_id *ent)
3397{
3398 struct net_device *netdev;
3399 struct ixgbe_adapter *adapter = NULL;
3400 struct ixgbe_hw *hw;
3401 const struct ixgbe_info *ii = ixgbe_info_tbl[ent->driver_data];
3402 unsigned long mmio_start, mmio_len;
3403 static int cards_found;
3404 int i, err, pci_using_dac;
3405 u16 link_status, link_speed, link_width;
3406 u32 part_num;
3407
3408 err = pci_enable_device(pdev);
3409 if (err)
3410 return err;
3411
3412 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK) &&
3413 !pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK)) {
3414 pci_using_dac = 1;
3415 } else {
3416 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
3417 if (err) {
3418 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
3419 if (err) {
3420 dev_err(&pdev->dev, "No usable DMA "
3421 "configuration, aborting\n");
3422 goto err_dma;
3423 }
3424 }
3425 pci_using_dac = 0;
3426 }
3427
3428 err = pci_request_regions(pdev, ixgbe_driver_name);
3429 if (err) {
3430 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3431 goto err_pci_reg;
3432 }
3433
3434 pci_set_master(pdev);
Wendy Xiongfb3b27b2008-04-23 11:09:24 -07003435 pci_save_state(pdev);
Auke Kok9a799d72007-09-15 14:07:45 -07003436
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -08003437#ifdef CONFIG_NETDEVICES_MULTIQUEUE
3438 netdev = alloc_etherdev_mq(sizeof(struct ixgbe_adapter), MAX_TX_QUEUES);
3439#else
Auke Kok9a799d72007-09-15 14:07:45 -07003440 netdev = alloc_etherdev(sizeof(struct ixgbe_adapter));
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -08003441#endif
Auke Kok9a799d72007-09-15 14:07:45 -07003442 if (!netdev) {
3443 err = -ENOMEM;
3444 goto err_alloc_etherdev;
3445 }
3446
Auke Kok9a799d72007-09-15 14:07:45 -07003447 SET_NETDEV_DEV(netdev, &pdev->dev);
3448
3449 pci_set_drvdata(pdev, netdev);
3450 adapter = netdev_priv(netdev);
3451
3452 adapter->netdev = netdev;
3453 adapter->pdev = pdev;
3454 hw = &adapter->hw;
3455 hw->back = adapter;
3456 adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
3457
3458 mmio_start = pci_resource_start(pdev, 0);
3459 mmio_len = pci_resource_len(pdev, 0);
3460
3461 hw->hw_addr = ioremap(mmio_start, mmio_len);
3462 if (!hw->hw_addr) {
3463 err = -EIO;
3464 goto err_ioremap;
3465 }
3466
3467 for (i = 1; i <= 5; i++) {
3468 if (pci_resource_len(pdev, i) == 0)
3469 continue;
3470 }
3471
3472 netdev->open = &ixgbe_open;
3473 netdev->stop = &ixgbe_close;
3474 netdev->hard_start_xmit = &ixgbe_xmit_frame;
3475 netdev->get_stats = &ixgbe_get_stats;
3476 netdev->set_multicast_list = &ixgbe_set_multi;
3477 netdev->set_mac_address = &ixgbe_set_mac;
3478 netdev->change_mtu = &ixgbe_change_mtu;
3479 ixgbe_set_ethtool_ops(netdev);
3480 netdev->tx_timeout = &ixgbe_tx_timeout;
3481 netdev->watchdog_timeo = 5 * HZ;
Auke Kok9a799d72007-09-15 14:07:45 -07003482 netdev->vlan_rx_register = ixgbe_vlan_rx_register;
3483 netdev->vlan_rx_add_vid = ixgbe_vlan_rx_add_vid;
3484 netdev->vlan_rx_kill_vid = ixgbe_vlan_rx_kill_vid;
3485#ifdef CONFIG_NET_POLL_CONTROLLER
3486 netdev->poll_controller = ixgbe_netpoll;
3487#endif
3488 strcpy(netdev->name, pci_name(pdev));
3489
3490 netdev->mem_start = mmio_start;
3491 netdev->mem_end = mmio_start + mmio_len;
3492
3493 adapter->bd_number = cards_found;
3494
3495 /* PCI config space info */
3496 hw->vendor_id = pdev->vendor;
3497 hw->device_id = pdev->device;
3498 hw->revision_id = pdev->revision;
3499 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3500 hw->subsystem_device_id = pdev->subsystem_device;
3501
3502 /* Setup hw api */
3503 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08003504 hw->mac.type = ii->mac;
Auke Kok9a799d72007-09-15 14:07:45 -07003505
3506 err = ii->get_invariants(hw);
3507 if (err)
3508 goto err_hw_init;
3509
3510 /* setup the private structure */
3511 err = ixgbe_sw_init(adapter);
3512 if (err)
3513 goto err_sw_init;
3514
3515 netdev->features = NETIF_F_SG |
3516 NETIF_F_HW_CSUM |
3517 NETIF_F_HW_VLAN_TX |
3518 NETIF_F_HW_VLAN_RX |
3519 NETIF_F_HW_VLAN_FILTER;
3520
3521 netdev->features |= NETIF_F_TSO;
3522
3523 netdev->features |= NETIF_F_TSO6;
3524 if (pci_using_dac)
3525 netdev->features |= NETIF_F_HIGHDMA;
3526
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -08003527#ifdef CONFIG_NETDEVICES_MULTIQUEUE
3528 netdev->features |= NETIF_F_MULTI_QUEUE;
3529#endif
Auke Kok9a799d72007-09-15 14:07:45 -07003530
3531 /* make sure the EEPROM is good */
3532 if (ixgbe_validate_eeprom_checksum(hw, NULL) < 0) {
3533 dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n");
3534 err = -EIO;
3535 goto err_eeprom;
3536 }
3537
3538 memcpy(netdev->dev_addr, hw->mac.perm_addr, netdev->addr_len);
3539 memcpy(netdev->perm_addr, hw->mac.perm_addr, netdev->addr_len);
3540
3541 if (ixgbe_validate_mac_addr(netdev->dev_addr)) {
3542 err = -EIO;
3543 goto err_eeprom;
3544 }
3545
3546 init_timer(&adapter->watchdog_timer);
3547 adapter->watchdog_timer.function = &ixgbe_watchdog;
3548 adapter->watchdog_timer.data = (unsigned long)adapter;
3549
3550 INIT_WORK(&adapter->reset_task, ixgbe_reset_task);
3551
3552 /* initialize default flow control settings */
3553 hw->fc.original_type = ixgbe_fc_full;
3554 hw->fc.type = ixgbe_fc_full;
3555 hw->fc.high_water = IXGBE_DEFAULT_FCRTH;
3556 hw->fc.low_water = IXGBE_DEFAULT_FCRTL;
3557 hw->fc.pause_time = IXGBE_DEFAULT_FCPAUSE;
3558
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08003559 err = ixgbe_init_interrupt_scheme(adapter);
3560 if (err)
3561 goto err_sw_init;
Auke Kok9a799d72007-09-15 14:07:45 -07003562
3563 /* print bus type/speed/width info */
3564 pci_read_config_word(pdev, IXGBE_PCI_LINK_STATUS, &link_status);
3565 link_speed = link_status & IXGBE_PCI_LINK_SPEED;
3566 link_width = link_status & IXGBE_PCI_LINK_WIDTH;
3567 dev_info(&pdev->dev, "(PCI Express:%s:%s) "
3568 "%02x:%02x:%02x:%02x:%02x:%02x\n",
3569 ((link_speed == IXGBE_PCI_LINK_SPEED_5000) ? "5.0Gb/s" :
3570 (link_speed == IXGBE_PCI_LINK_SPEED_2500) ? "2.5Gb/s" :
3571 "Unknown"),
3572 ((link_width == IXGBE_PCI_LINK_WIDTH_8) ? "Width x8" :
3573 (link_width == IXGBE_PCI_LINK_WIDTH_4) ? "Width x4" :
3574 (link_width == IXGBE_PCI_LINK_WIDTH_2) ? "Width x2" :
3575 (link_width == IXGBE_PCI_LINK_WIDTH_1) ? "Width x1" :
3576 "Unknown"),
3577 netdev->dev_addr[0], netdev->dev_addr[1], netdev->dev_addr[2],
3578 netdev->dev_addr[3], netdev->dev_addr[4], netdev->dev_addr[5]);
3579 ixgbe_read_part_num(hw, &part_num);
3580 dev_info(&pdev->dev, "MAC: %d, PHY: %d, PBA No: %06x-%03x\n",
3581 hw->mac.type, hw->phy.type,
3582 (part_num >> 8), (part_num & 0xff));
3583
Auke Kok0c254d82008-02-11 09:25:56 -08003584 if (link_width <= IXGBE_PCI_LINK_WIDTH_4) {
3585 dev_warn(&pdev->dev, "PCI-Express bandwidth available for "
3586 "this card is not sufficient for optimal "
3587 "performance.\n");
3588 dev_warn(&pdev->dev, "For optimal performance a x8 "
3589 "PCI-Express slot is required.\n");
3590 }
3591
Auke Kok9a799d72007-09-15 14:07:45 -07003592 /* reset the hardware with the new settings */
3593 ixgbe_start_hw(hw);
3594
3595 netif_carrier_off(netdev);
3596 netif_stop_queue(netdev);
Ayyappan Veeraiyan30eba972008-03-03 15:03:52 -08003597#ifdef CONFIG_NETDEVICES_MULTIQUEUE
3598 for (i = 0; i < adapter->num_tx_queues; i++)
3599 netif_stop_subqueue(netdev, i);
3600#endif
Auke Kok9a799d72007-09-15 14:07:45 -07003601
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08003602 ixgbe_napi_add_all(adapter);
3603
Auke Kok9a799d72007-09-15 14:07:45 -07003604 strcpy(netdev->name, "eth%d");
3605 err = register_netdev(netdev);
3606 if (err)
3607 goto err_register;
3608
Jeb Cramerbd0362d2008-03-03 15:04:02 -08003609#ifdef CONFIG_DCA
Denis V. Lunev652f0932008-03-27 14:39:17 +03003610 if (dca_add_requester(&pdev->dev) == 0) {
Jeb Cramerbd0362d2008-03-03 15:04:02 -08003611 adapter->flags |= IXGBE_FLAG_DCA_ENABLED;
3612 /* always use CB2 mode, difference is masked
3613 * in the CB driver */
3614 IXGBE_WRITE_REG(hw, IXGBE_DCA_CTRL, 2);
3615 ixgbe_setup_dca(adapter);
3616 }
3617#endif
Auke Kok9a799d72007-09-15 14:07:45 -07003618
3619 dev_info(&pdev->dev, "Intel(R) 10 Gigabit Network Connection\n");
3620 cards_found++;
3621 return 0;
3622
3623err_register:
Ayyappan Veeraiyan5eba3692008-02-01 15:59:04 -08003624 ixgbe_release_hw_control(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07003625err_hw_init:
3626err_sw_init:
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08003627 ixgbe_reset_interrupt_capability(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07003628err_eeprom:
3629 iounmap(hw->hw_addr);
3630err_ioremap:
3631 free_netdev(netdev);
3632err_alloc_etherdev:
3633 pci_release_regions(pdev);
3634err_pci_reg:
3635err_dma:
3636 pci_disable_device(pdev);
3637 return err;
3638}
3639
3640/**
3641 * ixgbe_remove - Device Removal Routine
3642 * @pdev: PCI device information struct
3643 *
3644 * ixgbe_remove is called by the PCI subsystem to alert the driver
3645 * that it should release a PCI device. The could be caused by a
3646 * Hot-Plug event, or because the driver is going to be removed from
3647 * memory.
3648 **/
3649static void __devexit ixgbe_remove(struct pci_dev *pdev)
3650{
3651 struct net_device *netdev = pci_get_drvdata(pdev);
3652 struct ixgbe_adapter *adapter = netdev_priv(netdev);
3653
3654 set_bit(__IXGBE_DOWN, &adapter->state);
3655 del_timer_sync(&adapter->watchdog_timer);
3656
3657 flush_scheduled_work();
3658
Jeb Cramerbd0362d2008-03-03 15:04:02 -08003659#ifdef CONFIG_DCA
3660 if (adapter->flags & IXGBE_FLAG_DCA_ENABLED) {
3661 adapter->flags &= ~IXGBE_FLAG_DCA_ENABLED;
3662 dca_remove_requester(&pdev->dev);
3663 IXGBE_WRITE_REG(&adapter->hw, IXGBE_DCA_CTRL, 1);
3664 }
3665
3666#endif
Auke Kok9a799d72007-09-15 14:07:45 -07003667 unregister_netdev(netdev);
3668
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08003669 ixgbe_reset_interrupt_capability(adapter);
Ayyappan Veeraiyan5eba3692008-02-01 15:59:04 -08003670
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08003671 ixgbe_release_hw_control(adapter);
Auke Kok9a799d72007-09-15 14:07:45 -07003672
3673 iounmap(adapter->hw.hw_addr);
3674 pci_release_regions(pdev);
3675
Ayyappan Veeraiyan021230d2008-03-03 15:03:45 -08003676 DPRINTK(PROBE, INFO, "complete\n");
3677 kfree(adapter->tx_ring);
3678 kfree(adapter->rx_ring);
3679
Auke Kok9a799d72007-09-15 14:07:45 -07003680 free_netdev(netdev);
3681
3682 pci_disable_device(pdev);
3683}
3684
3685/**
3686 * ixgbe_io_error_detected - called when PCI error is detected
3687 * @pdev: Pointer to PCI device
3688 * @state: The current pci connection state
3689 *
3690 * This function is called after a PCI bus error affecting
3691 * this device has been detected.
3692 */
3693static pci_ers_result_t ixgbe_io_error_detected(struct pci_dev *pdev,
3694 pci_channel_state_t state)
3695{
3696 struct net_device *netdev = pci_get_drvdata(pdev);
3697 struct ixgbe_adapter *adapter = netdev->priv;
3698
3699 netif_device_detach(netdev);
3700
3701 if (netif_running(netdev))
3702 ixgbe_down(adapter);
3703 pci_disable_device(pdev);
3704
3705 /* Request a slot slot reset. */
3706 return PCI_ERS_RESULT_NEED_RESET;
3707}
3708
3709/**
3710 * ixgbe_io_slot_reset - called after the pci bus has been reset.
3711 * @pdev: Pointer to PCI device
3712 *
3713 * Restart the card from scratch, as if from a cold-boot.
3714 */
3715static pci_ers_result_t ixgbe_io_slot_reset(struct pci_dev *pdev)
3716{
3717 struct net_device *netdev = pci_get_drvdata(pdev);
3718 struct ixgbe_adapter *adapter = netdev->priv;
3719
3720 if (pci_enable_device(pdev)) {
3721 DPRINTK(PROBE, ERR,
3722 "Cannot re-enable PCI device after reset.\n");
3723 return PCI_ERS_RESULT_DISCONNECT;
3724 }
3725 pci_set_master(pdev);
Wendy Xiongfb3b27b2008-04-23 11:09:24 -07003726 pci_restore_state(pdev);
Auke Kok9a799d72007-09-15 14:07:45 -07003727
3728 pci_enable_wake(pdev, PCI_D3hot, 0);
3729 pci_enable_wake(pdev, PCI_D3cold, 0);
3730
3731 ixgbe_reset(adapter);
3732
3733 return PCI_ERS_RESULT_RECOVERED;
3734}
3735
3736/**
3737 * ixgbe_io_resume - called when traffic can start flowing again.
3738 * @pdev: Pointer to PCI device
3739 *
3740 * This callback is called when the error recovery driver tells us that
3741 * its OK to resume normal operation.
3742 */
3743static void ixgbe_io_resume(struct pci_dev *pdev)
3744{
3745 struct net_device *netdev = pci_get_drvdata(pdev);
3746 struct ixgbe_adapter *adapter = netdev->priv;
3747
3748 if (netif_running(netdev)) {
3749 if (ixgbe_up(adapter)) {
3750 DPRINTK(PROBE, INFO, "ixgbe_up failed after reset\n");
3751 return;
3752 }
3753 }
3754
3755 netif_device_attach(netdev);
3756
3757}
3758
3759static struct pci_error_handlers ixgbe_err_handler = {
3760 .error_detected = ixgbe_io_error_detected,
3761 .slot_reset = ixgbe_io_slot_reset,
3762 .resume = ixgbe_io_resume,
3763};
3764
3765static struct pci_driver ixgbe_driver = {
3766 .name = ixgbe_driver_name,
3767 .id_table = ixgbe_pci_tbl,
3768 .probe = ixgbe_probe,
3769 .remove = __devexit_p(ixgbe_remove),
3770#ifdef CONFIG_PM
3771 .suspend = ixgbe_suspend,
3772 .resume = ixgbe_resume,
3773#endif
3774 .shutdown = ixgbe_shutdown,
3775 .err_handler = &ixgbe_err_handler
3776};
3777
3778/**
3779 * ixgbe_init_module - Driver Registration Routine
3780 *
3781 * ixgbe_init_module is the first routine called when the driver is
3782 * loaded. All it does is register with the PCI subsystem.
3783 **/
3784static int __init ixgbe_init_module(void)
3785{
3786 int ret;
3787 printk(KERN_INFO "%s: %s - version %s\n", ixgbe_driver_name,
3788 ixgbe_driver_string, ixgbe_driver_version);
3789
3790 printk(KERN_INFO "%s: %s\n", ixgbe_driver_name, ixgbe_copyright);
3791
Jeb Cramerbd0362d2008-03-03 15:04:02 -08003792#ifdef CONFIG_DCA
3793 dca_register_notify(&dca_notifier);
3794
3795#endif
Auke Kok9a799d72007-09-15 14:07:45 -07003796 ret = pci_register_driver(&ixgbe_driver);
3797 return ret;
3798}
3799module_init(ixgbe_init_module);
3800
3801/**
3802 * ixgbe_exit_module - Driver Exit Cleanup Routine
3803 *
3804 * ixgbe_exit_module is called just before the driver is removed
3805 * from memory.
3806 **/
3807static void __exit ixgbe_exit_module(void)
3808{
Jeb Cramerbd0362d2008-03-03 15:04:02 -08003809#ifdef CONFIG_DCA
3810 dca_unregister_notify(&dca_notifier);
3811#endif
Auke Kok9a799d72007-09-15 14:07:45 -07003812 pci_unregister_driver(&ixgbe_driver);
3813}
Jeb Cramerbd0362d2008-03-03 15:04:02 -08003814
3815#ifdef CONFIG_DCA
3816static int ixgbe_notify_dca(struct notifier_block *nb, unsigned long event,
3817 void *p)
3818{
3819 int ret_val;
3820
3821 ret_val = driver_for_each_device(&ixgbe_driver.driver, NULL, &event,
3822 __ixgbe_notify_dca);
3823
3824 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
3825}
3826#endif /* CONFIG_DCA */
3827
Auke Kok9a799d72007-09-15 14:07:45 -07003828module_exit(ixgbe_exit_module);
3829
3830/* ixgbe_main.c */