Pull cpuidle into release branch
[linux-2.6] / drivers / net / ixgbe / ixgbe_main.c
1 /*******************************************************************************
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
47 char ixgbe_driver_name[] = "ixgbe";
48 static const char ixgbe_driver_string[] =
49         "Intel(R) 10 Gigabit PCI Express Network Driver";
50
51 #define DRV_VERSION "1.1.18"
52 const char ixgbe_driver_version[] = DRV_VERSION;
53 static const char ixgbe_copyright[] =
54          "Copyright (c) 1999-2007 Intel Corporation.";
55
56 static const struct ixgbe_info *ixgbe_info_tbl[] = {
57         [board_82598AF]                 = &ixgbe_82598AF_info,
58         [board_82598EB]                 = &ixgbe_82598EB_info,
59         [board_82598AT]                 = &ixgbe_82598AT_info,
60 };
61
62 /* ixgbe_pci_tbl - PCI Device ID Table
63  *
64  * Wildcard entries (PCI_ANY_ID) should come last
65  * Last entry must be all 0s
66  *
67  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
68  *   Class, Class Mask, private data (not used) }
69  */
70 static struct pci_device_id ixgbe_pci_tbl[] = {
71         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_DUAL_PORT),
72          board_82598AF },
73         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AF_SINGLE_PORT),
74          board_82598AF },
75         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598AT_DUAL_PORT),
76          board_82598AT },
77         {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82598EB_CX4),
78          board_82598EB },
79
80         /* required last entry */
81         {0, }
82 };
83 MODULE_DEVICE_TABLE(pci, ixgbe_pci_tbl);
84
85 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
86 MODULE_DESCRIPTION("Intel(R) 10 Gigabit PCI Express Network Driver");
87 MODULE_LICENSE("GPL");
88 MODULE_VERSION(DRV_VERSION);
89
90 #define DEFAULT_DEBUG_LEVEL_SHIFT 3
91
92
93 #ifdef DEBUG
94 /**
95  * ixgbe_get_hw_dev_name - return device name string
96  * used by hardware layer to print debugging information
97  **/
98 char *ixgbe_get_hw_dev_name(struct ixgbe_hw *hw)
99 {
100         struct ixgbe_adapter *adapter = hw->back;
101         struct net_device *netdev = adapter->netdev;
102         return netdev->name;
103 }
104 #endif
105
106 static void ixgbe_set_ivar(struct ixgbe_adapter *adapter, u16 int_alloc_entry,
107                            u8 msix_vector)
108 {
109         u32 ivar, index;
110
111         msix_vector |= IXGBE_IVAR_ALLOC_VAL;
112         index = (int_alloc_entry >> 2) & 0x1F;
113         ivar = IXGBE_READ_REG(&adapter->hw, IXGBE_IVAR(index));
114         ivar &= ~(0xFF << (8 * (int_alloc_entry & 0x3)));
115         ivar |= (msix_vector << (8 * (int_alloc_entry & 0x3)));
116         IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR(index), ivar);
117 }
118
119 static void ixgbe_unmap_and_free_tx_resource(struct ixgbe_adapter *adapter,
120                                              struct ixgbe_tx_buffer
121                                              *tx_buffer_info)
122 {
123         if (tx_buffer_info->dma) {
124                 pci_unmap_page(adapter->pdev,
125                                tx_buffer_info->dma,
126                                tx_buffer_info->length, PCI_DMA_TODEVICE);
127                 tx_buffer_info->dma = 0;
128         }
129         if (tx_buffer_info->skb) {
130                 dev_kfree_skb_any(tx_buffer_info->skb);
131                 tx_buffer_info->skb = NULL;
132         }
133         /* tx_buffer_info must be completely set up in the transmit path */
134 }
135
136 static inline bool ixgbe_check_tx_hang(struct ixgbe_adapter *adapter,
137                                        struct ixgbe_ring *tx_ring,
138                                        unsigned int eop,
139                                        union ixgbe_adv_tx_desc *eop_desc)
140 {
141         /* Detect a transmit hang in hardware, this serializes the
142          * check with the clearing of time_stamp and movement of i */
143         adapter->detect_tx_hung = false;
144         if (tx_ring->tx_buffer_info[eop].dma &&
145             time_after(jiffies, tx_ring->tx_buffer_info[eop].time_stamp + HZ) &&
146             !(IXGBE_READ_REG(&adapter->hw, IXGBE_TFCS) & IXGBE_TFCS_TXOFF)) {
147                 /* detected Tx unit hang */
148                 DPRINTK(DRV, ERR, "Detected Tx Unit Hang\n"
149                         "  TDH                  <%x>\n"
150                         "  TDT                  <%x>\n"
151                         "  next_to_use          <%x>\n"
152                         "  next_to_clean        <%x>\n"
153                         "tx_buffer_info[next_to_clean]\n"
154                         "  time_stamp           <%lx>\n"
155                         "  next_to_watch        <%x>\n"
156                         "  jiffies              <%lx>\n"
157                         "  next_to_watch.status <%x>\n",
158                         readl(adapter->hw.hw_addr + tx_ring->head),
159                         readl(adapter->hw.hw_addr + tx_ring->tail),
160                         tx_ring->next_to_use,
161                         tx_ring->next_to_clean,
162                         tx_ring->tx_buffer_info[eop].time_stamp,
163                         eop, jiffies, eop_desc->wb.status);
164                 return true;
165         }
166
167         return false;
168 }
169
170 /**
171  * ixgbe_clean_tx_irq - Reclaim resources after transmit completes
172  * @adapter: board private structure
173  **/
174 static bool ixgbe_clean_tx_irq(struct ixgbe_adapter *adapter,
175                                     struct ixgbe_ring *tx_ring)
176 {
177         struct net_device *netdev = adapter->netdev;
178         union ixgbe_adv_tx_desc *tx_desc, *eop_desc;
179         struct ixgbe_tx_buffer *tx_buffer_info;
180         unsigned int i, eop;
181         bool cleaned = false;
182         int count = 0;
183
184         i = tx_ring->next_to_clean;
185         eop = tx_ring->tx_buffer_info[i].next_to_watch;
186         eop_desc = IXGBE_TX_DESC_ADV(*tx_ring, eop);
187         while (eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)) {
188                 for (cleaned = false; !cleaned;) {
189                         tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
190                         tx_buffer_info = &tx_ring->tx_buffer_info[i];
191                         cleaned = (i == eop);
192
193                         tx_ring->stats.bytes += tx_buffer_info->length;
194                         ixgbe_unmap_and_free_tx_resource(adapter,
195                                                          tx_buffer_info);
196                         tx_desc->wb.status = 0;
197
198                         i++;
199                         if (i == tx_ring->count)
200                                 i = 0;
201                 }
202
203                 tx_ring->stats.packets++;
204
205                 eop = tx_ring->tx_buffer_info[i].next_to_watch;
206                 eop_desc = IXGBE_TX_DESC_ADV(*tx_ring, eop);
207
208                 /* weight of a sort for tx, avoid endless transmit cleanup */
209                 if (count++ >= tx_ring->work_limit)
210                         break;
211         }
212
213         tx_ring->next_to_clean = i;
214
215 #define TX_WAKE_THRESHOLD 32
216         spin_lock(&tx_ring->tx_lock);
217
218         if (cleaned && netif_carrier_ok(netdev) &&
219             (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD) &&
220             !test_bit(__IXGBE_DOWN, &adapter->state))
221                 netif_wake_queue(netdev);
222
223         spin_unlock(&tx_ring->tx_lock);
224
225         if (adapter->detect_tx_hung)
226                 if (ixgbe_check_tx_hang(adapter, tx_ring, eop, eop_desc))
227                         netif_stop_queue(netdev);
228
229         if (count >= tx_ring->work_limit)
230                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS, tx_ring->eims_value);
231
232         return cleaned;
233 }
234
235 /**
236  * ixgbe_receive_skb - Send a completed packet up the stack
237  * @adapter: board private structure
238  * @skb: packet to send up
239  * @is_vlan: packet has a VLAN tag
240  * @tag: VLAN tag from descriptor
241  **/
242 static void ixgbe_receive_skb(struct ixgbe_adapter *adapter,
243                               struct sk_buff *skb, bool is_vlan,
244                               u16 tag)
245 {
246         if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL)) {
247                 if (adapter->vlgrp && is_vlan)
248                         vlan_hwaccel_receive_skb(skb, adapter->vlgrp, tag);
249                 else
250                         netif_receive_skb(skb);
251         } else {
252
253                 if (adapter->vlgrp && is_vlan)
254                         vlan_hwaccel_rx(skb, adapter->vlgrp, tag);
255                 else
256                         netif_rx(skb);
257         }
258 }
259
260 static inline void ixgbe_rx_checksum(struct ixgbe_adapter *adapter,
261                                          u32 status_err,
262                                          struct sk_buff *skb)
263 {
264         skb->ip_summed = CHECKSUM_NONE;
265
266         /* Ignore Checksum bit is set */
267         if ((status_err & IXGBE_RXD_STAT_IXSM) ||
268                      !(adapter->flags & IXGBE_FLAG_RX_CSUM_ENABLED))
269                 return;
270         /* TCP/UDP checksum error bit is set */
271         if (status_err & (IXGBE_RXDADV_ERR_TCPE | IXGBE_RXDADV_ERR_IPE)) {
272                 /* let the stack verify checksum errors */
273                 adapter->hw_csum_rx_error++;
274                 return;
275         }
276         /* It must be a TCP or UDP packet with a valid checksum */
277         if (status_err & (IXGBE_RXD_STAT_L4CS | IXGBE_RXD_STAT_UDPCS))
278                 skb->ip_summed = CHECKSUM_UNNECESSARY;
279         adapter->hw_csum_rx_good++;
280 }
281
282 /**
283  * ixgbe_alloc_rx_buffers - Replace used receive buffers; packet split
284  * @adapter: address of board private structure
285  **/
286 static void ixgbe_alloc_rx_buffers(struct ixgbe_adapter *adapter,
287                                        struct ixgbe_ring *rx_ring,
288                                        int cleaned_count)
289 {
290         struct net_device *netdev = adapter->netdev;
291         struct pci_dev *pdev = adapter->pdev;
292         union ixgbe_adv_rx_desc *rx_desc;
293         struct ixgbe_rx_buffer *rx_buffer_info;
294         struct sk_buff *skb;
295         unsigned int i;
296         unsigned int bufsz = adapter->rx_buf_len + NET_IP_ALIGN;
297
298         i = rx_ring->next_to_use;
299         rx_buffer_info = &rx_ring->rx_buffer_info[i];
300
301         while (cleaned_count--) {
302                 rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
303
304                 if (!rx_buffer_info->page &&
305                                 (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED)) {
306                         rx_buffer_info->page = alloc_page(GFP_ATOMIC);
307                         if (!rx_buffer_info->page) {
308                                 adapter->alloc_rx_page_failed++;
309                                 goto no_buffers;
310                         }
311                         rx_buffer_info->page_dma =
312                             pci_map_page(pdev, rx_buffer_info->page,
313                                          0, PAGE_SIZE, PCI_DMA_FROMDEVICE);
314                 }
315
316                 if (!rx_buffer_info->skb) {
317                         skb = netdev_alloc_skb(netdev, bufsz);
318
319                         if (!skb) {
320                                 adapter->alloc_rx_buff_failed++;
321                                 goto no_buffers;
322                         }
323
324                         /*
325                          * Make buffer alignment 2 beyond a 16 byte boundary
326                          * this will result in a 16 byte aligned IP header after
327                          * the 14 byte MAC header is removed
328                          */
329                         skb_reserve(skb, NET_IP_ALIGN);
330
331                         rx_buffer_info->skb = skb;
332                         rx_buffer_info->dma = pci_map_single(pdev, skb->data,
333                                                           bufsz,
334                                                           PCI_DMA_FROMDEVICE);
335                 }
336                 /* Refresh the desc even if buffer_addrs didn't change because
337                  * each write-back erases this info. */
338                 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
339                         rx_desc->read.pkt_addr =
340                             cpu_to_le64(rx_buffer_info->page_dma);
341                         rx_desc->read.hdr_addr =
342                                         cpu_to_le64(rx_buffer_info->dma);
343                 } else {
344                         rx_desc->read.pkt_addr =
345                                         cpu_to_le64(rx_buffer_info->dma);
346                 }
347
348                 i++;
349                 if (i == rx_ring->count)
350                         i = 0;
351                 rx_buffer_info = &rx_ring->rx_buffer_info[i];
352         }
353 no_buffers:
354         if (rx_ring->next_to_use != i) {
355                 rx_ring->next_to_use = i;
356                 if (i-- == 0)
357                         i = (rx_ring->count - 1);
358
359                 /*
360                  * Force memory writes to complete before letting h/w
361                  * know there are new descriptors to fetch.  (Only
362                  * applicable for weak-ordered memory model archs,
363                  * such as IA-64).
364                  */
365                 wmb();
366                 writel(i, adapter->hw.hw_addr + rx_ring->tail);
367         }
368 }
369
370 static bool ixgbe_clean_rx_irq(struct ixgbe_adapter *adapter,
371                                struct ixgbe_ring *rx_ring,
372                                int *work_done, int work_to_do)
373 {
374         struct net_device *netdev = adapter->netdev;
375         struct pci_dev *pdev = adapter->pdev;
376         union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
377         struct ixgbe_rx_buffer *rx_buffer_info, *next_buffer;
378         struct sk_buff *skb;
379         unsigned int i;
380         u32 upper_len, len, staterr;
381         u16 hdr_info, vlan_tag;
382         bool is_vlan, cleaned = false;
383         int cleaned_count = 0;
384
385         i = rx_ring->next_to_clean;
386         upper_len = 0;
387         rx_desc = IXGBE_RX_DESC_ADV(*rx_ring, i);
388         staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
389         rx_buffer_info = &rx_ring->rx_buffer_info[i];
390         is_vlan = (staterr & IXGBE_RXD_STAT_VP);
391         vlan_tag = le16_to_cpu(rx_desc->wb.upper.vlan);
392
393         while (staterr & IXGBE_RXD_STAT_DD) {
394                 if (*work_done >= work_to_do)
395                         break;
396                 (*work_done)++;
397
398                 if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
399                         hdr_info =
400                             le16_to_cpu(rx_desc->wb.lower.lo_dword.hdr_info);
401                         len =
402                             ((hdr_info & IXGBE_RXDADV_HDRBUFLEN_MASK) >>
403                              IXGBE_RXDADV_HDRBUFLEN_SHIFT);
404                         if (hdr_info & IXGBE_RXDADV_SPH)
405                                 adapter->rx_hdr_split++;
406                         if (len > IXGBE_RX_HDR_SIZE)
407                                 len = IXGBE_RX_HDR_SIZE;
408                         upper_len = le16_to_cpu(rx_desc->wb.upper.length);
409                 } else
410                         len = le16_to_cpu(rx_desc->wb.upper.length);
411
412                 cleaned = true;
413                 skb = rx_buffer_info->skb;
414                 prefetch(skb->data - NET_IP_ALIGN);
415                 rx_buffer_info->skb = NULL;
416
417                 if (len && !skb_shinfo(skb)->nr_frags) {
418                         pci_unmap_single(pdev, rx_buffer_info->dma,
419                                          adapter->rx_buf_len + NET_IP_ALIGN,
420                                          PCI_DMA_FROMDEVICE);
421                         skb_put(skb, len);
422                 }
423
424                 if (upper_len) {
425                         pci_unmap_page(pdev, rx_buffer_info->page_dma,
426                                        PAGE_SIZE, PCI_DMA_FROMDEVICE);
427                         rx_buffer_info->page_dma = 0;
428                         skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
429                                            rx_buffer_info->page, 0, upper_len);
430                         rx_buffer_info->page = NULL;
431
432                         skb->len += upper_len;
433                         skb->data_len += upper_len;
434                         skb->truesize += upper_len;
435                 }
436
437                 i++;
438                 if (i == rx_ring->count)
439                         i = 0;
440                 next_buffer = &rx_ring->rx_buffer_info[i];
441
442                 next_rxd = IXGBE_RX_DESC_ADV(*rx_ring, i);
443                 prefetch(next_rxd);
444
445                 cleaned_count++;
446                 if (staterr & IXGBE_RXD_STAT_EOP) {
447                         rx_ring->stats.packets++;
448                         rx_ring->stats.bytes += skb->len;
449                 } else {
450                         rx_buffer_info->skb = next_buffer->skb;
451                         rx_buffer_info->dma = next_buffer->dma;
452                         next_buffer->skb = skb;
453                         adapter->non_eop_descs++;
454                         goto next_desc;
455                 }
456
457                 if (staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK) {
458                         dev_kfree_skb_irq(skb);
459                         goto next_desc;
460                 }
461
462                 ixgbe_rx_checksum(adapter, staterr, skb);
463                 skb->protocol = eth_type_trans(skb, netdev);
464                 ixgbe_receive_skb(adapter, skb, is_vlan, vlan_tag);
465                 netdev->last_rx = jiffies;
466
467 next_desc:
468                 rx_desc->wb.upper.status_error = 0;
469
470                 /* return some buffers to hardware, one at a time is too slow */
471                 if (cleaned_count >= IXGBE_RX_BUFFER_WRITE) {
472                         ixgbe_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
473                         cleaned_count = 0;
474                 }
475
476                 /* use prefetched values */
477                 rx_desc = next_rxd;
478                 rx_buffer_info = next_buffer;
479
480                 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
481                 is_vlan = (staterr & IXGBE_RXD_STAT_VP);
482                 vlan_tag = le16_to_cpu(rx_desc->wb.upper.vlan);
483         }
484
485         rx_ring->next_to_clean = i;
486         cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
487
488         if (cleaned_count)
489                 ixgbe_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
490
491         return cleaned;
492 }
493
494 #define IXGBE_MAX_INTR 10
495 /**
496  * ixgbe_configure_msix - Configure MSI-X hardware
497  * @adapter: board private structure
498  *
499  * ixgbe_configure_msix sets up the hardware to properly generate MSI-X
500  * interrupts.
501  **/
502 static void ixgbe_configure_msix(struct ixgbe_adapter *adapter)
503 {
504         int i, vector = 0;
505
506         for (i = 0; i < adapter->num_tx_queues; i++) {
507                 ixgbe_set_ivar(adapter, IXGBE_IVAR_TX_QUEUE(i),
508                                IXGBE_MSIX_VECTOR(vector));
509                 writel(EITR_INTS_PER_SEC_TO_REG(adapter->tx_eitr),
510                        adapter->hw.hw_addr + adapter->tx_ring[i].itr_register);
511                 vector++;
512         }
513
514         for (i = 0; i < adapter->num_rx_queues; i++) {
515                 ixgbe_set_ivar(adapter, IXGBE_IVAR_RX_QUEUE(i),
516                                IXGBE_MSIX_VECTOR(vector));
517                 writel(EITR_INTS_PER_SEC_TO_REG(adapter->rx_eitr),
518                        adapter->hw.hw_addr + adapter->rx_ring[i].itr_register);
519                 vector++;
520         }
521
522         vector = adapter->num_tx_queues + adapter->num_rx_queues;
523         ixgbe_set_ivar(adapter, IXGBE_IVAR_OTHER_CAUSES_INDEX,
524                        IXGBE_MSIX_VECTOR(vector));
525         IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(vector), 1950);
526 }
527
528 static irqreturn_t ixgbe_msix_lsc(int irq, void *data)
529 {
530         struct net_device *netdev = data;
531         struct ixgbe_adapter *adapter = netdev_priv(netdev);
532         struct ixgbe_hw *hw = &adapter->hw;
533         u32 eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
534
535         if (eicr & IXGBE_EICR_LSC) {
536                 adapter->lsc_int++;
537                 if (!test_bit(__IXGBE_DOWN, &adapter->state))
538                         mod_timer(&adapter->watchdog_timer, jiffies);
539         }
540         IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, IXGBE_EIMS_OTHER);
541
542         return IRQ_HANDLED;
543 }
544
545 static irqreturn_t ixgbe_msix_clean_tx(int irq, void *data)
546 {
547         struct ixgbe_ring *txr = data;
548         struct ixgbe_adapter *adapter = txr->adapter;
549
550         ixgbe_clean_tx_irq(adapter, txr);
551
552         return IRQ_HANDLED;
553 }
554
555 static irqreturn_t ixgbe_msix_clean_rx(int irq, void *data)
556 {
557         struct ixgbe_ring *rxr = data;
558         struct ixgbe_adapter *adapter = rxr->adapter;
559
560         IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, rxr->eims_value);
561         netif_rx_schedule(adapter->netdev, &adapter->napi);
562         return IRQ_HANDLED;
563 }
564
565 static int ixgbe_clean_rxonly(struct napi_struct *napi, int budget)
566 {
567         struct ixgbe_adapter *adapter = container_of(napi,
568                                         struct ixgbe_adapter, napi);
569         struct net_device *netdev = adapter->netdev;
570         int work_done = 0;
571         struct ixgbe_ring *rxr = adapter->rx_ring;
572
573         /* Keep link state information with original netdev */
574         if (!netif_carrier_ok(netdev))
575                 goto quit_polling;
576
577         ixgbe_clean_rx_irq(adapter, rxr, &work_done, budget);
578
579         /* If no Tx and not enough Rx work done, exit the polling mode */
580         if ((work_done < budget) || !netif_running(netdev)) {
581 quit_polling:
582                 netif_rx_complete(netdev, napi);
583                 if (!test_bit(__IXGBE_DOWN, &adapter->state))
584                         IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS,
585                                         rxr->eims_value);
586         }
587
588         return work_done;
589 }
590
591 /**
592  * ixgbe_setup_msix - Initialize MSI-X interrupts
593  *
594  * ixgbe_setup_msix allocates MSI-X vectors and requests
595  * interrutps from the kernel.
596  **/
597 static int ixgbe_setup_msix(struct ixgbe_adapter *adapter)
598 {
599         struct net_device *netdev = adapter->netdev;
600         int i, int_vector = 0, err = 0;
601         int max_msix_count;
602
603         /* +1 for the LSC interrupt */
604         max_msix_count = adapter->num_rx_queues + adapter->num_tx_queues + 1;
605         adapter->msix_entries = kcalloc(max_msix_count,
606                                         sizeof(struct msix_entry), GFP_KERNEL);
607         if (!adapter->msix_entries)
608                 return -ENOMEM;
609
610         for (i = 0; i < max_msix_count; i++)
611                 adapter->msix_entries[i].entry = i;
612
613         err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
614                               max_msix_count);
615         if (err)
616                 goto out;
617
618         for (i = 0; i < adapter->num_tx_queues; i++) {
619                 sprintf(adapter->tx_ring[i].name, "%s-tx%d", netdev->name, i);
620                 err = request_irq(adapter->msix_entries[int_vector].vector,
621                                   &ixgbe_msix_clean_tx,
622                                   0,
623                                   adapter->tx_ring[i].name,
624                                   &(adapter->tx_ring[i]));
625                 if (err) {
626                         DPRINTK(PROBE, ERR,
627                                 "request_irq failed for MSIX interrupt "
628                                 "Error: %d\n", err);
629                         goto release_irqs;
630                 }
631                 adapter->tx_ring[i].eims_value =
632                     (1 << IXGBE_MSIX_VECTOR(int_vector));
633                 adapter->tx_ring[i].itr_register = IXGBE_EITR(int_vector);
634                 int_vector++;
635         }
636
637         for (i = 0; i < adapter->num_rx_queues; i++) {
638                 if (strlen(netdev->name) < (IFNAMSIZ - 5))
639                         sprintf(adapter->rx_ring[i].name,
640                                 "%s-rx%d", netdev->name, i);
641                 else
642                         memcpy(adapter->rx_ring[i].name,
643                                netdev->name, IFNAMSIZ);
644                 err = request_irq(adapter->msix_entries[int_vector].vector,
645                                   &ixgbe_msix_clean_rx, 0,
646                                   adapter->rx_ring[i].name,
647                                   &(adapter->rx_ring[i]));
648                 if (err) {
649                         DPRINTK(PROBE, ERR,
650                                 "request_irq failed for MSIX interrupt "
651                                 "Error: %d\n", err);
652                         goto release_irqs;
653                 }
654
655                 adapter->rx_ring[i].eims_value =
656                     (1 << IXGBE_MSIX_VECTOR(int_vector));
657                 adapter->rx_ring[i].itr_register = IXGBE_EITR(int_vector);
658                 int_vector++;
659         }
660
661         sprintf(adapter->lsc_name, "%s-lsc", netdev->name);
662         err = request_irq(adapter->msix_entries[int_vector].vector,
663                           &ixgbe_msix_lsc, 0, adapter->lsc_name, netdev);
664         if (err) {
665                 DPRINTK(PROBE, ERR,
666                         "request_irq for msix_lsc failed: %d\n", err);
667                 goto release_irqs;
668         }
669
670         /* FIXME: implement netif_napi_remove() instead */
671         adapter->napi.poll = ixgbe_clean_rxonly;
672         adapter->flags |= IXGBE_FLAG_MSIX_ENABLED;
673         return 0;
674
675 release_irqs:
676         int_vector--;
677         for (; int_vector >= adapter->num_tx_queues; int_vector--)
678                 free_irq(adapter->msix_entries[int_vector].vector,
679                          &(adapter->rx_ring[int_vector -
680                                             adapter->num_tx_queues]));
681
682         for (; int_vector >= 0; int_vector--)
683                 free_irq(adapter->msix_entries[int_vector].vector,
684                          &(adapter->tx_ring[int_vector]));
685 out:
686         kfree(adapter->msix_entries);
687         adapter->msix_entries = NULL;
688         adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
689         return err;
690 }
691
692 /**
693  * ixgbe_intr - Interrupt Handler
694  * @irq: interrupt number
695  * @data: pointer to a network interface device structure
696  * @pt_regs: CPU registers structure
697  **/
698 static irqreturn_t ixgbe_intr(int irq, void *data)
699 {
700         struct net_device *netdev = data;
701         struct ixgbe_adapter *adapter = netdev_priv(netdev);
702         struct ixgbe_hw *hw = &adapter->hw;
703         u32 eicr;
704
705         eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
706
707         if (!eicr)
708                 return IRQ_NONE;        /* Not our interrupt */
709
710         if (eicr & IXGBE_EICR_LSC) {
711                 adapter->lsc_int++;
712                 if (!test_bit(__IXGBE_DOWN, &adapter->state))
713                         mod_timer(&adapter->watchdog_timer, jiffies);
714         }
715         if (netif_rx_schedule_prep(netdev, &adapter->napi)) {
716                 /* Disable interrupts and register for poll. The flush of the
717                  * posted write is intentionally left out. */
718                 atomic_inc(&adapter->irq_sem);
719                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~0);
720                 __netif_rx_schedule(netdev, &adapter->napi);
721         }
722
723         return IRQ_HANDLED;
724 }
725
726 /**
727  * ixgbe_request_irq - initialize interrupts
728  * @adapter: board private structure
729  *
730  * Attempts to configure interrupts using the best available
731  * capabilities of the hardware and kernel.
732  **/
733 static int ixgbe_request_irq(struct ixgbe_adapter *adapter, u32 *num_rx_queues)
734 {
735         struct net_device *netdev = adapter->netdev;
736         int flags, err;
737         irqreturn_t(*handler) (int, void *) = &ixgbe_intr;
738
739         flags = IRQF_SHARED;
740
741         err = ixgbe_setup_msix(adapter);
742         if (!err)
743                 goto request_done;
744
745         /*
746          * if we can't do MSI-X, fall through and try MSI
747          * No need to reallocate memory since we're decreasing the number of
748          * queues. We just won't use the other ones, also it is freed correctly
749          * on ixgbe_remove.
750          */
751         *num_rx_queues = 1;
752
753         /* do MSI */
754         err = pci_enable_msi(adapter->pdev);
755         if (!err) {
756                 adapter->flags |= IXGBE_FLAG_MSI_ENABLED;
757                 flags &= ~IRQF_SHARED;
758                 handler = &ixgbe_intr;
759         }
760
761         err = request_irq(adapter->pdev->irq, handler, flags,
762                           netdev->name, netdev);
763         if (err)
764                 DPRINTK(PROBE, ERR, "request_irq failed, Error %d\n", err);
765
766 request_done:
767         return err;
768 }
769
770 static void ixgbe_free_irq(struct ixgbe_adapter *adapter)
771 {
772         struct net_device *netdev = adapter->netdev;
773
774         if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
775                 int i;
776
777                 for (i = 0; i < adapter->num_tx_queues; i++)
778                         free_irq(adapter->msix_entries[i].vector,
779                                  &(adapter->tx_ring[i]));
780                 for (i = 0; i < adapter->num_rx_queues; i++)
781                         free_irq(adapter->msix_entries[i +
782                                                 adapter->num_tx_queues].vector,
783                                 &(adapter->rx_ring[i]));
784                 i = adapter->num_rx_queues + adapter->num_tx_queues;
785                 free_irq(adapter->msix_entries[i].vector, netdev);
786                 pci_disable_msix(adapter->pdev);
787                 kfree(adapter->msix_entries);
788                 adapter->msix_entries = NULL;
789                 adapter->flags &= ~IXGBE_FLAG_MSIX_ENABLED;
790                 return;
791         }
792
793         free_irq(adapter->pdev->irq, netdev);
794         if (adapter->flags & IXGBE_FLAG_MSI_ENABLED) {
795                 pci_disable_msi(adapter->pdev);
796                 adapter->flags &= ~IXGBE_FLAG_MSI_ENABLED;
797         }
798 }
799
800 /**
801  * ixgbe_irq_disable - Mask off interrupt generation on the NIC
802  * @adapter: board private structure
803  **/
804 static inline void ixgbe_irq_disable(struct ixgbe_adapter *adapter)
805 {
806         atomic_inc(&adapter->irq_sem);
807         IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~0);
808         IXGBE_WRITE_FLUSH(&adapter->hw);
809         synchronize_irq(adapter->pdev->irq);
810 }
811
812 /**
813  * ixgbe_irq_enable - Enable default interrupt generation settings
814  * @adapter: board private structure
815  **/
816 static inline void ixgbe_irq_enable(struct ixgbe_adapter *adapter)
817 {
818         if (atomic_dec_and_test(&adapter->irq_sem)) {
819                 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
820                         IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC,
821                                         (IXGBE_EIMS_ENABLE_MASK &
822                                          ~(IXGBE_EIMS_OTHER | IXGBE_EIMS_LSC)));
823                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS,
824                                 IXGBE_EIMS_ENABLE_MASK);
825                 IXGBE_WRITE_FLUSH(&adapter->hw);
826         }
827 }
828
829 /**
830  * ixgbe_configure_msi_and_legacy - Initialize PIN (INTA...) and MSI interrupts
831  *
832  **/
833 static void ixgbe_configure_msi_and_legacy(struct ixgbe_adapter *adapter)
834 {
835         int i;
836         struct ixgbe_hw *hw = &adapter->hw;
837
838         if (adapter->rx_eitr)
839                 IXGBE_WRITE_REG(hw, IXGBE_EITR(0),
840                                 EITR_INTS_PER_SEC_TO_REG(adapter->rx_eitr));
841
842         /* for re-triggering the interrupt in non-NAPI mode */
843         adapter->rx_ring[0].eims_value = (1 << IXGBE_MSIX_VECTOR(0));
844         adapter->tx_ring[0].eims_value = (1 << IXGBE_MSIX_VECTOR(0));
845
846         ixgbe_set_ivar(adapter, IXGBE_IVAR_RX_QUEUE(0), 0);
847         for (i = 0; i < adapter->num_tx_queues; i++)
848                 ixgbe_set_ivar(adapter, IXGBE_IVAR_TX_QUEUE(i), i);
849 }
850
851 /**
852  * ixgbe_configure_tx - Configure 8254x Transmit Unit after Reset
853  * @adapter: board private structure
854  *
855  * Configure the Tx unit of the MAC after a reset.
856  **/
857 static void ixgbe_configure_tx(struct ixgbe_adapter *adapter)
858 {
859         u64 tdba;
860         struct ixgbe_hw *hw = &adapter->hw;
861         u32 i, tdlen;
862
863         /* Setup the HW Tx Head and Tail descriptor pointers */
864         for (i = 0; i < adapter->num_tx_queues; i++) {
865                 tdba = adapter->tx_ring[i].dma;
866                 tdlen = adapter->tx_ring[i].count *
867                     sizeof(union ixgbe_adv_tx_desc);
868                 IXGBE_WRITE_REG(hw, IXGBE_TDBAL(i), (tdba & DMA_32BIT_MASK));
869                 IXGBE_WRITE_REG(hw, IXGBE_TDBAH(i), (tdba >> 32));
870                 IXGBE_WRITE_REG(hw, IXGBE_TDLEN(i), tdlen);
871                 IXGBE_WRITE_REG(hw, IXGBE_TDH(i), 0);
872                 IXGBE_WRITE_REG(hw, IXGBE_TDT(i), 0);
873                 adapter->tx_ring[i].head = IXGBE_TDH(i);
874                 adapter->tx_ring[i].tail = IXGBE_TDT(i);
875         }
876
877         IXGBE_WRITE_REG(hw, IXGBE_TIPG, IXGBE_TIPG_FIBER_DEFAULT);
878 }
879
880 #define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
881                         (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
882
883 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT                 2
884 /**
885  * ixgbe_configure_rx - Configure 8254x Receive Unit after Reset
886  * @adapter: board private structure
887  *
888  * Configure the Rx unit of the MAC after a reset.
889  **/
890 static void ixgbe_configure_rx(struct ixgbe_adapter *adapter)
891 {
892         u64 rdba;
893         struct ixgbe_hw *hw = &adapter->hw;
894         struct net_device *netdev = adapter->netdev;
895         int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
896         u32 rdlen, rxctrl, rxcsum;
897         u32 random[10];
898         u32 reta, mrqc;
899         int i;
900         u32 fctrl, hlreg0;
901         u32 srrctl;
902         u32 pages;
903
904         /* Decide whether to use packet split mode or not */
905         if (netdev->mtu > ETH_DATA_LEN)
906                 adapter->flags |= IXGBE_FLAG_RX_PS_ENABLED;
907         else
908                 adapter->flags &= ~IXGBE_FLAG_RX_PS_ENABLED;
909
910         /* Set the RX buffer length according to the mode */
911         if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
912                 adapter->rx_buf_len = IXGBE_RX_HDR_SIZE;
913         } else {
914                 if (netdev->mtu <= ETH_DATA_LEN)
915                         adapter->rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
916                 else
917                         adapter->rx_buf_len = ALIGN(max_frame, 1024);
918         }
919
920         fctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
921         fctrl |= IXGBE_FCTRL_BAM;
922         IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, fctrl);
923
924         hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
925         if (adapter->netdev->mtu <= ETH_DATA_LEN)
926                 hlreg0 &= ~IXGBE_HLREG0_JUMBOEN;
927         else
928                 hlreg0 |= IXGBE_HLREG0_JUMBOEN;
929         IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
930
931         pages = PAGE_USE_COUNT(adapter->netdev->mtu);
932
933         srrctl = IXGBE_READ_REG(&adapter->hw, IXGBE_SRRCTL(0));
934         srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
935         srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
936
937         if (adapter->flags & IXGBE_FLAG_RX_PS_ENABLED) {
938                 srrctl |= PAGE_SIZE >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
939                 srrctl |= IXGBE_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
940                 srrctl |= ((IXGBE_RX_HDR_SIZE <<
941                             IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT) &
942                            IXGBE_SRRCTL_BSIZEHDR_MASK);
943         } else {
944                 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
945
946                 if (adapter->rx_buf_len == MAXIMUM_ETHERNET_VLAN_SIZE)
947                         srrctl |=
948                              IXGBE_RXBUFFER_2048 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
949                 else
950                         srrctl |=
951                              adapter->rx_buf_len >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
952         }
953         IXGBE_WRITE_REG(&adapter->hw, IXGBE_SRRCTL(0), srrctl);
954
955         rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
956         /* disable receives while setting up the descriptors */
957         rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
958         IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl & ~IXGBE_RXCTRL_RXEN);
959
960         /* Setup the HW Rx Head and Tail Descriptor Pointers and
961          * the Base and Length of the Rx Descriptor Ring */
962         for (i = 0; i < adapter->num_rx_queues; i++) {
963                 rdba = adapter->rx_ring[i].dma;
964                 IXGBE_WRITE_REG(hw, IXGBE_RDBAL(i), (rdba & DMA_32BIT_MASK));
965                 IXGBE_WRITE_REG(hw, IXGBE_RDBAH(i), (rdba >> 32));
966                 IXGBE_WRITE_REG(hw, IXGBE_RDLEN(i), rdlen);
967                 IXGBE_WRITE_REG(hw, IXGBE_RDH(i), 0);
968                 IXGBE_WRITE_REG(hw, IXGBE_RDT(i), 0);
969                 adapter->rx_ring[i].head = IXGBE_RDH(i);
970                 adapter->rx_ring[i].tail = IXGBE_RDT(i);
971         }
972
973         if (adapter->num_rx_queues > 1) {
974                 /* Random 40bytes used as random key in RSS hash function */
975                 get_random_bytes(&random[0], 40);
976
977                 switch (adapter->num_rx_queues) {
978                 case 8:
979                 case 4:
980                         /* Bits [3:0] in each byte refers the Rx queue no */
981                         reta = 0x00010203;
982                         break;
983                 case 2:
984                         reta = 0x00010001;
985                         break;
986                 default:
987                         reta = 0x00000000;
988                         break;
989                 }
990
991                 /* Fill out redirection table */
992                 for (i = 0; i < 32; i++) {
993                         IXGBE_WRITE_REG_ARRAY(hw, IXGBE_RETA(0), i, reta);
994                         if (adapter->num_rx_queues > 4) {
995                                 i++;
996                                 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_RETA(0), i,
997                                                       0x04050607);
998                         }
999                 }
1000
1001                 /* Fill out hash function seeds */
1002                 for (i = 0; i < 10; i++)
1003                         IXGBE_WRITE_REG_ARRAY(hw, IXGBE_RSSRK(0), i, random[i]);
1004
1005                 mrqc = IXGBE_MRQC_RSSEN
1006                     /* Perform hash on these packet types */
1007                     | IXGBE_MRQC_RSS_FIELD_IPV4
1008                     | IXGBE_MRQC_RSS_FIELD_IPV4_TCP
1009                     | IXGBE_MRQC_RSS_FIELD_IPV4_UDP
1010                     | IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP
1011                     | IXGBE_MRQC_RSS_FIELD_IPV6_EX
1012                     | IXGBE_MRQC_RSS_FIELD_IPV6
1013                     | IXGBE_MRQC_RSS_FIELD_IPV6_TCP
1014                     | IXGBE_MRQC_RSS_FIELD_IPV6_UDP
1015                     | IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP;
1016                 IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
1017
1018                 /* Multiqueue and packet checksumming are mutually exclusive. */
1019                 rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
1020                 rxcsum |= IXGBE_RXCSUM_PCSD;
1021                 IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
1022         } else {
1023                 /* Enable Receive Checksum Offload for TCP and UDP */
1024                 rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
1025                 if (adapter->flags & IXGBE_FLAG_RX_CSUM_ENABLED) {
1026                         /* Enable IPv4 payload checksum for UDP fragments
1027                          * Must be used in conjunction with packet-split. */
1028                         rxcsum |= IXGBE_RXCSUM_IPPCSE;
1029                 } else {
1030                         /* don't need to clear IPPCSE as it defaults to 0 */
1031                 }
1032                 IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
1033         }
1034         /* Enable Receives */
1035         IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl);
1036         rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
1037 }
1038
1039 static void ixgbe_vlan_rx_register(struct net_device *netdev,
1040                                    struct vlan_group *grp)
1041 {
1042         struct ixgbe_adapter *adapter = netdev_priv(netdev);
1043         u32 ctrl;
1044
1045         ixgbe_irq_disable(adapter);
1046         adapter->vlgrp = grp;
1047
1048         if (grp) {
1049                 /* enable VLAN tag insert/strip */
1050                 ctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_VLNCTRL);
1051                 ctrl |= IXGBE_VLNCTRL_VME | IXGBE_VLNCTRL_VFE;
1052                 ctrl &= ~IXGBE_VLNCTRL_CFIEN;
1053                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_VLNCTRL, ctrl);
1054         }
1055
1056         ixgbe_irq_enable(adapter);
1057 }
1058
1059 static void ixgbe_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1060 {
1061         struct ixgbe_adapter *adapter = netdev_priv(netdev);
1062
1063         /* add VID to filter table */
1064         ixgbe_set_vfta(&adapter->hw, vid, 0, true);
1065 }
1066
1067 static void ixgbe_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1068 {
1069         struct ixgbe_adapter *adapter = netdev_priv(netdev);
1070
1071         ixgbe_irq_disable(adapter);
1072         vlan_group_set_device(adapter->vlgrp, vid, NULL);
1073         ixgbe_irq_enable(adapter);
1074
1075         /* remove VID from filter table */
1076         ixgbe_set_vfta(&adapter->hw, vid, 0, false);
1077 }
1078
1079 static void ixgbe_restore_vlan(struct ixgbe_adapter *adapter)
1080 {
1081         ixgbe_vlan_rx_register(adapter->netdev, adapter->vlgrp);
1082
1083         if (adapter->vlgrp) {
1084                 u16 vid;
1085                 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
1086                         if (!vlan_group_get_device(adapter->vlgrp, vid))
1087                                 continue;
1088                         ixgbe_vlan_rx_add_vid(adapter->netdev, vid);
1089                 }
1090         }
1091 }
1092
1093 /**
1094  * ixgbe_set_multi - Multicast and Promiscuous mode set
1095  * @netdev: network interface device structure
1096  *
1097  * The set_multi entry point is called whenever the multicast address
1098  * list or the network interface flags are updated.  This routine is
1099  * responsible for configuring the hardware for proper multicast,
1100  * promiscuous mode, and all-multi behavior.
1101  **/
1102 static void ixgbe_set_multi(struct net_device *netdev)
1103 {
1104         struct ixgbe_adapter *adapter = netdev_priv(netdev);
1105         struct ixgbe_hw *hw = &adapter->hw;
1106         struct dev_mc_list *mc_ptr;
1107         u8 *mta_list;
1108         u32 fctrl;
1109         int i;
1110
1111         /* Check for Promiscuous and All Multicast modes */
1112
1113         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
1114
1115         if (netdev->flags & IFF_PROMISC) {
1116                 fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
1117         } else if (netdev->flags & IFF_ALLMULTI) {
1118                 fctrl |= IXGBE_FCTRL_MPE;
1119                 fctrl &= ~IXGBE_FCTRL_UPE;
1120         } else {
1121                 fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
1122         }
1123
1124         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
1125
1126         if (netdev->mc_count) {
1127                 mta_list = kcalloc(netdev->mc_count, ETH_ALEN, GFP_ATOMIC);
1128                 if (!mta_list)
1129                         return;
1130
1131                 /* Shared function expects packed array of only addresses. */
1132                 mc_ptr = netdev->mc_list;
1133
1134                 for (i = 0; i < netdev->mc_count; i++) {
1135                         if (!mc_ptr)
1136                                 break;
1137                         memcpy(mta_list + (i * ETH_ALEN), mc_ptr->dmi_addr,
1138                                ETH_ALEN);
1139                         mc_ptr = mc_ptr->next;
1140                 }
1141
1142                 ixgbe_update_mc_addr_list(hw, mta_list, i, 0);
1143                 kfree(mta_list);
1144         } else {
1145                 ixgbe_update_mc_addr_list(hw, NULL, 0, 0);
1146         }
1147
1148 }
1149
1150 static void ixgbe_configure(struct ixgbe_adapter *adapter)
1151 {
1152         struct net_device *netdev = adapter->netdev;
1153         int i;
1154
1155         ixgbe_set_multi(netdev);
1156
1157         ixgbe_restore_vlan(adapter);
1158
1159         ixgbe_configure_tx(adapter);
1160         ixgbe_configure_rx(adapter);
1161         for (i = 0; i < adapter->num_rx_queues; i++)
1162                 ixgbe_alloc_rx_buffers(adapter, &adapter->rx_ring[i],
1163                                            (adapter->rx_ring[i].count - 1));
1164 }
1165
1166 static int ixgbe_up_complete(struct ixgbe_adapter *adapter)
1167 {
1168         struct net_device *netdev = adapter->netdev;
1169         int i;
1170         u32 gpie = 0;
1171         struct ixgbe_hw *hw = &adapter->hw;
1172         u32 txdctl, rxdctl, mhadd;
1173         int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1174
1175         if (adapter->flags & (IXGBE_FLAG_MSIX_ENABLED |
1176                               IXGBE_FLAG_MSI_ENABLED)) {
1177                 if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED) {
1178                         gpie = (IXGBE_GPIE_MSIX_MODE | IXGBE_GPIE_EIAME |
1179                                 IXGBE_GPIE_PBA_SUPPORT | IXGBE_GPIE_OCD);
1180                 } else {
1181                         /* MSI only */
1182                         gpie = (IXGBE_GPIE_EIAME |
1183                                 IXGBE_GPIE_PBA_SUPPORT);
1184                 }
1185                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_GPIE, gpie);
1186                 gpie = IXGBE_READ_REG(&adapter->hw, IXGBE_GPIE);
1187         }
1188
1189         mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
1190
1191         if (max_frame != (mhadd >> IXGBE_MHADD_MFS_SHIFT)) {
1192                 mhadd &= ~IXGBE_MHADD_MFS_MASK;
1193                 mhadd |= max_frame << IXGBE_MHADD_MFS_SHIFT;
1194
1195                 IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
1196         }
1197
1198         for (i = 0; i < adapter->num_tx_queues; i++) {
1199                 txdctl = IXGBE_READ_REG(&adapter->hw, IXGBE_TXDCTL(i));
1200                 txdctl |= IXGBE_TXDCTL_ENABLE;
1201                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_TXDCTL(i), txdctl);
1202         }
1203
1204         for (i = 0; i < adapter->num_rx_queues; i++) {
1205                 rxdctl = IXGBE_READ_REG(&adapter->hw, IXGBE_RXDCTL(i));
1206                 rxdctl |= IXGBE_RXDCTL_ENABLE;
1207                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_RXDCTL(i), rxdctl);
1208         }
1209         /* enable all receives */
1210         rxdctl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
1211         rxdctl |= (IXGBE_RXCTRL_DMBYPS | IXGBE_RXCTRL_RXEN);
1212         IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxdctl);
1213
1214         if (adapter->flags & IXGBE_FLAG_MSIX_ENABLED)
1215                 ixgbe_configure_msix(adapter);
1216         else
1217                 ixgbe_configure_msi_and_legacy(adapter);
1218
1219         clear_bit(__IXGBE_DOWN, &adapter->state);
1220         napi_enable(&adapter->napi);
1221         ixgbe_irq_enable(adapter);
1222
1223         /* bring the link up in the watchdog, this could race with our first
1224          * link up interrupt but shouldn't be a problem */
1225         mod_timer(&adapter->watchdog_timer, jiffies);
1226         return 0;
1227 }
1228
1229 int ixgbe_up(struct ixgbe_adapter *adapter)
1230 {
1231         /* hardware has been reset, we need to reload some things */
1232         ixgbe_configure(adapter);
1233
1234         return ixgbe_up_complete(adapter);
1235 }
1236
1237 void ixgbe_reset(struct ixgbe_adapter *adapter)
1238 {
1239         if (ixgbe_init_hw(&adapter->hw))
1240                 DPRINTK(PROBE, ERR, "Hardware Error\n");
1241
1242         /* reprogram the RAR[0] in case user changed it. */
1243         ixgbe_set_rar(&adapter->hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
1244
1245 }
1246
1247 #ifdef CONFIG_PM
1248 static int ixgbe_resume(struct pci_dev *pdev)
1249 {
1250         struct net_device *netdev = pci_get_drvdata(pdev);
1251         struct ixgbe_adapter *adapter = netdev_priv(netdev);
1252         u32 err, num_rx_queues = adapter->num_rx_queues;
1253
1254         pci_set_power_state(pdev, PCI_D0);
1255         pci_restore_state(pdev);
1256         err = pci_enable_device(pdev);
1257         if (err) {
1258                 printk(KERN_ERR "ixgbe: Cannot enable PCI device from " \
1259                                 "suspend\n");
1260                 return err;
1261         }
1262         pci_set_master(pdev);
1263
1264         pci_enable_wake(pdev, PCI_D3hot, 0);
1265         pci_enable_wake(pdev, PCI_D3cold, 0);
1266
1267         if (netif_running(netdev)) {
1268                 err = ixgbe_request_irq(adapter, &num_rx_queues);
1269                 if (err)
1270                         return err;
1271         }
1272
1273         ixgbe_reset(adapter);
1274
1275         if (netif_running(netdev))
1276                 ixgbe_up(adapter);
1277
1278         netif_device_attach(netdev);
1279
1280         return 0;
1281 }
1282 #endif
1283
1284 /**
1285  * ixgbe_clean_rx_ring - Free Rx Buffers per Queue
1286  * @adapter: board private structure
1287  * @rx_ring: ring to free buffers from
1288  **/
1289 static void ixgbe_clean_rx_ring(struct ixgbe_adapter *adapter,
1290                                 struct ixgbe_ring *rx_ring)
1291 {
1292         struct pci_dev *pdev = adapter->pdev;
1293         unsigned long size;
1294         unsigned int i;
1295
1296         /* Free all the Rx ring sk_buffs */
1297
1298         for (i = 0; i < rx_ring->count; i++) {
1299                 struct ixgbe_rx_buffer *rx_buffer_info;
1300
1301                 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1302                 if (rx_buffer_info->dma) {
1303                         pci_unmap_single(pdev, rx_buffer_info->dma,
1304                                          adapter->rx_buf_len,
1305                                          PCI_DMA_FROMDEVICE);
1306                         rx_buffer_info->dma = 0;
1307                 }
1308                 if (rx_buffer_info->skb) {
1309                         dev_kfree_skb(rx_buffer_info->skb);
1310                         rx_buffer_info->skb = NULL;
1311                 }
1312                 if (!rx_buffer_info->page)
1313                         continue;
1314                 pci_unmap_page(pdev, rx_buffer_info->page_dma, PAGE_SIZE,
1315                                PCI_DMA_FROMDEVICE);
1316                 rx_buffer_info->page_dma = 0;
1317
1318                 put_page(rx_buffer_info->page);
1319                 rx_buffer_info->page = NULL;
1320         }
1321
1322         size = sizeof(struct ixgbe_rx_buffer) * rx_ring->count;
1323         memset(rx_ring->rx_buffer_info, 0, size);
1324
1325         /* Zero out the descriptor ring */
1326         memset(rx_ring->desc, 0, rx_ring->size);
1327
1328         rx_ring->next_to_clean = 0;
1329         rx_ring->next_to_use = 0;
1330
1331         writel(0, adapter->hw.hw_addr + rx_ring->head);
1332         writel(0, adapter->hw.hw_addr + rx_ring->tail);
1333 }
1334
1335 /**
1336  * ixgbe_clean_tx_ring - Free Tx Buffers
1337  * @adapter: board private structure
1338  * @tx_ring: ring to be cleaned
1339  **/
1340 static void ixgbe_clean_tx_ring(struct ixgbe_adapter *adapter,
1341                                 struct ixgbe_ring *tx_ring)
1342 {
1343         struct ixgbe_tx_buffer *tx_buffer_info;
1344         unsigned long size;
1345         unsigned int i;
1346
1347         /* Free all the Tx ring sk_buffs */
1348
1349         for (i = 0; i < tx_ring->count; i++) {
1350                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
1351                 ixgbe_unmap_and_free_tx_resource(adapter, tx_buffer_info);
1352         }
1353
1354         size = sizeof(struct ixgbe_tx_buffer) * tx_ring->count;
1355         memset(tx_ring->tx_buffer_info, 0, size);
1356
1357         /* Zero out the descriptor ring */
1358         memset(tx_ring->desc, 0, tx_ring->size);
1359
1360         tx_ring->next_to_use = 0;
1361         tx_ring->next_to_clean = 0;
1362
1363         writel(0, adapter->hw.hw_addr + tx_ring->head);
1364         writel(0, adapter->hw.hw_addr + tx_ring->tail);
1365 }
1366
1367 /**
1368  * ixgbe_clean_all_tx_rings - Free Tx Buffers for all queues
1369  * @adapter: board private structure
1370  **/
1371 static void ixgbe_clean_all_tx_rings(struct ixgbe_adapter *adapter)
1372 {
1373         int i;
1374
1375         for (i = 0; i < adapter->num_tx_queues; i++)
1376                 ixgbe_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1377 }
1378
1379 /**
1380  * ixgbe_clean_all_rx_rings - Free Rx Buffers for all queues
1381  * @adapter: board private structure
1382  **/
1383 static void ixgbe_clean_all_rx_rings(struct ixgbe_adapter *adapter)
1384 {
1385         int i;
1386
1387         for (i = 0; i < adapter->num_rx_queues; i++)
1388                 ixgbe_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1389 }
1390
1391 void ixgbe_down(struct ixgbe_adapter *adapter)
1392 {
1393         struct net_device *netdev = adapter->netdev;
1394         u32 rxctrl;
1395
1396         /* signal that we are down to the interrupt handler */
1397         set_bit(__IXGBE_DOWN, &adapter->state);
1398
1399         /* disable receives */
1400         rxctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_RXCTRL);
1401         IXGBE_WRITE_REG(&adapter->hw, IXGBE_RXCTRL,
1402                         rxctrl & ~IXGBE_RXCTRL_RXEN);
1403
1404         netif_tx_disable(netdev);
1405
1406         /* disable transmits in the hardware */
1407
1408         /* flush both disables */
1409         IXGBE_WRITE_FLUSH(&adapter->hw);
1410         msleep(10);
1411
1412         ixgbe_irq_disable(adapter);
1413
1414         napi_disable(&adapter->napi);
1415         del_timer_sync(&adapter->watchdog_timer);
1416
1417         netif_carrier_off(netdev);
1418         netif_stop_queue(netdev);
1419
1420         ixgbe_reset(adapter);
1421         ixgbe_clean_all_tx_rings(adapter);
1422         ixgbe_clean_all_rx_rings(adapter);
1423
1424 }
1425
1426 static int ixgbe_suspend(struct pci_dev *pdev, pm_message_t state)
1427 {
1428         struct net_device *netdev = pci_get_drvdata(pdev);
1429         struct ixgbe_adapter *adapter = netdev_priv(netdev);
1430 #ifdef CONFIG_PM
1431         int retval = 0;
1432 #endif
1433
1434         netif_device_detach(netdev);
1435
1436         if (netif_running(netdev)) {
1437                 ixgbe_down(adapter);
1438                 ixgbe_free_irq(adapter);
1439         }
1440
1441 #ifdef CONFIG_PM
1442         retval = pci_save_state(pdev);
1443         if (retval)
1444                 return retval;
1445 #endif
1446
1447         pci_enable_wake(pdev, PCI_D3hot, 0);
1448         pci_enable_wake(pdev, PCI_D3cold, 0);
1449
1450         pci_disable_device(pdev);
1451
1452         pci_set_power_state(pdev, pci_choose_state(pdev, state));
1453
1454         return 0;
1455 }
1456
1457 static void ixgbe_shutdown(struct pci_dev *pdev)
1458 {
1459         ixgbe_suspend(pdev, PMSG_SUSPEND);
1460 }
1461
1462 /**
1463  * ixgbe_clean - NAPI Rx polling callback
1464  * @adapter: board private structure
1465  **/
1466 static int ixgbe_clean(struct napi_struct *napi, int budget)
1467 {
1468         struct ixgbe_adapter *adapter = container_of(napi,
1469                                         struct ixgbe_adapter, napi);
1470         struct net_device *netdev = adapter->netdev;
1471         int tx_cleaned = 0, work_done = 0;
1472
1473         /* Keep link state information with original netdev */
1474         if (!netif_carrier_ok(adapter->netdev))
1475                 goto quit_polling;
1476
1477         /* In non-MSIX case, there is no multi-Tx/Rx queue */
1478         tx_cleaned = ixgbe_clean_tx_irq(adapter, adapter->tx_ring);
1479         ixgbe_clean_rx_irq(adapter, &adapter->rx_ring[0], &work_done,
1480                            budget);
1481
1482         /* If no Tx and not enough Rx work done, exit the polling mode */
1483         if ((!tx_cleaned && (work_done < budget)) ||
1484             !netif_running(adapter->netdev)) {
1485 quit_polling:
1486                 netif_rx_complete(netdev, napi);
1487                 ixgbe_irq_enable(adapter);
1488         }
1489
1490         return work_done;
1491 }
1492
1493 /**
1494  * ixgbe_tx_timeout - Respond to a Tx Hang
1495  * @netdev: network interface device structure
1496  **/
1497 static void ixgbe_tx_timeout(struct net_device *netdev)
1498 {
1499         struct ixgbe_adapter *adapter = netdev_priv(netdev);
1500
1501         /* Do the reset outside of interrupt context */
1502         schedule_work(&adapter->reset_task);
1503 }
1504
1505 static void ixgbe_reset_task(struct work_struct *work)
1506 {
1507         struct ixgbe_adapter *adapter;
1508         adapter = container_of(work, struct ixgbe_adapter, reset_task);
1509
1510         adapter->tx_timeout_count++;
1511
1512         ixgbe_down(adapter);
1513         ixgbe_up(adapter);
1514 }
1515
1516 /**
1517  * ixgbe_alloc_queues - Allocate memory for all rings
1518  * @adapter: board private structure to initialize
1519  *
1520  * We allocate one ring per queue at run-time since we don't know the
1521  * number of queues at compile-time.  The polling_netdev array is
1522  * intended for Multiqueue, but should work fine with a single queue.
1523  **/
1524 static int __devinit ixgbe_alloc_queues(struct ixgbe_adapter *adapter)
1525 {
1526         int i;
1527
1528         adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1529                                    sizeof(struct ixgbe_ring), GFP_KERNEL);
1530         if (!adapter->tx_ring)
1531                 return -ENOMEM;
1532
1533         for (i = 0; i < adapter->num_tx_queues; i++)
1534                 adapter->tx_ring[i].count = IXGBE_DEFAULT_TXD;
1535
1536         adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1537                                    sizeof(struct ixgbe_ring), GFP_KERNEL);
1538         if (!adapter->rx_ring) {
1539                 kfree(adapter->tx_ring);
1540                 return -ENOMEM;
1541         }
1542
1543         for (i = 0; i < adapter->num_rx_queues; i++) {
1544                 adapter->rx_ring[i].adapter = adapter;
1545                 adapter->rx_ring[i].itr_register = IXGBE_EITR(i);
1546                 adapter->rx_ring[i].count = IXGBE_DEFAULT_RXD;
1547         }
1548
1549         return 0;
1550 }
1551
1552 /**
1553  * ixgbe_sw_init - Initialize general software structures (struct ixgbe_adapter)
1554  * @adapter: board private structure to initialize
1555  *
1556  * ixgbe_sw_init initializes the Adapter private data structure.
1557  * Fields are initialized based on PCI device information and
1558  * OS network device settings (MTU size).
1559  **/
1560 static int __devinit ixgbe_sw_init(struct ixgbe_adapter *adapter)
1561 {
1562         struct ixgbe_hw *hw = &adapter->hw;
1563         struct pci_dev *pdev = adapter->pdev;
1564
1565         /* default flow control settings */
1566         hw->fc.original_type = ixgbe_fc_full;
1567         hw->fc.type = ixgbe_fc_full;
1568
1569         hw->mac.link_mode_select = IXGBE_AUTOC_LMS_10G_LINK_NO_AN;
1570         if (hw->mac.ops.reset(hw)) {
1571                 dev_err(&pdev->dev, "HW Init failed\n");
1572                 return -EIO;
1573         }
1574         if (hw->phy.ops.setup_speed(hw, IXGBE_LINK_SPEED_10GB_FULL, true,
1575                                    false)) {
1576                 dev_err(&pdev->dev, "Link Speed setup failed\n");
1577                 return -EIO;
1578         }
1579
1580         /* initialize eeprom parameters */
1581         if (ixgbe_init_eeprom(hw)) {
1582                 dev_err(&pdev->dev, "EEPROM initialization failed\n");
1583                 return -EIO;
1584         }
1585
1586         /* Set the default values */
1587         adapter->num_rx_queues = IXGBE_DEFAULT_RXQ;
1588         adapter->num_tx_queues = 1;
1589         adapter->flags |= IXGBE_FLAG_RX_CSUM_ENABLED;
1590
1591         if (ixgbe_alloc_queues(adapter)) {
1592                 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
1593                 return -ENOMEM;
1594         }
1595
1596         atomic_set(&adapter->irq_sem, 1);
1597         set_bit(__IXGBE_DOWN, &adapter->state);
1598
1599         return 0;
1600 }
1601
1602 /**
1603  * ixgbe_setup_tx_resources - allocate Tx resources (Descriptors)
1604  * @adapter: board private structure
1605  * @txdr:    tx descriptor ring (for a specific queue) to setup
1606  *
1607  * Return 0 on success, negative on failure
1608  **/
1609 int ixgbe_setup_tx_resources(struct ixgbe_adapter *adapter,
1610                              struct ixgbe_ring *txdr)
1611 {
1612         struct pci_dev *pdev = adapter->pdev;
1613         int size;
1614
1615         size = sizeof(struct ixgbe_tx_buffer) * txdr->count;
1616         txdr->tx_buffer_info = vmalloc(size);
1617         if (!txdr->tx_buffer_info) {
1618                 DPRINTK(PROBE, ERR,
1619                 "Unable to allocate memory for the transmit descriptor ring\n");
1620                 return -ENOMEM;
1621         }
1622         memset(txdr->tx_buffer_info, 0, size);
1623
1624         /* round up to nearest 4K */
1625         txdr->size = txdr->count * sizeof(union ixgbe_adv_tx_desc);
1626         txdr->size = ALIGN(txdr->size, 4096);
1627
1628         txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
1629         if (!txdr->desc) {
1630                 vfree(txdr->tx_buffer_info);
1631                 DPRINTK(PROBE, ERR,
1632                         "Memory allocation failed for the tx desc ring\n");
1633                 return -ENOMEM;
1634         }
1635
1636         txdr->adapter = adapter;
1637         txdr->next_to_use = 0;
1638         txdr->next_to_clean = 0;
1639         txdr->work_limit = txdr->count;
1640         spin_lock_init(&txdr->tx_lock);
1641
1642         return 0;
1643 }
1644
1645 /**
1646  * ixgbe_setup_rx_resources - allocate Rx resources (Descriptors)
1647  * @adapter: board private structure
1648  * @rxdr:    rx descriptor ring (for a specific queue) to setup
1649  *
1650  * Returns 0 on success, negative on failure
1651  **/
1652 int ixgbe_setup_rx_resources(struct ixgbe_adapter *adapter,
1653                              struct ixgbe_ring *rxdr)
1654 {
1655         struct pci_dev *pdev = adapter->pdev;
1656         int size, desc_len;
1657
1658         size = sizeof(struct ixgbe_rx_buffer) * rxdr->count;
1659         rxdr->rx_buffer_info = vmalloc(size);
1660         if (!rxdr->rx_buffer_info) {
1661                 DPRINTK(PROBE, ERR,
1662                         "vmalloc allocation failed for the rx desc ring\n");
1663                 return -ENOMEM;
1664         }
1665         memset(rxdr->rx_buffer_info, 0, size);
1666
1667         desc_len = sizeof(union ixgbe_adv_rx_desc);
1668
1669         /* Round up to nearest 4K */
1670         rxdr->size = rxdr->count * desc_len;
1671         rxdr->size = ALIGN(rxdr->size, 4096);
1672
1673         rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1674
1675         if (!rxdr->desc) {
1676                 DPRINTK(PROBE, ERR,
1677                         "Memory allocation failed for the rx desc ring\n");
1678                 vfree(rxdr->rx_buffer_info);
1679                 return -ENOMEM;
1680         }
1681
1682         rxdr->next_to_clean = 0;
1683         rxdr->next_to_use = 0;
1684         rxdr->adapter = adapter;
1685
1686         return 0;
1687 }
1688
1689 /**
1690  * ixgbe_free_tx_resources - Free Tx Resources per Queue
1691  * @adapter: board private structure
1692  * @tx_ring: Tx descriptor ring for a specific queue
1693  *
1694  * Free all transmit software resources
1695  **/
1696 static void ixgbe_free_tx_resources(struct ixgbe_adapter *adapter,
1697                                     struct ixgbe_ring *tx_ring)
1698 {
1699         struct pci_dev *pdev = adapter->pdev;
1700
1701         ixgbe_clean_tx_ring(adapter, tx_ring);
1702
1703         vfree(tx_ring->tx_buffer_info);
1704         tx_ring->tx_buffer_info = NULL;
1705
1706         pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
1707
1708         tx_ring->desc = NULL;
1709 }
1710
1711 /**
1712  * ixgbe_free_all_tx_resources - Free Tx Resources for All Queues
1713  * @adapter: board private structure
1714  *
1715  * Free all transmit software resources
1716  **/
1717 static void ixgbe_free_all_tx_resources(struct ixgbe_adapter *adapter)
1718 {
1719         int i;
1720
1721         for (i = 0; i < adapter->num_tx_queues; i++)
1722                 ixgbe_free_tx_resources(adapter, &adapter->tx_ring[i]);
1723 }
1724
1725 /**
1726  * ixgbe_free_rx_resources - Free Rx Resources
1727  * @adapter: board private structure
1728  * @rx_ring: ring to clean the resources from
1729  *
1730  * Free all receive software resources
1731  **/
1732 static void ixgbe_free_rx_resources(struct ixgbe_adapter *adapter,
1733                                     struct ixgbe_ring *rx_ring)
1734 {
1735         struct pci_dev *pdev = adapter->pdev;
1736
1737         ixgbe_clean_rx_ring(adapter, rx_ring);
1738
1739         vfree(rx_ring->rx_buffer_info);
1740         rx_ring->rx_buffer_info = NULL;
1741
1742         pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
1743
1744         rx_ring->desc = NULL;
1745 }
1746
1747 /**
1748  * ixgbe_free_all_rx_resources - Free Rx Resources for All Queues
1749  * @adapter: board private structure
1750  *
1751  * Free all receive software resources
1752  **/
1753 static void ixgbe_free_all_rx_resources(struct ixgbe_adapter *adapter)
1754 {
1755         int i;
1756
1757         for (i = 0; i < adapter->num_rx_queues; i++)
1758                 ixgbe_free_rx_resources(adapter, &adapter->rx_ring[i]);
1759 }
1760
1761 /**
1762  * ixgbe_setup_all_tx_resources - wrapper to allocate Tx resources
1763  *                                (Descriptors) for all queues
1764  * @adapter: board private structure
1765  *
1766  * If this function returns with an error, then it's possible one or
1767  * more of the rings is populated (while the rest are not).  It is the
1768  * callers duty to clean those orphaned rings.
1769  *
1770  * Return 0 on success, negative on failure
1771  **/
1772 static int ixgbe_setup_all_tx_resources(struct ixgbe_adapter *adapter)
1773 {
1774         int i, err = 0;
1775
1776         for (i = 0; i < adapter->num_tx_queues; i++) {
1777                 err = ixgbe_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1778                 if (err) {
1779                         DPRINTK(PROBE, ERR,
1780                                 "Allocation for Tx Queue %u failed\n", i);
1781                         break;
1782                 }
1783         }
1784
1785         return err;
1786 }
1787
1788 /**
1789  * ixgbe_setup_all_rx_resources - wrapper to allocate Rx resources
1790  *                                (Descriptors) for all queues
1791  * @adapter: board private structure
1792  *
1793  * If this function returns with an error, then it's possible one or
1794  * more of the rings is populated (while the rest are not).  It is the
1795  * callers duty to clean those orphaned rings.
1796  *
1797  * Return 0 on success, negative on failure
1798  **/
1799
1800 static int ixgbe_setup_all_rx_resources(struct ixgbe_adapter *adapter)
1801 {
1802         int i, err = 0;
1803
1804         for (i = 0; i < adapter->num_rx_queues; i++) {
1805                 err = ixgbe_setup_rx_resources(adapter, &adapter->rx_ring[i]);
1806                 if (err) {
1807                         DPRINTK(PROBE, ERR,
1808                                 "Allocation for Rx Queue %u failed\n", i);
1809                         break;
1810                 }
1811         }
1812
1813         return err;
1814 }
1815
1816 /**
1817  * ixgbe_change_mtu - Change the Maximum Transfer Unit
1818  * @netdev: network interface device structure
1819  * @new_mtu: new value for maximum frame size
1820  *
1821  * Returns 0 on success, negative on failure
1822  **/
1823 static int ixgbe_change_mtu(struct net_device *netdev, int new_mtu)
1824 {
1825         struct ixgbe_adapter *adapter = netdev_priv(netdev);
1826         int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
1827
1828         if ((max_frame < (ETH_ZLEN + ETH_FCS_LEN)) ||
1829             (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE))
1830                 return -EINVAL;
1831
1832         netdev->mtu = new_mtu;
1833
1834         if (netif_running(netdev)) {
1835                 ixgbe_down(adapter);
1836                 ixgbe_up(adapter);
1837         }
1838
1839         return 0;
1840 }
1841
1842 /**
1843  * ixgbe_open - Called when a network interface is made active
1844  * @netdev: network interface device structure
1845  *
1846  * Returns 0 on success, negative value on failure
1847  *
1848  * The open entry point is called when a network interface is made
1849  * active by the system (IFF_UP).  At this point all resources needed
1850  * for transmit and receive operations are allocated, the interrupt
1851  * handler is registered with the OS, the watchdog timer is started,
1852  * and the stack is notified that the interface is ready.
1853  **/
1854 static int ixgbe_open(struct net_device *netdev)
1855 {
1856         struct ixgbe_adapter *adapter = netdev_priv(netdev);
1857         int err;
1858         u32 ctrl_ext;
1859         u32 num_rx_queues = adapter->num_rx_queues;
1860
1861         /* Let firmware know the driver has taken over */
1862         ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
1863         IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
1864                         ctrl_ext | IXGBE_CTRL_EXT_DRV_LOAD);
1865
1866 try_intr_reinit:
1867         /* allocate transmit descriptors */
1868         err = ixgbe_setup_all_tx_resources(adapter);
1869         if (err)
1870                 goto err_setup_tx;
1871
1872         if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED)) {
1873                 num_rx_queues = 1;
1874                 adapter->num_rx_queues = num_rx_queues;
1875         }
1876
1877         /* allocate receive descriptors */
1878         err = ixgbe_setup_all_rx_resources(adapter);
1879         if (err)
1880                 goto err_setup_rx;
1881
1882         ixgbe_configure(adapter);
1883
1884         err = ixgbe_request_irq(adapter, &num_rx_queues);
1885         if (err)
1886                 goto err_req_irq;
1887
1888         /* ixgbe_request might have reduced num_rx_queues */
1889         if (num_rx_queues < adapter->num_rx_queues) {
1890                 /* We didn't get MSI-X, so we need to release everything,
1891                  * set our Rx queue count to num_rx_queues, and redo the
1892                  * whole init process.
1893                  */
1894                 ixgbe_free_irq(adapter);
1895                 if (adapter->flags & IXGBE_FLAG_MSI_ENABLED) {
1896                         pci_disable_msi(adapter->pdev);
1897                         adapter->flags &= ~IXGBE_FLAG_MSI_ENABLED;
1898                 }
1899                 ixgbe_free_all_rx_resources(adapter);
1900                 ixgbe_free_all_tx_resources(adapter);
1901                 adapter->num_rx_queues = num_rx_queues;
1902
1903                 /* Reset the hardware, and start over. */
1904                 ixgbe_reset(adapter);
1905
1906                 goto try_intr_reinit;
1907         }
1908
1909         err = ixgbe_up_complete(adapter);
1910         if (err)
1911                 goto err_up;
1912
1913         return 0;
1914
1915 err_up:
1916         ixgbe_free_irq(adapter);
1917 err_req_irq:
1918         ixgbe_free_all_rx_resources(adapter);
1919 err_setup_rx:
1920         ixgbe_free_all_tx_resources(adapter);
1921 err_setup_tx:
1922         ixgbe_reset(adapter);
1923
1924         return err;
1925 }
1926
1927 /**
1928  * ixgbe_close - Disables a network interface
1929  * @netdev: network interface device structure
1930  *
1931  * Returns 0, this is not allowed to fail
1932  *
1933  * The close entry point is called when an interface is de-activated
1934  * by the OS.  The hardware is still under the drivers control, but
1935  * needs to be disabled.  A global MAC reset is issued to stop the
1936  * hardware, and all transmit and receive resources are freed.
1937  **/
1938 static int ixgbe_close(struct net_device *netdev)
1939 {
1940         struct ixgbe_adapter *adapter = netdev_priv(netdev);
1941         u32 ctrl_ext;
1942
1943         ixgbe_down(adapter);
1944         ixgbe_free_irq(adapter);
1945
1946         ixgbe_free_all_tx_resources(adapter);
1947         ixgbe_free_all_rx_resources(adapter);
1948
1949         ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
1950         IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT,
1951                         ctrl_ext & ~IXGBE_CTRL_EXT_DRV_LOAD);
1952
1953         return 0;
1954 }
1955
1956 /**
1957  * ixgbe_update_stats - Update the board statistics counters.
1958  * @adapter: board private structure
1959  **/
1960 void ixgbe_update_stats(struct ixgbe_adapter *adapter)
1961 {
1962         struct ixgbe_hw *hw = &adapter->hw;
1963         u64 good_rx, missed_rx, bprc;
1964
1965         adapter->stats.crcerrs += IXGBE_READ_REG(hw, IXGBE_CRCERRS);
1966         good_rx = IXGBE_READ_REG(hw, IXGBE_GPRC);
1967         missed_rx = IXGBE_READ_REG(hw, IXGBE_MPC(0));
1968         missed_rx += IXGBE_READ_REG(hw, IXGBE_MPC(1));
1969         missed_rx += IXGBE_READ_REG(hw, IXGBE_MPC(2));
1970         missed_rx += IXGBE_READ_REG(hw, IXGBE_MPC(3));
1971         missed_rx += IXGBE_READ_REG(hw, IXGBE_MPC(4));
1972         missed_rx += IXGBE_READ_REG(hw, IXGBE_MPC(5));
1973         missed_rx += IXGBE_READ_REG(hw, IXGBE_MPC(6));
1974         missed_rx += IXGBE_READ_REG(hw, IXGBE_MPC(7));
1975         adapter->stats.gprc += (good_rx - missed_rx);
1976
1977         adapter->stats.mpc[0] += missed_rx;
1978         adapter->stats.gorc += IXGBE_READ_REG(hw, IXGBE_GORCH);
1979         bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
1980         adapter->stats.bprc += bprc;
1981         adapter->stats.mprc += IXGBE_READ_REG(hw, IXGBE_MPRC);
1982         adapter->stats.mprc -= bprc;
1983         adapter->stats.roc += IXGBE_READ_REG(hw, IXGBE_ROC);
1984         adapter->stats.prc64 += IXGBE_READ_REG(hw, IXGBE_PRC64);
1985         adapter->stats.prc127 += IXGBE_READ_REG(hw, IXGBE_PRC127);
1986         adapter->stats.prc255 += IXGBE_READ_REG(hw, IXGBE_PRC255);
1987         adapter->stats.prc511 += IXGBE_READ_REG(hw, IXGBE_PRC511);
1988         adapter->stats.prc1023 += IXGBE_READ_REG(hw, IXGBE_PRC1023);
1989         adapter->stats.prc1522 += IXGBE_READ_REG(hw, IXGBE_PRC1522);
1990
1991         adapter->stats.rlec += IXGBE_READ_REG(hw, IXGBE_RLEC);
1992         adapter->stats.lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
1993         adapter->stats.lxontxc += IXGBE_READ_REG(hw, IXGBE_LXONTXC);
1994         adapter->stats.lxoffrxc += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
1995         adapter->stats.lxofftxc += IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
1996         adapter->stats.ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
1997         adapter->stats.gptc += IXGBE_READ_REG(hw, IXGBE_GPTC);
1998         adapter->stats.gotc += IXGBE_READ_REG(hw, IXGBE_GOTCH);
1999         adapter->stats.rnbc[0] += IXGBE_READ_REG(hw, IXGBE_RNBC(0));
2000         adapter->stats.ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
2001         adapter->stats.rfc += IXGBE_READ_REG(hw, IXGBE_RFC);
2002         adapter->stats.rjc += IXGBE_READ_REG(hw, IXGBE_RJC);
2003         adapter->stats.tor += IXGBE_READ_REG(hw, IXGBE_TORH);
2004         adapter->stats.tpr += IXGBE_READ_REG(hw, IXGBE_TPR);
2005         adapter->stats.ptc64 += IXGBE_READ_REG(hw, IXGBE_PTC64);
2006         adapter->stats.ptc127 += IXGBE_READ_REG(hw, IXGBE_PTC127);
2007         adapter->stats.ptc255 += IXGBE_READ_REG(hw, IXGBE_PTC255);
2008         adapter->stats.ptc511 += IXGBE_READ_REG(hw, IXGBE_PTC511);
2009         adapter->stats.ptc1023 += IXGBE_READ_REG(hw, IXGBE_PTC1023);
2010         adapter->stats.ptc1522 += IXGBE_READ_REG(hw, IXGBE_PTC1522);
2011         adapter->stats.mptc += IXGBE_READ_REG(hw, IXGBE_MPTC);
2012         adapter->stats.bptc += IXGBE_READ_REG(hw, IXGBE_BPTC);
2013
2014         /* Fill out the OS statistics structure */
2015         adapter->net_stats.rx_packets = adapter->stats.gprc;
2016         adapter->net_stats.tx_packets = adapter->stats.gptc;
2017         adapter->net_stats.rx_bytes = adapter->stats.gorc;
2018         adapter->net_stats.tx_bytes = adapter->stats.gotc;
2019         adapter->net_stats.multicast = adapter->stats.mprc;
2020
2021         /* Rx Errors */
2022         adapter->net_stats.rx_errors = adapter->stats.crcerrs +
2023                                                 adapter->stats.rlec;
2024         adapter->net_stats.rx_dropped = 0;
2025         adapter->net_stats.rx_length_errors = adapter->stats.rlec;
2026         adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
2027         adapter->net_stats.rx_missed_errors = adapter->stats.mpc[0];
2028
2029 }
2030
2031 /**
2032  * ixgbe_watchdog - Timer Call-back
2033  * @data: pointer to adapter cast into an unsigned long
2034  **/
2035 static void ixgbe_watchdog(unsigned long data)
2036 {
2037         struct ixgbe_adapter *adapter = (struct ixgbe_adapter *)data;
2038         struct net_device *netdev = adapter->netdev;
2039         bool link_up;
2040         u32 link_speed = 0;
2041
2042         adapter->hw.phy.ops.check(&adapter->hw, &(link_speed), &link_up);
2043
2044         if (link_up) {
2045                 if (!netif_carrier_ok(netdev)) {
2046                         u32 frctl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
2047                         u32 rmcs = IXGBE_READ_REG(&adapter->hw, IXGBE_RMCS);
2048 #define FLOW_RX (frctl & IXGBE_FCTRL_RFCE)
2049 #define FLOW_TX (rmcs & IXGBE_RMCS_TFCE_802_3X)
2050                         DPRINTK(LINK, INFO, "NIC Link is Up %s, "
2051                                 "Flow Control: %s\n",
2052                                 (link_speed == IXGBE_LINK_SPEED_10GB_FULL ?
2053                                  "10 Gbps" :
2054                                  (link_speed == IXGBE_LINK_SPEED_1GB_FULL ?
2055                                   "1 Gpbs" : "unknown speed")),
2056                                 ((FLOW_RX && FLOW_TX) ? "RX/TX" :
2057                                  (FLOW_RX ? "RX" :
2058                                  (FLOW_TX ? "TX" : "None"))));
2059
2060                         netif_carrier_on(netdev);
2061                         netif_wake_queue(netdev);
2062                 } else {
2063                         /* Force detection of hung controller */
2064                         adapter->detect_tx_hung = true;
2065                 }
2066         } else {
2067                 if (netif_carrier_ok(netdev)) {
2068                         DPRINTK(LINK, INFO, "NIC Link is Down\n");
2069                         netif_carrier_off(netdev);
2070                         netif_stop_queue(netdev);
2071                 }
2072         }
2073
2074         ixgbe_update_stats(adapter);
2075
2076         /* Reset the timer */
2077         if (!test_bit(__IXGBE_DOWN, &adapter->state))
2078                 mod_timer(&adapter->watchdog_timer,
2079                           round_jiffies(jiffies + 2 * HZ));
2080 }
2081
2082 #define IXGBE_MAX_TXD_PWR       14
2083 #define IXGBE_MAX_DATA_PER_TXD  (1 << IXGBE_MAX_TXD_PWR)
2084
2085 /* Tx Descriptors needed, worst case */
2086 #define TXD_USE_COUNT(S) (((S) >> IXGBE_MAX_TXD_PWR) + \
2087                          (((S) & (IXGBE_MAX_DATA_PER_TXD - 1)) ? 1 : 0))
2088 #define DESC_NEEDED (TXD_USE_COUNT(IXGBE_MAX_DATA_PER_TXD) /* skb->data */ + \
2089         MAX_SKB_FRAGS * TXD_USE_COUNT(PAGE_SIZE) + 1)   /* for context */
2090
2091 static int ixgbe_tso(struct ixgbe_adapter *adapter,
2092                          struct ixgbe_ring *tx_ring, struct sk_buff *skb,
2093                          u32 tx_flags, u8 *hdr_len)
2094 {
2095         struct ixgbe_adv_tx_context_desc *context_desc;
2096         unsigned int i;
2097         int err;
2098         struct ixgbe_tx_buffer *tx_buffer_info;
2099         u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
2100         u32 mss_l4len_idx = 0, l4len;
2101         *hdr_len = 0;
2102
2103         if (skb_is_gso(skb)) {
2104                 if (skb_header_cloned(skb)) {
2105                         err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2106                         if (err)
2107                                 return err;
2108                 }
2109                 l4len = tcp_hdrlen(skb);
2110                 *hdr_len += l4len;
2111
2112                 if (skb->protocol == ntohs(ETH_P_IP)) {
2113                         struct iphdr *iph = ip_hdr(skb);
2114                         iph->tot_len = 0;
2115                         iph->check = 0;
2116                         tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2117                                                                  iph->daddr, 0,
2118                                                                  IPPROTO_TCP,
2119                                                                  0);
2120                         adapter->hw_tso_ctxt++;
2121                 } else if (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6) {
2122                         ipv6_hdr(skb)->payload_len = 0;
2123                         tcp_hdr(skb)->check =
2124                             ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2125                                              &ipv6_hdr(skb)->daddr,
2126                                              0, IPPROTO_TCP, 0);
2127                         adapter->hw_tso6_ctxt++;
2128                 }
2129
2130                 i = tx_ring->next_to_use;
2131
2132                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2133                 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
2134
2135                 /* VLAN MACLEN IPLEN */
2136                 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2137                         vlan_macip_lens |=
2138                             (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
2139                 vlan_macip_lens |= ((skb_network_offset(skb)) <<
2140                                     IXGBE_ADVTXD_MACLEN_SHIFT);
2141                 *hdr_len += skb_network_offset(skb);
2142                 vlan_macip_lens |=
2143                     (skb_transport_header(skb) - skb_network_header(skb));
2144                 *hdr_len +=
2145                     (skb_transport_header(skb) - skb_network_header(skb));
2146                 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2147                 context_desc->seqnum_seed = 0;
2148
2149                 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2150                 type_tucmd_mlhl |= (IXGBE_TXD_CMD_DEXT |
2151                                     IXGBE_ADVTXD_DTYP_CTXT);
2152
2153                 if (skb->protocol == ntohs(ETH_P_IP))
2154                         type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
2155                 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2156                 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
2157
2158                 /* MSS L4LEN IDX */
2159                 mss_l4len_idx |=
2160                     (skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT);
2161                 mss_l4len_idx |= (l4len << IXGBE_ADVTXD_L4LEN_SHIFT);
2162                 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2163
2164                 tx_buffer_info->time_stamp = jiffies;
2165                 tx_buffer_info->next_to_watch = i;
2166
2167                 i++;
2168                 if (i == tx_ring->count)
2169                         i = 0;
2170                 tx_ring->next_to_use = i;
2171
2172                 return true;
2173         }
2174         return false;
2175 }
2176
2177 static bool ixgbe_tx_csum(struct ixgbe_adapter *adapter,
2178                                    struct ixgbe_ring *tx_ring,
2179                                    struct sk_buff *skb, u32 tx_flags)
2180 {
2181         struct ixgbe_adv_tx_context_desc *context_desc;
2182         unsigned int i;
2183         struct ixgbe_tx_buffer *tx_buffer_info;
2184         u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
2185
2186         if (skb->ip_summed == CHECKSUM_PARTIAL ||
2187             (tx_flags & IXGBE_TX_FLAGS_VLAN)) {
2188                 i = tx_ring->next_to_use;
2189                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2190                 context_desc = IXGBE_TX_CTXTDESC_ADV(*tx_ring, i);
2191
2192                 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2193                         vlan_macip_lens |=
2194                             (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
2195                 vlan_macip_lens |= (skb_network_offset(skb) <<
2196                                     IXGBE_ADVTXD_MACLEN_SHIFT);
2197                 if (skb->ip_summed == CHECKSUM_PARTIAL)
2198                         vlan_macip_lens |= (skb_transport_header(skb) -
2199                                             skb_network_header(skb));
2200
2201                 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2202                 context_desc->seqnum_seed = 0;
2203
2204                 type_tucmd_mlhl |= (IXGBE_TXD_CMD_DEXT |
2205                                     IXGBE_ADVTXD_DTYP_CTXT);
2206
2207                 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2208                         if (skb->protocol == ntohs(ETH_P_IP))
2209                                 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
2210
2211                         if (skb->sk->sk_protocol == IPPROTO_TCP)
2212                                 type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2213                 }
2214
2215                 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd_mlhl);
2216                 context_desc->mss_l4len_idx = 0;
2217
2218                 tx_buffer_info->time_stamp = jiffies;
2219                 tx_buffer_info->next_to_watch = i;
2220                 adapter->hw_csum_tx_good++;
2221                 i++;
2222                 if (i == tx_ring->count)
2223                         i = 0;
2224                 tx_ring->next_to_use = i;
2225
2226                 return true;
2227         }
2228         return false;
2229 }
2230
2231 static int ixgbe_tx_map(struct ixgbe_adapter *adapter,
2232                         struct ixgbe_ring *tx_ring,
2233                         struct sk_buff *skb, unsigned int first)
2234 {
2235         struct ixgbe_tx_buffer *tx_buffer_info;
2236         unsigned int len = skb->len;
2237         unsigned int offset = 0, size, count = 0, i;
2238         unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2239         unsigned int f;
2240
2241         len -= skb->data_len;
2242
2243         i = tx_ring->next_to_use;
2244
2245         while (len) {
2246                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2247                 size = min(len, (uint)IXGBE_MAX_DATA_PER_TXD);
2248
2249                 tx_buffer_info->length = size;
2250                 tx_buffer_info->dma = pci_map_single(adapter->pdev,
2251                                                   skb->data + offset,
2252                                                   size, PCI_DMA_TODEVICE);
2253                 tx_buffer_info->time_stamp = jiffies;
2254                 tx_buffer_info->next_to_watch = i;
2255
2256                 len -= size;
2257                 offset += size;
2258                 count++;
2259                 i++;
2260                 if (i == tx_ring->count)
2261                         i = 0;
2262         }
2263
2264         for (f = 0; f < nr_frags; f++) {
2265                 struct skb_frag_struct *frag;
2266
2267                 frag = &skb_shinfo(skb)->frags[f];
2268                 len = frag->size;
2269                 offset = frag->page_offset;
2270
2271                 while (len) {
2272                         tx_buffer_info = &tx_ring->tx_buffer_info[i];
2273                         size = min(len, (uint)IXGBE_MAX_DATA_PER_TXD);
2274
2275                         tx_buffer_info->length = size;
2276                         tx_buffer_info->dma = pci_map_page(adapter->pdev,
2277                                                         frag->page,
2278                                                         offset,
2279                                                         size, PCI_DMA_TODEVICE);
2280                         tx_buffer_info->time_stamp = jiffies;
2281                         tx_buffer_info->next_to_watch = i;
2282
2283                         len -= size;
2284                         offset += size;
2285                         count++;
2286                         i++;
2287                         if (i == tx_ring->count)
2288                                 i = 0;
2289                 }
2290         }
2291         if (i == 0)
2292                 i = tx_ring->count - 1;
2293         else
2294                 i = i - 1;
2295         tx_ring->tx_buffer_info[i].skb = skb;
2296         tx_ring->tx_buffer_info[first].next_to_watch = i;
2297
2298         return count;
2299 }
2300
2301 static void ixgbe_tx_queue(struct ixgbe_adapter *adapter,
2302                                struct ixgbe_ring *tx_ring,
2303                                int tx_flags, int count, u32 paylen, u8 hdr_len)
2304 {
2305         union ixgbe_adv_tx_desc *tx_desc = NULL;
2306         struct ixgbe_tx_buffer *tx_buffer_info;
2307         u32 olinfo_status = 0, cmd_type_len = 0;
2308         unsigned int i;
2309         u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
2310
2311         cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
2312
2313         cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
2314
2315         if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2316                 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
2317
2318         if (tx_flags & IXGBE_TX_FLAGS_TSO) {
2319                 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
2320
2321                 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
2322                                                 IXGBE_ADVTXD_POPTS_SHIFT;
2323
2324                 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
2325                         olinfo_status |= IXGBE_TXD_POPTS_IXSM <<
2326                                                 IXGBE_ADVTXD_POPTS_SHIFT;
2327
2328         } else if (tx_flags & IXGBE_TX_FLAGS_CSUM)
2329                 olinfo_status |= IXGBE_TXD_POPTS_TXSM <<
2330                                                 IXGBE_ADVTXD_POPTS_SHIFT;
2331
2332         olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
2333
2334         i = tx_ring->next_to_use;
2335         while (count--) {
2336                 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2337                 tx_desc = IXGBE_TX_DESC_ADV(*tx_ring, i);
2338                 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
2339                 tx_desc->read.cmd_type_len =
2340                         cpu_to_le32(cmd_type_len | tx_buffer_info->length);
2341                 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2342
2343                 i++;
2344                 if (i == tx_ring->count)
2345                         i = 0;
2346         }
2347
2348         tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
2349
2350         /*
2351          * Force memory writes to complete before letting h/w
2352          * know there are new descriptors to fetch.  (Only
2353          * applicable for weak-ordered memory model archs,
2354          * such as IA-64).
2355          */
2356         wmb();
2357
2358         tx_ring->next_to_use = i;
2359         writel(i, adapter->hw.hw_addr + tx_ring->tail);
2360 }
2361
2362 static int ixgbe_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2363 {
2364         struct ixgbe_adapter *adapter = netdev_priv(netdev);
2365         struct ixgbe_ring *tx_ring;
2366         unsigned int len = skb->len;
2367         unsigned int first;
2368         unsigned int tx_flags = 0;
2369         unsigned long flags = 0;
2370         u8 hdr_len;
2371         int tso;
2372         unsigned int mss = 0;
2373         int count = 0;
2374         unsigned int f;
2375         unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2376         len -= skb->data_len;
2377
2378         tx_ring = adapter->tx_ring;
2379
2380         if (skb->len <= 0) {
2381                 dev_kfree_skb(skb);
2382                 return NETDEV_TX_OK;
2383         }
2384         mss = skb_shinfo(skb)->gso_size;
2385
2386         if (mss)
2387                 count++;
2388         else if (skb->ip_summed == CHECKSUM_PARTIAL)
2389                 count++;
2390
2391         count += TXD_USE_COUNT(len);
2392         for (f = 0; f < nr_frags; f++)
2393                 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
2394
2395         spin_lock_irqsave(&tx_ring->tx_lock, flags);
2396         if (IXGBE_DESC_UNUSED(tx_ring) < (count + 2)) {
2397                 adapter->tx_busy++;
2398                 netif_stop_queue(netdev);
2399                 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
2400                 return NETDEV_TX_BUSY;
2401         }
2402         spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
2403         if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
2404                 tx_flags |= IXGBE_TX_FLAGS_VLAN;
2405                 tx_flags |= (vlan_tx_tag_get(skb) << IXGBE_TX_FLAGS_VLAN_SHIFT);
2406         }
2407
2408         if (skb->protocol == ntohs(ETH_P_IP))
2409                 tx_flags |= IXGBE_TX_FLAGS_IPV4;
2410         first = tx_ring->next_to_use;
2411         tso = ixgbe_tso(adapter, tx_ring, skb, tx_flags, &hdr_len);
2412         if (tso < 0) {
2413                 dev_kfree_skb_any(skb);
2414                 return NETDEV_TX_OK;
2415         }
2416
2417         if (tso)
2418                 tx_flags |= IXGBE_TX_FLAGS_TSO;
2419         else if (ixgbe_tx_csum(adapter, tx_ring, skb, tx_flags) &&
2420                  (skb->ip_summed == CHECKSUM_PARTIAL))
2421                 tx_flags |= IXGBE_TX_FLAGS_CSUM;
2422
2423         ixgbe_tx_queue(adapter, tx_ring, tx_flags,
2424                            ixgbe_tx_map(adapter, tx_ring, skb, first),
2425                            skb->len, hdr_len);
2426
2427         netdev->trans_start = jiffies;
2428
2429         spin_lock_irqsave(&tx_ring->tx_lock, flags);
2430         /* Make sure there is space in the ring for the next send. */
2431         if (IXGBE_DESC_UNUSED(tx_ring) < DESC_NEEDED)
2432                 netif_stop_queue(netdev);
2433         spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
2434
2435         return NETDEV_TX_OK;
2436 }
2437
2438 /**
2439  * ixgbe_get_stats - Get System Network Statistics
2440  * @netdev: network interface device structure
2441  *
2442  * Returns the address of the device statistics structure.
2443  * The statistics are actually updated from the timer callback.
2444  **/
2445 static struct net_device_stats *ixgbe_get_stats(struct net_device *netdev)
2446 {
2447         struct ixgbe_adapter *adapter = netdev_priv(netdev);
2448
2449         /* only return the current stats */
2450         return &adapter->net_stats;
2451 }
2452
2453 /**
2454  * ixgbe_set_mac - Change the Ethernet Address of the NIC
2455  * @netdev: network interface device structure
2456  * @p: pointer to an address structure
2457  *
2458  * Returns 0 on success, negative on failure
2459  **/
2460 static int ixgbe_set_mac(struct net_device *netdev, void *p)
2461 {
2462         struct ixgbe_adapter *adapter = netdev_priv(netdev);
2463         struct sockaddr *addr = p;
2464
2465         if (!is_valid_ether_addr(addr->sa_data))
2466                 return -EADDRNOTAVAIL;
2467
2468         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2469         memcpy(adapter->hw.mac.addr, addr->sa_data, netdev->addr_len);
2470
2471         ixgbe_set_rar(&adapter->hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
2472
2473         return 0;
2474 }
2475
2476 #ifdef CONFIG_NET_POLL_CONTROLLER
2477 /*
2478  * Polling 'interrupt' - used by things like netconsole to send skbs
2479  * without having to re-enable interrupts. It's not called while
2480  * the interrupt routine is executing.
2481  */
2482 static void ixgbe_netpoll(struct net_device *netdev)
2483 {
2484         struct ixgbe_adapter *adapter = netdev_priv(netdev);
2485
2486         disable_irq(adapter->pdev->irq);
2487         adapter->flags |= IXGBE_FLAG_IN_NETPOLL;
2488         ixgbe_intr(adapter->pdev->irq, netdev);
2489         adapter->flags &= ~IXGBE_FLAG_IN_NETPOLL;
2490         enable_irq(adapter->pdev->irq);
2491 }
2492 #endif
2493
2494 /**
2495  * ixgbe_probe - Device Initialization Routine
2496  * @pdev: PCI device information struct
2497  * @ent: entry in ixgbe_pci_tbl
2498  *
2499  * Returns 0 on success, negative on failure
2500  *
2501  * ixgbe_probe initializes an adapter identified by a pci_dev structure.
2502  * The OS initialization, configuring of the adapter private structure,
2503  * and a hardware reset occur.
2504  **/
2505 static int __devinit ixgbe_probe(struct pci_dev *pdev,
2506                                  const struct pci_device_id *ent)
2507 {
2508         struct net_device *netdev;
2509         struct ixgbe_adapter *adapter = NULL;
2510         struct ixgbe_hw *hw;
2511         const struct ixgbe_info *ii = ixgbe_info_tbl[ent->driver_data];
2512         unsigned long mmio_start, mmio_len;
2513         static int cards_found;
2514         int i, err, pci_using_dac;
2515         u16 link_status, link_speed, link_width;
2516         u32 part_num;
2517
2518         err = pci_enable_device(pdev);
2519         if (err)
2520                 return err;
2521
2522         if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK) &&
2523             !pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK)) {
2524                 pci_using_dac = 1;
2525         } else {
2526                 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
2527                 if (err) {
2528                         err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
2529                         if (err) {
2530                                 dev_err(&pdev->dev, "No usable DMA "
2531                                         "configuration, aborting\n");
2532                                 goto err_dma;
2533                         }
2534                 }
2535                 pci_using_dac = 0;
2536         }
2537
2538         err = pci_request_regions(pdev, ixgbe_driver_name);
2539         if (err) {
2540                 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
2541                 goto err_pci_reg;
2542         }
2543
2544         pci_set_master(pdev);
2545
2546         netdev = alloc_etherdev(sizeof(struct ixgbe_adapter));
2547         if (!netdev) {
2548                 err = -ENOMEM;
2549                 goto err_alloc_etherdev;
2550         }
2551
2552         SET_NETDEV_DEV(netdev, &pdev->dev);
2553
2554         pci_set_drvdata(pdev, netdev);
2555         adapter = netdev_priv(netdev);
2556
2557         adapter->netdev = netdev;
2558         adapter->pdev = pdev;
2559         hw = &adapter->hw;
2560         hw->back = adapter;
2561         adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
2562
2563         mmio_start = pci_resource_start(pdev, 0);
2564         mmio_len = pci_resource_len(pdev, 0);
2565
2566         hw->hw_addr = ioremap(mmio_start, mmio_len);
2567         if (!hw->hw_addr) {
2568                 err = -EIO;
2569                 goto err_ioremap;
2570         }
2571
2572         for (i = 1; i <= 5; i++) {
2573                 if (pci_resource_len(pdev, i) == 0)
2574                         continue;
2575         }
2576
2577         netdev->open = &ixgbe_open;
2578         netdev->stop = &ixgbe_close;
2579         netdev->hard_start_xmit = &ixgbe_xmit_frame;
2580         netdev->get_stats = &ixgbe_get_stats;
2581         netdev->set_multicast_list = &ixgbe_set_multi;
2582         netdev->set_mac_address = &ixgbe_set_mac;
2583         netdev->change_mtu = &ixgbe_change_mtu;
2584         ixgbe_set_ethtool_ops(netdev);
2585         netdev->tx_timeout = &ixgbe_tx_timeout;
2586         netdev->watchdog_timeo = 5 * HZ;
2587         netif_napi_add(netdev, &adapter->napi, ixgbe_clean, 64);
2588         netdev->vlan_rx_register = ixgbe_vlan_rx_register;
2589         netdev->vlan_rx_add_vid = ixgbe_vlan_rx_add_vid;
2590         netdev->vlan_rx_kill_vid = ixgbe_vlan_rx_kill_vid;
2591 #ifdef CONFIG_NET_POLL_CONTROLLER
2592         netdev->poll_controller = ixgbe_netpoll;
2593 #endif
2594         strcpy(netdev->name, pci_name(pdev));
2595
2596         netdev->mem_start = mmio_start;
2597         netdev->mem_end = mmio_start + mmio_len;
2598
2599         adapter->bd_number = cards_found;
2600
2601         /* PCI config space info */
2602         hw->vendor_id = pdev->vendor;
2603         hw->device_id = pdev->device;
2604         hw->revision_id = pdev->revision;
2605         hw->subsystem_vendor_id = pdev->subsystem_vendor;
2606         hw->subsystem_device_id = pdev->subsystem_device;
2607
2608         /* Setup hw api */
2609         memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
2610         memcpy(&hw->phy.ops, ii->phy_ops, sizeof(hw->phy.ops));
2611
2612         err = ii->get_invariants(hw);
2613         if (err)
2614                 goto err_hw_init;
2615
2616         /* setup the private structure */
2617         err = ixgbe_sw_init(adapter);
2618         if (err)
2619                 goto err_sw_init;
2620
2621         netdev->features = NETIF_F_SG |
2622                            NETIF_F_HW_CSUM |
2623                            NETIF_F_HW_VLAN_TX |
2624                            NETIF_F_HW_VLAN_RX |
2625                            NETIF_F_HW_VLAN_FILTER;
2626
2627         netdev->features |= NETIF_F_TSO;
2628
2629         netdev->features |= NETIF_F_TSO6;
2630         if (pci_using_dac)
2631                 netdev->features |= NETIF_F_HIGHDMA;
2632
2633
2634         /* make sure the EEPROM is good */
2635         if (ixgbe_validate_eeprom_checksum(hw, NULL) < 0) {
2636                 dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n");
2637                 err = -EIO;
2638                 goto err_eeprom;
2639         }
2640
2641         memcpy(netdev->dev_addr, hw->mac.perm_addr, netdev->addr_len);
2642         memcpy(netdev->perm_addr, hw->mac.perm_addr, netdev->addr_len);
2643
2644         if (ixgbe_validate_mac_addr(netdev->dev_addr)) {
2645                 err = -EIO;
2646                 goto err_eeprom;
2647         }
2648
2649         init_timer(&adapter->watchdog_timer);
2650         adapter->watchdog_timer.function = &ixgbe_watchdog;
2651         adapter->watchdog_timer.data = (unsigned long)adapter;
2652
2653         INIT_WORK(&adapter->reset_task, ixgbe_reset_task);
2654
2655         /* initialize default flow control settings */
2656         hw->fc.original_type = ixgbe_fc_full;
2657         hw->fc.type = ixgbe_fc_full;
2658         hw->fc.high_water = IXGBE_DEFAULT_FCRTH;
2659         hw->fc.low_water = IXGBE_DEFAULT_FCRTL;
2660         hw->fc.pause_time = IXGBE_DEFAULT_FCPAUSE;
2661
2662         /* Interrupt Throttle Rate */
2663         adapter->rx_eitr = (1000000 / IXGBE_DEFAULT_ITR_RX_USECS);
2664         adapter->tx_eitr = (1000000 / IXGBE_DEFAULT_ITR_TX_USECS);
2665
2666         /* print bus type/speed/width info */
2667         pci_read_config_word(pdev, IXGBE_PCI_LINK_STATUS, &link_status);
2668         link_speed = link_status & IXGBE_PCI_LINK_SPEED;
2669         link_width = link_status & IXGBE_PCI_LINK_WIDTH;
2670         dev_info(&pdev->dev, "(PCI Express:%s:%s) "
2671                  "%02x:%02x:%02x:%02x:%02x:%02x\n",
2672                 ((link_speed == IXGBE_PCI_LINK_SPEED_5000) ? "5.0Gb/s" :
2673                  (link_speed == IXGBE_PCI_LINK_SPEED_2500) ? "2.5Gb/s" :
2674                  "Unknown"),
2675                 ((link_width == IXGBE_PCI_LINK_WIDTH_8) ? "Width x8" :
2676                  (link_width == IXGBE_PCI_LINK_WIDTH_4) ? "Width x4" :
2677                  (link_width == IXGBE_PCI_LINK_WIDTH_2) ? "Width x2" :
2678                  (link_width == IXGBE_PCI_LINK_WIDTH_1) ? "Width x1" :
2679                  "Unknown"),
2680                 netdev->dev_addr[0], netdev->dev_addr[1], netdev->dev_addr[2],
2681                 netdev->dev_addr[3], netdev->dev_addr[4], netdev->dev_addr[5]);
2682         ixgbe_read_part_num(hw, &part_num);
2683         dev_info(&pdev->dev, "MAC: %d, PHY: %d, PBA No: %06x-%03x\n",
2684                  hw->mac.type, hw->phy.type,
2685                  (part_num >> 8), (part_num & 0xff));
2686
2687         /* reset the hardware with the new settings */
2688         ixgbe_start_hw(hw);
2689
2690         netif_carrier_off(netdev);
2691         netif_stop_queue(netdev);
2692
2693         strcpy(netdev->name, "eth%d");
2694         err = register_netdev(netdev);
2695         if (err)
2696                 goto err_register;
2697
2698
2699         dev_info(&pdev->dev, "Intel(R) 10 Gigabit Network Connection\n");
2700         cards_found++;
2701         return 0;
2702
2703 err_register:
2704 err_hw_init:
2705 err_sw_init:
2706 err_eeprom:
2707         iounmap(hw->hw_addr);
2708 err_ioremap:
2709         free_netdev(netdev);
2710 err_alloc_etherdev:
2711         pci_release_regions(pdev);
2712 err_pci_reg:
2713 err_dma:
2714         pci_disable_device(pdev);
2715         return err;
2716 }
2717
2718 /**
2719  * ixgbe_remove - Device Removal Routine
2720  * @pdev: PCI device information struct
2721  *
2722  * ixgbe_remove is called by the PCI subsystem to alert the driver
2723  * that it should release a PCI device.  The could be caused by a
2724  * Hot-Plug event, or because the driver is going to be removed from
2725  * memory.
2726  **/
2727 static void __devexit ixgbe_remove(struct pci_dev *pdev)
2728 {
2729         struct net_device *netdev = pci_get_drvdata(pdev);
2730         struct ixgbe_adapter *adapter = netdev_priv(netdev);
2731
2732         set_bit(__IXGBE_DOWN, &adapter->state);
2733         del_timer_sync(&adapter->watchdog_timer);
2734
2735         flush_scheduled_work();
2736
2737         unregister_netdev(netdev);
2738
2739         kfree(adapter->tx_ring);
2740         kfree(adapter->rx_ring);
2741
2742         iounmap(adapter->hw.hw_addr);
2743         pci_release_regions(pdev);
2744
2745         free_netdev(netdev);
2746
2747         pci_disable_device(pdev);
2748 }
2749
2750 /**
2751  * ixgbe_io_error_detected - called when PCI error is detected
2752  * @pdev: Pointer to PCI device
2753  * @state: The current pci connection state
2754  *
2755  * This function is called after a PCI bus error affecting
2756  * this device has been detected.
2757  */
2758 static pci_ers_result_t ixgbe_io_error_detected(struct pci_dev *pdev,
2759                                                 pci_channel_state_t state)
2760 {
2761         struct net_device *netdev = pci_get_drvdata(pdev);
2762         struct ixgbe_adapter *adapter = netdev->priv;
2763
2764         netif_device_detach(netdev);
2765
2766         if (netif_running(netdev))
2767                 ixgbe_down(adapter);
2768         pci_disable_device(pdev);
2769
2770         /* Request a slot slot reset. */
2771         return PCI_ERS_RESULT_NEED_RESET;
2772 }
2773
2774 /**
2775  * ixgbe_io_slot_reset - called after the pci bus has been reset.
2776  * @pdev: Pointer to PCI device
2777  *
2778  * Restart the card from scratch, as if from a cold-boot.
2779  */
2780 static pci_ers_result_t ixgbe_io_slot_reset(struct pci_dev *pdev)
2781 {
2782         struct net_device *netdev = pci_get_drvdata(pdev);
2783         struct ixgbe_adapter *adapter = netdev->priv;
2784
2785         if (pci_enable_device(pdev)) {
2786                 DPRINTK(PROBE, ERR,
2787                         "Cannot re-enable PCI device after reset.\n");
2788                 return PCI_ERS_RESULT_DISCONNECT;
2789         }
2790         pci_set_master(pdev);
2791
2792         pci_enable_wake(pdev, PCI_D3hot, 0);
2793         pci_enable_wake(pdev, PCI_D3cold, 0);
2794
2795         ixgbe_reset(adapter);
2796
2797         return PCI_ERS_RESULT_RECOVERED;
2798 }
2799
2800 /**
2801  * ixgbe_io_resume - called when traffic can start flowing again.
2802  * @pdev: Pointer to PCI device
2803  *
2804  * This callback is called when the error recovery driver tells us that
2805  * its OK to resume normal operation.
2806  */
2807 static void ixgbe_io_resume(struct pci_dev *pdev)
2808 {
2809         struct net_device *netdev = pci_get_drvdata(pdev);
2810         struct ixgbe_adapter *adapter = netdev->priv;
2811
2812         if (netif_running(netdev)) {
2813                 if (ixgbe_up(adapter)) {
2814                         DPRINTK(PROBE, INFO, "ixgbe_up failed after reset\n");
2815                         return;
2816                 }
2817         }
2818
2819         netif_device_attach(netdev);
2820
2821 }
2822
2823 static struct pci_error_handlers ixgbe_err_handler = {
2824         .error_detected = ixgbe_io_error_detected,
2825         .slot_reset = ixgbe_io_slot_reset,
2826         .resume = ixgbe_io_resume,
2827 };
2828
2829 static struct pci_driver ixgbe_driver = {
2830         .name     = ixgbe_driver_name,
2831         .id_table = ixgbe_pci_tbl,
2832         .probe    = ixgbe_probe,
2833         .remove   = __devexit_p(ixgbe_remove),
2834 #ifdef CONFIG_PM
2835         .suspend  = ixgbe_suspend,
2836         .resume   = ixgbe_resume,
2837 #endif
2838         .shutdown = ixgbe_shutdown,
2839         .err_handler = &ixgbe_err_handler
2840 };
2841
2842 /**
2843  * ixgbe_init_module - Driver Registration Routine
2844  *
2845  * ixgbe_init_module is the first routine called when the driver is
2846  * loaded. All it does is register with the PCI subsystem.
2847  **/
2848 static int __init ixgbe_init_module(void)
2849 {
2850         int ret;
2851         printk(KERN_INFO "%s: %s - version %s\n", ixgbe_driver_name,
2852                ixgbe_driver_string, ixgbe_driver_version);
2853
2854         printk(KERN_INFO "%s: %s\n", ixgbe_driver_name, ixgbe_copyright);
2855
2856         ret = pci_register_driver(&ixgbe_driver);
2857         return ret;
2858 }
2859 module_init(ixgbe_init_module);
2860
2861 /**
2862  * ixgbe_exit_module - Driver Exit Cleanup Routine
2863  *
2864  * ixgbe_exit_module is called just before the driver is removed
2865  * from memory.
2866  **/
2867 static void __exit ixgbe_exit_module(void)
2868 {
2869         pci_unregister_driver(&ixgbe_driver);
2870 }
2871 module_exit(ixgbe_exit_module);
2872
2873 /* ixgbe_main.c */