firewire: Implement ioctl to initiate bus reset.
[linux-2.6] / drivers / net / atl1 / atl1_main.c
1 /*
2  * Copyright(c) 2005 - 2006 Attansic Corporation. All rights reserved.
3  * Copyright(c) 2006 Chris Snook <csnook@redhat.com>
4  * Copyright(c) 2006 Jay Cliburn <jcliburn@gmail.com>
5  *
6  * Derived from Intel e1000 driver
7  * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the Free
11  * Software Foundation; either version 2 of the License, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program; if not, write to the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The full GNU General Public License is included in this distribution in the
24  * file called COPYING.
25  *
26  * Contact Information:
27  * Xiong Huang <xiong_huang@attansic.com>
28  * Attansic Technology Corp. 3F 147, Xianzheng 9th Road, Zhubei,
29  * Xinzhu  302, TAIWAN, REPUBLIC OF CHINA
30  *
31  * Chris Snook <csnook@redhat.com>
32  * Jay Cliburn <jcliburn@gmail.com>
33  *
34  * This version is adapted from the Attansic reference driver for
35  * inclusion in the Linux kernel.  It is currently under heavy development.
36  * A very incomplete list of things that need to be dealt with:
37  *
38  * TODO:
39  * Fix TSO; tx performance is horrible with TSO enabled.
40  * Wake on LAN.
41  * Add more ethtool functions, including set ring parameters.
42  * Fix abstruse irq enable/disable condition described here:
43  *      http://marc.theaimsgroup.com/?l=linux-netdev&m=116398508500553&w=2
44  *
45  * NEEDS TESTING:
46  * VLAN
47  * multicast
48  * promiscuous mode
49  * interrupt coalescing
50  * SMP torture testing
51  */
52
53 #include <linux/types.h>
54 #include <linux/netdevice.h>
55 #include <linux/pci.h>
56 #include <linux/spinlock.h>
57 #include <linux/slab.h>
58 #include <linux/string.h>
59 #include <linux/skbuff.h>
60 #include <linux/etherdevice.h>
61 #include <linux/if_vlan.h>
62 #include <linux/irqreturn.h>
63 #include <linux/workqueue.h>
64 #include <linux/timer.h>
65 #include <linux/jiffies.h>
66 #include <linux/hardirq.h>
67 #include <linux/interrupt.h>
68 #include <linux/irqflags.h>
69 #include <linux/dma-mapping.h>
70 #include <linux/net.h>
71 #include <linux/pm.h>
72 #include <linux/in.h>
73 #include <linux/ip.h>
74 #include <linux/tcp.h>
75 #include <linux/compiler.h>
76 #include <linux/delay.h>
77 #include <linux/mii.h>
78 #include <net/checksum.h>
79
80 #include <asm/atomic.h>
81 #include <asm/byteorder.h>
82
83 #include "atl1.h"
84
85 #define DRIVER_VERSION "2.0.7"
86
87 char atl1_driver_name[] = "atl1";
88 static const char atl1_driver_string[] = "Attansic L1 Ethernet Network Driver";
89 static const char atl1_copyright[] = "Copyright(c) 2005-2006 Attansic Corporation.";
90 char atl1_driver_version[] = DRIVER_VERSION;
91
92 MODULE_AUTHOR
93     ("Attansic Corporation <xiong_huang@attansic.com>, Chris Snook <csnook@redhat.com>, Jay Cliburn <jcliburn@gmail.com>");
94 MODULE_DESCRIPTION("Attansic 1000M Ethernet Network Driver");
95 MODULE_LICENSE("GPL");
96 MODULE_VERSION(DRIVER_VERSION);
97
98 /*
99  * atl1_pci_tbl - PCI Device ID Table
100  */
101 static const struct pci_device_id atl1_pci_tbl[] = {
102         {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L1)},
103         /* required last entry */
104         {0,}
105 };
106
107 MODULE_DEVICE_TABLE(pci, atl1_pci_tbl);
108
109 /*
110  * atl1_sw_init - Initialize general software structures (struct atl1_adapter)
111  * @adapter: board private structure to initialize
112  *
113  * atl1_sw_init initializes the Adapter private data structure.
114  * Fields are initialized based on PCI device information and
115  * OS network device settings (MTU size).
116  */
117 static int __devinit atl1_sw_init(struct atl1_adapter *adapter)
118 {
119         struct atl1_hw *hw = &adapter->hw;
120         struct net_device *netdev = adapter->netdev;
121         struct pci_dev *pdev = adapter->pdev;
122
123         /* PCI config space info */
124         pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
125
126         hw->max_frame_size = netdev->mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
127         hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
128
129         adapter->wol = 0;
130         adapter->rx_buffer_len = (hw->max_frame_size + 7) & ~7;
131         adapter->ict = 50000;   /* 100ms */
132         adapter->link_speed = SPEED_0;  /* hardware init */
133         adapter->link_duplex = FULL_DUPLEX;
134
135         hw->phy_configured = false;
136         hw->preamble_len = 7;
137         hw->ipgt = 0x60;
138         hw->min_ifg = 0x50;
139         hw->ipgr1 = 0x40;
140         hw->ipgr2 = 0x60;
141         hw->max_retry = 0xf;
142         hw->lcol = 0x37;
143         hw->jam_ipg = 7;
144         hw->rfd_burst = 8;
145         hw->rrd_burst = 8;
146         hw->rfd_fetch_gap = 1;
147         hw->rx_jumbo_th = adapter->rx_buffer_len / 8;
148         hw->rx_jumbo_lkah = 1;
149         hw->rrd_ret_timer = 16;
150         hw->tpd_burst = 4;
151         hw->tpd_fetch_th = 16;
152         hw->txf_burst = 0x100;
153         hw->tx_jumbo_task_th = (hw->max_frame_size + 7) >> 3;
154         hw->tpd_fetch_gap = 1;
155         hw->rcb_value = atl1_rcb_64;
156         hw->dma_ord = atl1_dma_ord_enh;
157         hw->dmar_block = atl1_dma_req_256;
158         hw->dmaw_block = atl1_dma_req_256;
159         hw->cmb_rrd = 4;
160         hw->cmb_tpd = 4;
161         hw->cmb_rx_timer = 1;   /* about 2us */
162         hw->cmb_tx_timer = 1;   /* about 2us */
163         hw->smb_timer = 100000; /* about 200ms */
164
165         atomic_set(&adapter->irq_sem, 0);
166         spin_lock_init(&adapter->lock);
167         spin_lock_init(&adapter->mb_lock);
168
169         return 0;
170 }
171
172 /*
173  * atl1_setup_mem_resources - allocate Tx / RX descriptor resources
174  * @adapter: board private structure
175  *
176  * Return 0 on success, negative on failure
177  */
178 s32 atl1_setup_ring_resources(struct atl1_adapter *adapter)
179 {
180         struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
181         struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
182         struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
183         struct atl1_ring_header *ring_header = &adapter->ring_header;
184         struct pci_dev *pdev = adapter->pdev;
185         int size;
186         u8 offset = 0;
187
188         size = sizeof(struct atl1_buffer) * (tpd_ring->count + rfd_ring->count);
189         tpd_ring->buffer_info = kzalloc(size, GFP_KERNEL);
190         if (unlikely(!tpd_ring->buffer_info)) {
191                 printk(KERN_WARNING "%s: kzalloc failed , size = D%d\n",
192                         atl1_driver_name, size);
193                 goto err_nomem;
194         }
195         rfd_ring->buffer_info =
196             (struct atl1_buffer *)(tpd_ring->buffer_info + tpd_ring->count);
197
198         /* real ring DMA buffer */
199         ring_header->size = size = sizeof(struct tx_packet_desc) *
200                                         tpd_ring->count
201             + sizeof(struct rx_free_desc) * rfd_ring->count
202             + sizeof(struct rx_return_desc) * rrd_ring->count
203             + sizeof(struct coals_msg_block)
204             + sizeof(struct stats_msg_block)
205             + 40;               /* "40: for 8 bytes align" huh? -- CHS */
206
207         ring_header->desc = pci_alloc_consistent(pdev, ring_header->size,
208                                                 &ring_header->dma);
209         if (unlikely(!ring_header->desc)) {
210                 printk(KERN_WARNING
211                         "%s: pci_alloc_consistent failed, size = D%d\n",
212                         atl1_driver_name, size);
213                 goto err_nomem;
214         }
215
216         memset(ring_header->desc, 0, ring_header->size);
217
218         /* init TPD ring */
219         tpd_ring->dma = ring_header->dma;
220         offset = (tpd_ring->dma & 0x7) ? (8 - (ring_header->dma & 0x7)) : 0;
221         tpd_ring->dma += offset;
222         tpd_ring->desc = (u8 *) ring_header->desc + offset;
223         tpd_ring->size = sizeof(struct tx_packet_desc) * tpd_ring->count;
224         atomic_set(&tpd_ring->next_to_use, 0);
225         atomic_set(&tpd_ring->next_to_clean, 0);
226
227         /* init RFD ring */
228         rfd_ring->dma = tpd_ring->dma + tpd_ring->size;
229         offset = (rfd_ring->dma & 0x7) ? (8 - (rfd_ring->dma & 0x7)) : 0;
230         rfd_ring->dma += offset;
231         rfd_ring->desc = (u8 *) tpd_ring->desc + (tpd_ring->size + offset);
232         rfd_ring->size = sizeof(struct rx_free_desc) * rfd_ring->count;
233         rfd_ring->next_to_clean = 0;
234         /* rfd_ring->next_to_use = rfd_ring->count - 1; */
235         atomic_set(&rfd_ring->next_to_use, 0);
236
237         /* init RRD ring */
238         rrd_ring->dma = rfd_ring->dma + rfd_ring->size;
239         offset = (rrd_ring->dma & 0x7) ? (8 - (rrd_ring->dma & 0x7)) : 0;
240         rrd_ring->dma += offset;
241         rrd_ring->desc = (u8 *) rfd_ring->desc + (rfd_ring->size + offset);
242         rrd_ring->size = sizeof(struct rx_return_desc) * rrd_ring->count;
243         rrd_ring->next_to_use = 0;
244         atomic_set(&rrd_ring->next_to_clean, 0);
245
246         /* init CMB */
247         adapter->cmb.dma = rrd_ring->dma + rrd_ring->size;
248         offset = (adapter->cmb.dma & 0x7) ? (8 - (adapter->cmb.dma & 0x7)) : 0;
249         adapter->cmb.dma += offset;
250         adapter->cmb.cmb =
251             (struct coals_msg_block *) ((u8 *) rrd_ring->desc +
252                                    (rrd_ring->size + offset));
253
254         /* init SMB */
255         adapter->smb.dma = adapter->cmb.dma + sizeof(struct coals_msg_block);
256         offset = (adapter->smb.dma & 0x7) ? (8 - (adapter->smb.dma & 0x7)) : 0;
257         adapter->smb.dma += offset;
258         adapter->smb.smb = (struct stats_msg_block *)
259             ((u8 *) adapter->cmb.cmb + (sizeof(struct coals_msg_block) + offset));
260
261         return ATL1_SUCCESS;
262
263 err_nomem:
264         kfree(tpd_ring->buffer_info);
265         return -ENOMEM;
266 }
267
268 /*
269  * atl1_irq_enable - Enable default interrupt generation settings
270  * @adapter: board private structure
271  */
272 static void atl1_irq_enable(struct atl1_adapter *adapter)
273 {
274         if (likely(!atomic_dec_and_test(&adapter->irq_sem)))
275                 iowrite32(IMR_NORMAL_MASK, adapter->hw.hw_addr + REG_IMR);
276 }
277
278 static void atl1_clear_phy_int(struct atl1_adapter *adapter)
279 {
280         u16 phy_data;
281         unsigned long flags;
282
283         spin_lock_irqsave(&adapter->lock, flags);
284         atl1_read_phy_reg(&adapter->hw, 19, &phy_data);
285         spin_unlock_irqrestore(&adapter->lock, flags);
286 }
287
288 static void atl1_inc_smb(struct atl1_adapter *adapter)
289 {
290         struct stats_msg_block *smb = adapter->smb.smb;
291
292         /* Fill out the OS statistics structure */
293         adapter->soft_stats.rx_packets += smb->rx_ok;
294         adapter->soft_stats.tx_packets += smb->tx_ok;
295         adapter->soft_stats.rx_bytes += smb->rx_byte_cnt;
296         adapter->soft_stats.tx_bytes += smb->tx_byte_cnt;
297         adapter->soft_stats.multicast += smb->rx_mcast;
298         adapter->soft_stats.collisions += (smb->tx_1_col +
299                                            smb->tx_2_col * 2 +
300                                            smb->tx_late_col +
301                                            smb->tx_abort_col *
302                                            adapter->hw.max_retry);
303
304         /* Rx Errors */
305         adapter->soft_stats.rx_errors += (smb->rx_frag +
306                                           smb->rx_fcs_err +
307                                           smb->rx_len_err +
308                                           smb->rx_sz_ov +
309                                           smb->rx_rxf_ov +
310                                           smb->rx_rrd_ov + smb->rx_align_err);
311         adapter->soft_stats.rx_fifo_errors += smb->rx_rxf_ov;
312         adapter->soft_stats.rx_length_errors += smb->rx_len_err;
313         adapter->soft_stats.rx_crc_errors += smb->rx_fcs_err;
314         adapter->soft_stats.rx_frame_errors += smb->rx_align_err;
315         adapter->soft_stats.rx_missed_errors += (smb->rx_rrd_ov +
316                                                  smb->rx_rxf_ov);
317
318         adapter->soft_stats.rx_pause += smb->rx_pause;
319         adapter->soft_stats.rx_rrd_ov += smb->rx_rrd_ov;
320         adapter->soft_stats.rx_trunc += smb->rx_sz_ov;
321
322         /* Tx Errors */
323         adapter->soft_stats.tx_errors += (smb->tx_late_col +
324                                           smb->tx_abort_col +
325                                           smb->tx_underrun + smb->tx_trunc);
326         adapter->soft_stats.tx_fifo_errors += smb->tx_underrun;
327         adapter->soft_stats.tx_aborted_errors += smb->tx_abort_col;
328         adapter->soft_stats.tx_window_errors += smb->tx_late_col;
329
330         adapter->soft_stats.excecol += smb->tx_abort_col;
331         adapter->soft_stats.deffer += smb->tx_defer;
332         adapter->soft_stats.scc += smb->tx_1_col;
333         adapter->soft_stats.mcc += smb->tx_2_col;
334         adapter->soft_stats.latecol += smb->tx_late_col;
335         adapter->soft_stats.tx_underun += smb->tx_underrun;
336         adapter->soft_stats.tx_trunc += smb->tx_trunc;
337         adapter->soft_stats.tx_pause += smb->tx_pause;
338
339         adapter->net_stats.rx_packets = adapter->soft_stats.rx_packets;
340         adapter->net_stats.tx_packets = adapter->soft_stats.tx_packets;
341         adapter->net_stats.rx_bytes = adapter->soft_stats.rx_bytes;
342         adapter->net_stats.tx_bytes = adapter->soft_stats.tx_bytes;
343         adapter->net_stats.multicast = adapter->soft_stats.multicast;
344         adapter->net_stats.collisions = adapter->soft_stats.collisions;
345         adapter->net_stats.rx_errors = adapter->soft_stats.rx_errors;
346         adapter->net_stats.rx_over_errors =
347             adapter->soft_stats.rx_missed_errors;
348         adapter->net_stats.rx_length_errors =
349             adapter->soft_stats.rx_length_errors;
350         adapter->net_stats.rx_crc_errors = adapter->soft_stats.rx_crc_errors;
351         adapter->net_stats.rx_frame_errors =
352             adapter->soft_stats.rx_frame_errors;
353         adapter->net_stats.rx_fifo_errors = adapter->soft_stats.rx_fifo_errors;
354         adapter->net_stats.rx_missed_errors =
355             adapter->soft_stats.rx_missed_errors;
356         adapter->net_stats.tx_errors = adapter->soft_stats.tx_errors;
357         adapter->net_stats.tx_fifo_errors = adapter->soft_stats.tx_fifo_errors;
358         adapter->net_stats.tx_aborted_errors =
359             adapter->soft_stats.tx_aborted_errors;
360         adapter->net_stats.tx_window_errors =
361             adapter->soft_stats.tx_window_errors;
362         adapter->net_stats.tx_carrier_errors =
363             adapter->soft_stats.tx_carrier_errors;
364 }
365
366 static void atl1_rx_checksum(struct atl1_adapter *adapter,
367                                         struct rx_return_desc *rrd,
368                                         struct sk_buff *skb)
369 {
370         skb->ip_summed = CHECKSUM_NONE;
371
372         if (unlikely(rrd->pkt_flg & PACKET_FLAG_ERR)) {
373                 if (rrd->err_flg & (ERR_FLAG_CRC | ERR_FLAG_TRUNC |
374                                         ERR_FLAG_CODE | ERR_FLAG_OV)) {
375                         adapter->hw_csum_err++;
376                         printk(KERN_DEBUG "%s: rx checksum error\n",
377                                 atl1_driver_name);
378                         return;
379                 }
380         }
381
382         /* not IPv4 */
383         if (!(rrd->pkt_flg & PACKET_FLAG_IPV4))
384                 /* checksum is invalid, but it's not an IPv4 pkt, so ok */
385                 return;
386
387         /* IPv4 packet */
388         if (likely(!(rrd->err_flg &
389                 (ERR_FLAG_IP_CHKSUM | ERR_FLAG_L4_CHKSUM)))) {
390                 skb->ip_summed = CHECKSUM_UNNECESSARY;
391                 adapter->hw_csum_good++;
392                 return;
393         }
394
395         /* IPv4, but hardware thinks its checksum is wrong */
396         printk(KERN_DEBUG "%s: hw csum wrong pkt_flag:%x, err_flag:%x\n",
397                 atl1_driver_name, rrd->pkt_flg, rrd->err_flg);
398         skb->ip_summed = CHECKSUM_COMPLETE;
399         skb->csum = htons(rrd->xsz.xsum_sz.rx_chksum);
400         adapter->hw_csum_err++;
401         return;
402 }
403
404 /*
405  * atl1_alloc_rx_buffers - Replace used receive buffers
406  * @adapter: address of board private structure
407  */
408 static u16 atl1_alloc_rx_buffers(struct atl1_adapter *adapter)
409 {
410         struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
411         struct net_device *netdev = adapter->netdev;
412         struct pci_dev *pdev = adapter->pdev;
413         struct page *page;
414         unsigned long offset;
415         struct atl1_buffer *buffer_info, *next_info;
416         struct sk_buff *skb;
417         u16 num_alloc = 0;
418         u16 rfd_next_to_use, next_next;
419         struct rx_free_desc *rfd_desc;
420
421         next_next = rfd_next_to_use = atomic_read(&rfd_ring->next_to_use);
422         if (++next_next == rfd_ring->count)
423                 next_next = 0;
424         buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
425         next_info = &rfd_ring->buffer_info[next_next];
426
427         while (!buffer_info->alloced && !next_info->alloced) {
428                 if (buffer_info->skb) {
429                         buffer_info->alloced = 1;
430                         goto next;
431                 }
432
433                 rfd_desc = ATL1_RFD_DESC(rfd_ring, rfd_next_to_use);
434
435                 skb = dev_alloc_skb(adapter->rx_buffer_len + NET_IP_ALIGN);
436                 if (unlikely(!skb)) {   /* Better luck next round */
437                         adapter->net_stats.rx_dropped++;
438                         break;
439                 }
440
441                 /*
442                  * Make buffer alignment 2 beyond a 16 byte boundary
443                  * this will result in a 16 byte aligned IP header after
444                  * the 14 byte MAC header is removed
445                  */
446                 skb_reserve(skb, NET_IP_ALIGN);
447                 skb->dev = netdev;
448
449                 buffer_info->alloced = 1;
450                 buffer_info->skb = skb;
451                 buffer_info->length = (u16) adapter->rx_buffer_len;
452                 page = virt_to_page(skb->data);
453                 offset = (unsigned long)skb->data & ~PAGE_MASK;
454                 buffer_info->dma = pci_map_page(pdev, page, offset,
455                                                 adapter->rx_buffer_len,
456                                                 PCI_DMA_FROMDEVICE);
457                 rfd_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
458                 rfd_desc->buf_len = cpu_to_le16(adapter->rx_buffer_len);
459                 rfd_desc->coalese = 0;
460
461 next:
462                 rfd_next_to_use = next_next;
463                 if (unlikely(++next_next == rfd_ring->count))
464                         next_next = 0;
465
466                 buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
467                 next_info = &rfd_ring->buffer_info[next_next];
468                 num_alloc++;
469         }
470
471         if (num_alloc) {
472                 /*
473                  * Force memory writes to complete before letting h/w
474                  * know there are new descriptors to fetch.  (Only
475                  * applicable for weak-ordered memory model archs,
476                  * such as IA-64).
477                  */
478                 wmb();
479                 atomic_set(&rfd_ring->next_to_use, (int)rfd_next_to_use);
480         }
481         return num_alloc;
482 }
483
484 static void atl1_intr_rx(struct atl1_adapter *adapter)
485 {
486         int i, count;
487         u16 length;
488         u16 rrd_next_to_clean;
489         u32 value;
490         struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
491         struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
492         struct atl1_buffer *buffer_info;
493         struct rx_return_desc *rrd;
494         struct sk_buff *skb;
495
496         count = 0;
497
498         rrd_next_to_clean = atomic_read(&rrd_ring->next_to_clean);
499
500         while (1) {
501                 rrd = ATL1_RRD_DESC(rrd_ring, rrd_next_to_clean);
502                 i = 1;
503                 if (likely(rrd->xsz.valid)) {   /* packet valid */
504 chk_rrd:
505                         /* check rrd status */
506                         if (likely(rrd->num_buf == 1))
507                                 goto rrd_ok;
508
509                         /* rrd seems to be bad */
510                         if (unlikely(i-- > 0)) {
511                                 /* rrd may not be DMAed completely */
512                                 printk(KERN_DEBUG
513                                         "%s: RRD may not be DMAed completely\n",
514                                         atl1_driver_name);
515                                 udelay(1);
516                                 goto chk_rrd;
517                         }
518                         /* bad rrd */
519                         printk(KERN_DEBUG "%s: bad RRD\n", atl1_driver_name);
520                         /* see if update RFD index */
521                         if (rrd->num_buf > 1) {
522                                 u16 num_buf;
523                                 num_buf =
524                                     (rrd->xsz.xsum_sz.pkt_size +
525                                      adapter->rx_buffer_len -
526                                      1) / adapter->rx_buffer_len;
527                                 if (rrd->num_buf == num_buf) {
528                                         /* clean alloc flag for bad rrd */
529                                         while (rfd_ring->next_to_clean !=
530                                                (rrd->buf_indx + num_buf)) {
531                                                 rfd_ring->buffer_info[rfd_ring->
532                                                                       next_to_clean].alloced = 0;
533                                                 if (++rfd_ring->next_to_clean ==
534                                                     rfd_ring->count) {
535                                                         rfd_ring->
536                                                             next_to_clean = 0;
537                                                 }
538                                         }
539                                 }
540                         }
541
542                         /* update rrd */
543                         rrd->xsz.valid = 0;
544                         if (++rrd_next_to_clean == rrd_ring->count)
545                                 rrd_next_to_clean = 0;
546                         count++;
547                         continue;
548                 } else {        /* current rrd still not be updated */
549
550                         break;
551                 }
552 rrd_ok:
553                 /* clean alloc flag for bad rrd */
554                 while (rfd_ring->next_to_clean != rrd->buf_indx) {
555                         rfd_ring->buffer_info[rfd_ring->next_to_clean].alloced =
556                             0;
557                         if (++rfd_ring->next_to_clean == rfd_ring->count)
558                                 rfd_ring->next_to_clean = 0;
559                 }
560
561                 buffer_info = &rfd_ring->buffer_info[rrd->buf_indx];
562                 if (++rfd_ring->next_to_clean == rfd_ring->count)
563                         rfd_ring->next_to_clean = 0;
564
565                 /* update rrd next to clean */
566                 if (++rrd_next_to_clean == rrd_ring->count)
567                         rrd_next_to_clean = 0;
568                 count++;
569
570                 if (unlikely(rrd->pkt_flg & PACKET_FLAG_ERR)) {
571                         if (!(rrd->err_flg &
572                                 (ERR_FLAG_IP_CHKSUM | ERR_FLAG_L4_CHKSUM
573                                 | ERR_FLAG_LEN))) {
574                                 /* packet error, don't need upstream */
575                                 buffer_info->alloced = 0;
576                                 rrd->xsz.valid = 0;
577                                 continue;
578                         }
579                 }
580
581                 /* Good Receive */
582                 pci_unmap_page(adapter->pdev, buffer_info->dma,
583                                buffer_info->length, PCI_DMA_FROMDEVICE);
584                 skb = buffer_info->skb;
585                 length = le16_to_cpu(rrd->xsz.xsum_sz.pkt_size);
586
587                 skb_put(skb, length - ETHERNET_FCS_SIZE);
588
589                 /* Receive Checksum Offload */
590                 atl1_rx_checksum(adapter, rrd, skb);
591                 skb->protocol = eth_type_trans(skb, adapter->netdev);
592
593                 if (adapter->vlgrp && (rrd->pkt_flg & PACKET_FLAG_VLAN_INS)) {
594                         u16 vlan_tag = (rrd->vlan_tag >> 4) |
595                                         ((rrd->vlan_tag & 7) << 13) |
596                                         ((rrd->vlan_tag & 8) << 9);
597                         vlan_hwaccel_rx(skb, adapter->vlgrp, vlan_tag);
598                 } else
599                         netif_rx(skb);
600
601                 /* let protocol layer free skb */
602                 buffer_info->skb = NULL;
603                 buffer_info->alloced = 0;
604                 rrd->xsz.valid = 0;
605
606                 adapter->netdev->last_rx = jiffies;
607         }
608
609         atomic_set(&rrd_ring->next_to_clean, rrd_next_to_clean);
610
611         atl1_alloc_rx_buffers(adapter);
612
613         /* update mailbox ? */
614         if (count) {
615                 u32 tpd_next_to_use;
616                 u32 rfd_next_to_use;
617                 u32 rrd_next_to_clean;
618
619                 spin_lock(&adapter->mb_lock);
620
621                 tpd_next_to_use = atomic_read(&adapter->tpd_ring.next_to_use);
622                 rfd_next_to_use =
623                     atomic_read(&adapter->rfd_ring.next_to_use);
624                 rrd_next_to_clean =
625                     atomic_read(&adapter->rrd_ring.next_to_clean);
626                 value = ((rfd_next_to_use & MB_RFD_PROD_INDX_MASK) <<
627                         MB_RFD_PROD_INDX_SHIFT) |
628                         ((rrd_next_to_clean & MB_RRD_CONS_INDX_MASK) <<
629                         MB_RRD_CONS_INDX_SHIFT) |
630                         ((tpd_next_to_use & MB_TPD_PROD_INDX_MASK) <<
631                         MB_TPD_PROD_INDX_SHIFT);
632                 iowrite32(value, adapter->hw.hw_addr + REG_MAILBOX);
633                 spin_unlock(&adapter->mb_lock);
634         }
635 }
636
637 static void atl1_intr_tx(struct atl1_adapter *adapter)
638 {
639         struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
640         struct atl1_buffer *buffer_info;
641         u16 sw_tpd_next_to_clean;
642         u16 cmb_tpd_next_to_clean;
643         u8 update = 0;
644
645         sw_tpd_next_to_clean = atomic_read(&tpd_ring->next_to_clean);
646         cmb_tpd_next_to_clean = le16_to_cpu(adapter->cmb.cmb->tpd_cons_idx);
647
648         while (cmb_tpd_next_to_clean != sw_tpd_next_to_clean) {
649                 struct tx_packet_desc *tpd;
650                 update = 1;
651                 tpd = ATL1_TPD_DESC(tpd_ring, sw_tpd_next_to_clean);
652                 buffer_info = &tpd_ring->buffer_info[sw_tpd_next_to_clean];
653                 if (buffer_info->dma) {
654                         pci_unmap_page(adapter->pdev, buffer_info->dma,
655                                        buffer_info->length, PCI_DMA_TODEVICE);
656                         buffer_info->dma = 0;
657                 }
658
659                 if (buffer_info->skb) {
660                         dev_kfree_skb_irq(buffer_info->skb);
661                         buffer_info->skb = NULL;
662                 }
663                 tpd->buffer_addr = 0;
664                 tpd->desc.data = 0;
665
666                 if (++sw_tpd_next_to_clean == tpd_ring->count)
667                         sw_tpd_next_to_clean = 0;
668         }
669         atomic_set(&tpd_ring->next_to_clean, sw_tpd_next_to_clean);
670
671         if (netif_queue_stopped(adapter->netdev)
672             && netif_carrier_ok(adapter->netdev))
673                 netif_wake_queue(adapter->netdev);
674 }
675
676 static void atl1_check_for_link(struct atl1_adapter *adapter)
677 {
678         struct net_device *netdev = adapter->netdev;
679         u16 phy_data = 0;
680
681         spin_lock(&adapter->lock);
682         adapter->phy_timer_pending = false;
683         atl1_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
684         atl1_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
685         spin_unlock(&adapter->lock);
686
687         /* notify upper layer link down ASAP */
688         if (!(phy_data & BMSR_LSTATUS)) {       /* Link Down */
689                 if (netif_carrier_ok(netdev)) { /* old link state: Up */
690                         printk(KERN_INFO "%s: %s link is down\n",
691                                atl1_driver_name, netdev->name);
692                         adapter->link_speed = SPEED_0;
693                         netif_carrier_off(netdev);
694                         netif_stop_queue(netdev);
695                 }
696         }
697         schedule_work(&adapter->link_chg_task);
698 }
699
700 /*
701  * atl1_intr - Interrupt Handler
702  * @irq: interrupt number
703  * @data: pointer to a network interface device structure
704  * @pt_regs: CPU registers structure
705  */
706 static irqreturn_t atl1_intr(int irq, void *data)
707 {
708         /*struct atl1_adapter *adapter = ((struct net_device *)data)->priv;*/
709         struct atl1_adapter *adapter = netdev_priv(data);
710         u32 status;
711         u8 update_rx;
712         int max_ints = 10;
713
714         status = adapter->cmb.cmb->int_stats;
715         if (!status)
716                 return IRQ_NONE;
717
718         update_rx = 0;
719
720         do {
721                 /* clear CMB interrupt status at once */
722                 adapter->cmb.cmb->int_stats = 0;
723
724                 if (status & ISR_GPHY)  /* clear phy status */
725                         atl1_clear_phy_int(adapter);
726
727                 /* clear ISR status, and Enable CMB DMA/Disable Interrupt */
728                 iowrite32(status | ISR_DIS_INT, adapter->hw.hw_addr + REG_ISR);
729
730                 /* check if SMB intr */
731                 if (status & ISR_SMB)
732                         atl1_inc_smb(adapter);
733
734                 /* check if PCIE PHY Link down */
735                 if (status & ISR_PHY_LINKDOWN) {
736                         printk(KERN_DEBUG "%s: pcie phy link down %x\n",
737                                 atl1_driver_name, status);
738                         if (netif_running(adapter->netdev)) {   /* reset MAC */
739                                 iowrite32(0, adapter->hw.hw_addr + REG_IMR);
740                                 schedule_work(&adapter->pcie_dma_to_rst_task);
741                                 return IRQ_HANDLED;
742                         }
743                 }
744
745                 /* check if DMA read/write error ? */
746                 if (status & (ISR_DMAR_TO_RST | ISR_DMAW_TO_RST)) {
747                         printk(KERN_DEBUG
748                                 "%s: pcie DMA r/w error (status = 0x%x)\n",
749                                 atl1_driver_name, status);
750                         iowrite32(0, adapter->hw.hw_addr + REG_IMR);
751                         schedule_work(&adapter->pcie_dma_to_rst_task);
752                         return IRQ_HANDLED;
753                 }
754
755                 /* link event */
756                 if (status & ISR_GPHY) {
757                         adapter->soft_stats.tx_carrier_errors++;
758                         atl1_check_for_link(adapter);
759                 }
760
761                 /* transmit event */
762                 if (status & ISR_CMB_TX)
763                         atl1_intr_tx(adapter);
764
765                 /* rx exception */
766                 if (unlikely(status & (ISR_RXF_OV | ISR_RFD_UNRUN |
767                                 ISR_RRD_OV | ISR_HOST_RFD_UNRUN |
768                                 ISR_HOST_RRD_OV | ISR_CMB_RX))) {
769                         if (status &
770                             (ISR_RXF_OV | ISR_RFD_UNRUN | ISR_RRD_OV |
771                              ISR_HOST_RFD_UNRUN | ISR_HOST_RRD_OV))
772                                 printk(KERN_INFO
773                                         "%s: rx exception: status = 0x%x\n",
774                                         atl1_driver_name, status);
775                         atl1_intr_rx(adapter);
776                 }
777
778                 if (--max_ints < 0)
779                         break;
780
781         } while ((status = adapter->cmb.cmb->int_stats));
782
783         /* re-enable Interrupt */
784         iowrite32(ISR_DIS_SMB | ISR_DIS_DMA, adapter->hw.hw_addr + REG_ISR);
785         return IRQ_HANDLED;
786 }
787
788 /*
789  * atl1_set_multi - Multicast and Promiscuous mode set
790  * @netdev: network interface device structure
791  *
792  * The set_multi entry point is called whenever the multicast address
793  * list or the network interface flags are updated.  This routine is
794  * responsible for configuring the hardware for proper multicast,
795  * promiscuous mode, and all-multi behavior.
796  */
797 static void atl1_set_multi(struct net_device *netdev)
798 {
799         struct atl1_adapter *adapter = netdev_priv(netdev);
800         struct atl1_hw *hw = &adapter->hw;
801         struct dev_mc_list *mc_ptr;
802         u32 rctl;
803         u32 hash_value;
804
805         /* Check for Promiscuous and All Multicast modes */
806         rctl = ioread32(hw->hw_addr + REG_MAC_CTRL);
807         if (netdev->flags & IFF_PROMISC)
808                 rctl |= MAC_CTRL_PROMIS_EN;
809         else if (netdev->flags & IFF_ALLMULTI) {
810                 rctl |= MAC_CTRL_MC_ALL_EN;
811                 rctl &= ~MAC_CTRL_PROMIS_EN;
812         } else
813                 rctl &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
814
815         iowrite32(rctl, hw->hw_addr + REG_MAC_CTRL);
816
817         /* clear the old settings from the multicast hash table */
818         iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE);
819         iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2));
820
821         /* compute mc addresses' hash value ,and put it into hash table */
822         for (mc_ptr = netdev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
823                 hash_value = atl1_hash_mc_addr(hw, mc_ptr->dmi_addr);
824                 atl1_hash_set(hw, hash_value);
825         }
826 }
827
828 static void atl1_setup_mac_ctrl(struct atl1_adapter *adapter)
829 {
830         u32 value;
831         struct atl1_hw *hw = &adapter->hw;
832         struct net_device *netdev = adapter->netdev;
833         /* Config MAC CTRL Register */
834         value = MAC_CTRL_TX_EN | MAC_CTRL_RX_EN;
835         /* duplex */
836         if (FULL_DUPLEX == adapter->link_duplex)
837                 value |= MAC_CTRL_DUPLX;
838         /* speed */
839         value |= ((u32) ((SPEED_1000 == adapter->link_speed) ?
840                          MAC_CTRL_SPEED_1000 : MAC_CTRL_SPEED_10_100) <<
841                   MAC_CTRL_SPEED_SHIFT);
842         /* flow control */
843         value |= (MAC_CTRL_TX_FLOW | MAC_CTRL_RX_FLOW);
844         /* PAD & CRC */
845         value |= (MAC_CTRL_ADD_CRC | MAC_CTRL_PAD);
846         /* preamble length */
847         value |= (((u32) adapter->hw.preamble_len
848                    & MAC_CTRL_PRMLEN_MASK) << MAC_CTRL_PRMLEN_SHIFT);
849         /* vlan */
850         if (adapter->vlgrp)
851                 value |= MAC_CTRL_RMV_VLAN;
852         /* rx checksum
853            if (adapter->rx_csum)
854            value |= MAC_CTRL_RX_CHKSUM_EN;
855          */
856         /* filter mode */
857         value |= MAC_CTRL_BC_EN;
858         if (netdev->flags & IFF_PROMISC)
859                 value |= MAC_CTRL_PROMIS_EN;
860         else if (netdev->flags & IFF_ALLMULTI)
861                 value |= MAC_CTRL_MC_ALL_EN;
862         /* value |= MAC_CTRL_LOOPBACK; */
863         iowrite32(value, hw->hw_addr + REG_MAC_CTRL);
864 }
865
866 static u32 atl1_check_link(struct atl1_adapter *adapter)
867 {
868         struct atl1_hw *hw = &adapter->hw;
869         struct net_device *netdev = adapter->netdev;
870         u32 ret_val;
871         u16 speed, duplex, phy_data;
872         int reconfig = 0;
873
874         /* MII_BMSR must read twice */
875         atl1_read_phy_reg(hw, MII_BMSR, &phy_data);
876         atl1_read_phy_reg(hw, MII_BMSR, &phy_data);
877         if (!(phy_data & BMSR_LSTATUS)) {       /* link down */
878                 if (netif_carrier_ok(netdev)) { /* old link state: Up */
879                         printk(KERN_INFO "%s: link is down\n",
880                                 atl1_driver_name);
881                         adapter->link_speed = SPEED_0;
882                         netif_carrier_off(netdev);
883                         netif_stop_queue(netdev);
884                 }
885                 return ATL1_SUCCESS;
886         }
887
888         /* Link Up */
889         ret_val = atl1_get_speed_and_duplex(hw, &speed, &duplex);
890         if (ret_val)
891                 return ret_val;
892
893         switch (hw->media_type) {
894         case MEDIA_TYPE_1000M_FULL:
895                 if (speed != SPEED_1000 || duplex != FULL_DUPLEX)
896                         reconfig = 1;
897                 break;
898         case MEDIA_TYPE_100M_FULL:
899                 if (speed != SPEED_100 || duplex != FULL_DUPLEX)
900                         reconfig = 1;
901                 break;
902         case MEDIA_TYPE_100M_HALF:
903                 if (speed != SPEED_100 || duplex != HALF_DUPLEX)
904                         reconfig = 1;
905                 break;
906         case MEDIA_TYPE_10M_FULL:
907                 if (speed != SPEED_10 || duplex != FULL_DUPLEX)
908                         reconfig = 1;
909                 break;
910         case MEDIA_TYPE_10M_HALF:
911                 if (speed != SPEED_10 || duplex != HALF_DUPLEX)
912                         reconfig = 1;
913                 break;
914         }
915
916         /* link result is our setting */
917         if (!reconfig) {
918                 if (adapter->link_speed != speed
919                     || adapter->link_duplex != duplex) {
920                         adapter->link_speed = speed;
921                         adapter->link_duplex = duplex;
922                         atl1_setup_mac_ctrl(adapter);
923                         printk(KERN_INFO "%s: %s link is up %d Mbps %s\n",
924                                atl1_driver_name, netdev->name,
925                                adapter->link_speed,
926                                adapter->link_duplex ==
927                                FULL_DUPLEX ? "full duplex" : "half duplex");
928                 }
929                 if (!netif_carrier_ok(netdev)) {        /* Link down -> Up */
930                         netif_carrier_on(netdev);
931                         netif_wake_queue(netdev);
932                 }
933                 return ATL1_SUCCESS;
934         }
935
936         /* change orignal link status */
937         if (netif_carrier_ok(netdev)) {
938                 adapter->link_speed = SPEED_0;
939                 netif_carrier_off(netdev);
940                 netif_stop_queue(netdev);
941         }
942
943         if (hw->media_type != MEDIA_TYPE_AUTO_SENSOR &&
944             hw->media_type != MEDIA_TYPE_1000M_FULL) {
945                 switch (hw->media_type) {
946                 case MEDIA_TYPE_100M_FULL:
947                         phy_data = MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
948                                    MII_CR_RESET;
949                         break;
950                 case MEDIA_TYPE_100M_HALF:
951                         phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
952                         break;
953                 case MEDIA_TYPE_10M_FULL:
954                         phy_data =
955                             MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
956                         break;
957                 default:        /* MEDIA_TYPE_10M_HALF: */
958                         phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
959                         break;
960                 }
961                 atl1_write_phy_reg(hw, MII_BMCR, phy_data);
962                 return ATL1_SUCCESS;
963         }
964
965         /* auto-neg, insert timer to re-config phy */
966         if (!adapter->phy_timer_pending) {
967                 adapter->phy_timer_pending = true;
968                 mod_timer(&adapter->phy_config_timer, jiffies + 3 * HZ);
969         }
970
971         return ATL1_SUCCESS;
972 }
973
974 static void set_flow_ctrl_old(struct atl1_adapter *adapter)
975 {
976         u32 hi, lo, value;
977
978         /* RFD Flow Control */
979         value = adapter->rfd_ring.count;
980         hi = value / 16;
981         if (hi < 2)
982                 hi = 2;
983         lo = value * 7 / 8;
984
985         value = ((hi & RXQ_RXF_PAUSE_TH_HI_MASK) << RXQ_RXF_PAUSE_TH_HI_SHIFT) |
986             ((lo & RXQ_RXF_PAUSE_TH_LO_MASK) << RXQ_RXF_PAUSE_TH_LO_SHIFT);
987         iowrite32(value, adapter->hw.hw_addr + REG_RXQ_RXF_PAUSE_THRESH);
988
989         /* RRD Flow Control */
990         value = adapter->rrd_ring.count;
991         lo = value / 16;
992         hi = value * 7 / 8;
993         if (lo < 2)
994                 lo = 2;
995         value = ((hi & RXQ_RRD_PAUSE_TH_HI_MASK) << RXQ_RRD_PAUSE_TH_HI_SHIFT) |
996             ((lo & RXQ_RRD_PAUSE_TH_LO_MASK) << RXQ_RRD_PAUSE_TH_LO_SHIFT);
997         iowrite32(value, adapter->hw.hw_addr + REG_RXQ_RRD_PAUSE_THRESH);
998 }
999
1000 static void set_flow_ctrl_new(struct atl1_hw *hw)
1001 {
1002         u32 hi, lo, value;
1003
1004         /* RXF Flow Control */
1005         value = ioread32(hw->hw_addr + REG_SRAM_RXF_LEN);
1006         lo = value / 16;
1007         if (lo < 192)
1008                 lo = 192;
1009         hi = value * 7 / 8;
1010         if (hi < lo)
1011                 hi = lo + 16;
1012         value = ((hi & RXQ_RXF_PAUSE_TH_HI_MASK) << RXQ_RXF_PAUSE_TH_HI_SHIFT) |
1013             ((lo & RXQ_RXF_PAUSE_TH_LO_MASK) << RXQ_RXF_PAUSE_TH_LO_SHIFT);
1014         iowrite32(value, hw->hw_addr + REG_RXQ_RXF_PAUSE_THRESH);
1015
1016         /* RRD Flow Control */
1017         value = ioread32(hw->hw_addr + REG_SRAM_RRD_LEN);
1018         lo = value / 8;
1019         hi = value * 7 / 8;
1020         if (lo < 2)
1021                 lo = 2;
1022         if (hi < lo)
1023                 hi = lo + 3;
1024         value = ((hi & RXQ_RRD_PAUSE_TH_HI_MASK) << RXQ_RRD_PAUSE_TH_HI_SHIFT) |
1025             ((lo & RXQ_RRD_PAUSE_TH_LO_MASK) << RXQ_RRD_PAUSE_TH_LO_SHIFT);
1026         iowrite32(value, hw->hw_addr + REG_RXQ_RRD_PAUSE_THRESH);
1027 }
1028
1029 /*
1030  * atl1_configure - Configure Transmit&Receive Unit after Reset
1031  * @adapter: board private structure
1032  *
1033  * Configure the Tx /Rx unit of the MAC after a reset.
1034  */
1035 static u32 atl1_configure(struct atl1_adapter *adapter)
1036 {
1037         struct atl1_hw *hw = &adapter->hw;
1038         u32 value;
1039
1040         /* clear interrupt status */
1041         iowrite32(0xffffffff, adapter->hw.hw_addr + REG_ISR);
1042
1043         /* set MAC Address */
1044         value = (((u32) hw->mac_addr[2]) << 24) |
1045                 (((u32) hw->mac_addr[3]) << 16) |
1046                 (((u32) hw->mac_addr[4]) << 8) |
1047                 (((u32) hw->mac_addr[5]));
1048         iowrite32(value, hw->hw_addr + REG_MAC_STA_ADDR);
1049         value = (((u32) hw->mac_addr[0]) << 8) | (((u32) hw->mac_addr[1]));
1050         iowrite32(value, hw->hw_addr + (REG_MAC_STA_ADDR + 4));
1051
1052         /* tx / rx ring */
1053
1054         /* HI base address */
1055         iowrite32((u32) ((adapter->tpd_ring.dma & 0xffffffff00000000ULL) >> 32),
1056                 hw->hw_addr + REG_DESC_BASE_ADDR_HI);
1057         /* LO base address */
1058         iowrite32((u32) (adapter->rfd_ring.dma & 0x00000000ffffffffULL),
1059                 hw->hw_addr + REG_DESC_RFD_ADDR_LO);
1060         iowrite32((u32) (adapter->rrd_ring.dma & 0x00000000ffffffffULL),
1061                 hw->hw_addr + REG_DESC_RRD_ADDR_LO);
1062         iowrite32((u32) (adapter->tpd_ring.dma & 0x00000000ffffffffULL),
1063                 hw->hw_addr + REG_DESC_TPD_ADDR_LO);
1064         iowrite32((u32) (adapter->cmb.dma & 0x00000000ffffffffULL),
1065                 hw->hw_addr + REG_DESC_CMB_ADDR_LO);
1066         iowrite32((u32) (adapter->smb.dma & 0x00000000ffffffffULL),
1067                 hw->hw_addr + REG_DESC_SMB_ADDR_LO);
1068
1069         /* element count */
1070         value = adapter->rrd_ring.count;
1071         value <<= 16;
1072         value += adapter->rfd_ring.count;
1073         iowrite32(value, hw->hw_addr + REG_DESC_RFD_RRD_RING_SIZE);
1074         iowrite32(adapter->tpd_ring.count, hw->hw_addr + REG_DESC_TPD_RING_SIZE);
1075
1076         /* Load Ptr */
1077         iowrite32(1, hw->hw_addr + REG_LOAD_PTR);
1078
1079         /* config Mailbox */
1080         value = ((atomic_read(&adapter->tpd_ring.next_to_use)
1081                   & MB_TPD_PROD_INDX_MASK) << MB_TPD_PROD_INDX_SHIFT) |
1082             ((atomic_read(&adapter->rrd_ring.next_to_clean)
1083               & MB_RRD_CONS_INDX_MASK) << MB_RRD_CONS_INDX_SHIFT) |
1084             ((atomic_read(&adapter->rfd_ring.next_to_use)
1085               & MB_RFD_PROD_INDX_MASK) << MB_RFD_PROD_INDX_SHIFT);
1086         iowrite32(value, hw->hw_addr + REG_MAILBOX);
1087
1088         /* config IPG/IFG */
1089         value = (((u32) hw->ipgt & MAC_IPG_IFG_IPGT_MASK)
1090                  << MAC_IPG_IFG_IPGT_SHIFT) |
1091             (((u32) hw->min_ifg & MAC_IPG_IFG_MIFG_MASK)
1092              << MAC_IPG_IFG_MIFG_SHIFT) |
1093             (((u32) hw->ipgr1 & MAC_IPG_IFG_IPGR1_MASK)
1094              << MAC_IPG_IFG_IPGR1_SHIFT) |
1095             (((u32) hw->ipgr2 & MAC_IPG_IFG_IPGR2_MASK)
1096              << MAC_IPG_IFG_IPGR2_SHIFT);
1097         iowrite32(value, hw->hw_addr + REG_MAC_IPG_IFG);
1098
1099         /* config  Half-Duplex Control */
1100         value = ((u32) hw->lcol & MAC_HALF_DUPLX_CTRL_LCOL_MASK) |
1101             (((u32) hw->max_retry & MAC_HALF_DUPLX_CTRL_RETRY_MASK)
1102              << MAC_HALF_DUPLX_CTRL_RETRY_SHIFT) |
1103             MAC_HALF_DUPLX_CTRL_EXC_DEF_EN |
1104             (0xa << MAC_HALF_DUPLX_CTRL_ABEBT_SHIFT) |
1105             (((u32) hw->jam_ipg & MAC_HALF_DUPLX_CTRL_JAMIPG_MASK)
1106              << MAC_HALF_DUPLX_CTRL_JAMIPG_SHIFT);
1107         iowrite32(value, hw->hw_addr + REG_MAC_HALF_DUPLX_CTRL);
1108
1109         /* set Interrupt Moderator Timer */
1110         iowrite16(adapter->imt, hw->hw_addr + REG_IRQ_MODU_TIMER_INIT);
1111         iowrite32(MASTER_CTRL_ITIMER_EN, hw->hw_addr + REG_MASTER_CTRL);
1112
1113         /* set Interrupt Clear Timer */
1114         iowrite16(adapter->ict, hw->hw_addr + REG_CMBDISDMA_TIMER);
1115
1116         /* set MTU, 4 : VLAN */
1117         iowrite32(hw->max_frame_size + 4, hw->hw_addr + REG_MTU);
1118
1119         /* jumbo size & rrd retirement timer */
1120         value = (((u32) hw->rx_jumbo_th & RXQ_JMBOSZ_TH_MASK)
1121                  << RXQ_JMBOSZ_TH_SHIFT) |
1122             (((u32) hw->rx_jumbo_lkah & RXQ_JMBO_LKAH_MASK)
1123              << RXQ_JMBO_LKAH_SHIFT) |
1124             (((u32) hw->rrd_ret_timer & RXQ_RRD_TIMER_MASK)
1125              << RXQ_RRD_TIMER_SHIFT);
1126         iowrite32(value, hw->hw_addr + REG_RXQ_JMBOSZ_RRDTIM);
1127
1128         /* Flow Control */
1129         switch (hw->dev_rev) {
1130         case 0x8001:
1131         case 0x9001:
1132         case 0x9002:
1133         case 0x9003:
1134                 set_flow_ctrl_old(adapter);
1135                 break;
1136         default:
1137                 set_flow_ctrl_new(hw);
1138                 break;
1139         }
1140
1141         /* config TXQ */
1142         value = (((u32) hw->tpd_burst & TXQ_CTRL_TPD_BURST_NUM_MASK)
1143                  << TXQ_CTRL_TPD_BURST_NUM_SHIFT) |
1144             (((u32) hw->txf_burst & TXQ_CTRL_TXF_BURST_NUM_MASK)
1145              << TXQ_CTRL_TXF_BURST_NUM_SHIFT) |
1146             (((u32) hw->tpd_fetch_th & TXQ_CTRL_TPD_FETCH_TH_MASK)
1147              << TXQ_CTRL_TPD_FETCH_TH_SHIFT) | TXQ_CTRL_ENH_MODE | TXQ_CTRL_EN;
1148         iowrite32(value, hw->hw_addr + REG_TXQ_CTRL);
1149
1150         /* min tpd fetch gap & tx jumbo packet size threshold for taskoffload */
1151         value = (((u32) hw->tx_jumbo_task_th & TX_JUMBO_TASK_TH_MASK)
1152                  << TX_JUMBO_TASK_TH_SHIFT) |
1153             (((u32) hw->tpd_fetch_gap & TX_TPD_MIN_IPG_MASK)
1154              << TX_TPD_MIN_IPG_SHIFT);
1155         iowrite32(value, hw->hw_addr + REG_TX_JUMBO_TASK_TH_TPD_IPG);
1156
1157         /* config RXQ */
1158         value = (((u32) hw->rfd_burst & RXQ_CTRL_RFD_BURST_NUM_MASK)
1159                  << RXQ_CTRL_RFD_BURST_NUM_SHIFT) |
1160             (((u32) hw->rrd_burst & RXQ_CTRL_RRD_BURST_THRESH_MASK)
1161              << RXQ_CTRL_RRD_BURST_THRESH_SHIFT) |
1162             (((u32) hw->rfd_fetch_gap & RXQ_CTRL_RFD_PREF_MIN_IPG_MASK)
1163              << RXQ_CTRL_RFD_PREF_MIN_IPG_SHIFT) |
1164             RXQ_CTRL_CUT_THRU_EN | RXQ_CTRL_EN;
1165         iowrite32(value, hw->hw_addr + REG_RXQ_CTRL);
1166
1167         /* config DMA Engine */
1168         value = ((((u32) hw->dmar_block) & DMA_CTRL_DMAR_BURST_LEN_MASK)
1169                  << DMA_CTRL_DMAR_BURST_LEN_SHIFT) |
1170             ((((u32) hw->dmaw_block) & DMA_CTRL_DMAR_BURST_LEN_MASK)
1171              << DMA_CTRL_DMAR_BURST_LEN_SHIFT) |
1172             DMA_CTRL_DMAR_EN | DMA_CTRL_DMAW_EN;
1173         value |= (u32) hw->dma_ord;
1174         if (atl1_rcb_128 == hw->rcb_value)
1175                 value |= DMA_CTRL_RCB_VALUE;
1176         iowrite32(value, hw->hw_addr + REG_DMA_CTRL);
1177
1178         /* config CMB / SMB */
1179         value = hw->cmb_rrd | ((u32) hw->cmb_tpd << 16);
1180         iowrite32(value, hw->hw_addr + REG_CMB_WRITE_TH);
1181         value = hw->cmb_rx_timer | ((u32) hw->cmb_tx_timer << 16);
1182         iowrite32(value, hw->hw_addr + REG_CMB_WRITE_TIMER);
1183         iowrite32(hw->smb_timer, hw->hw_addr + REG_SMB_TIMER);
1184
1185         /* --- enable CMB / SMB */
1186         value = CSMB_CTRL_CMB_EN | CSMB_CTRL_SMB_EN;
1187         iowrite32(value, hw->hw_addr + REG_CSMB_CTRL);
1188
1189         value = ioread32(adapter->hw.hw_addr + REG_ISR);
1190         if (unlikely((value & ISR_PHY_LINKDOWN) != 0))
1191                 value = 1;      /* config failed */
1192         else
1193                 value = 0;
1194
1195         /* clear all interrupt status */
1196         iowrite32(0x3fffffff, adapter->hw.hw_addr + REG_ISR);
1197         iowrite32(0, adapter->hw.hw_addr + REG_ISR);
1198         return value;
1199 }
1200
1201 /*
1202  * atl1_irq_disable - Mask off interrupt generation on the NIC
1203  * @adapter: board private structure
1204  */
1205 static void atl1_irq_disable(struct atl1_adapter *adapter)
1206 {
1207         atomic_inc(&adapter->irq_sem);
1208         iowrite32(0, adapter->hw.hw_addr + REG_IMR);
1209         ioread32(adapter->hw.hw_addr + REG_IMR);
1210         synchronize_irq(adapter->pdev->irq);
1211 }
1212
1213 static void atl1_vlan_rx_register(struct net_device *netdev,
1214                                 struct vlan_group *grp)
1215 {
1216         struct atl1_adapter *adapter = netdev_priv(netdev);
1217         unsigned long flags;
1218         u32 ctrl;
1219
1220         spin_lock_irqsave(&adapter->lock, flags);
1221         /* atl1_irq_disable(adapter); */
1222         adapter->vlgrp = grp;
1223
1224         if (grp) {
1225                 /* enable VLAN tag insert/strip */
1226                 ctrl = ioread32(adapter->hw.hw_addr + REG_MAC_CTRL);
1227                 ctrl |= MAC_CTRL_RMV_VLAN;
1228                 iowrite32(ctrl, adapter->hw.hw_addr + REG_MAC_CTRL);
1229         } else {
1230                 /* disable VLAN tag insert/strip */
1231                 ctrl = ioread32(adapter->hw.hw_addr + REG_MAC_CTRL);
1232                 ctrl &= ~MAC_CTRL_RMV_VLAN;
1233                 iowrite32(ctrl, adapter->hw.hw_addr + REG_MAC_CTRL);
1234         }
1235
1236         /* atl1_irq_enable(adapter); */
1237         spin_unlock_irqrestore(&adapter->lock, flags);
1238 }
1239
1240 /* FIXME: justify or remove -- CHS */
1241 static void atl1_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1242 {
1243         /* We don't do Vlan filtering */
1244         return;
1245 }
1246
1247 /* FIXME: this looks wrong too -- CHS */
1248 static void atl1_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1249 {
1250         struct atl1_adapter *adapter = netdev_priv(netdev);
1251         unsigned long flags;
1252
1253         spin_lock_irqsave(&adapter->lock, flags);
1254         /* atl1_irq_disable(adapter); */
1255         vlan_group_set_device(adapter->vlgrp, vid, NULL);
1256         /* atl1_irq_enable(adapter); */
1257         spin_unlock_irqrestore(&adapter->lock, flags);
1258         /* We don't do Vlan filtering */
1259         return;
1260 }
1261
1262 static void atl1_restore_vlan(struct atl1_adapter *adapter)
1263 {
1264         atl1_vlan_rx_register(adapter->netdev, adapter->vlgrp);
1265         if (adapter->vlgrp) {
1266                 u16 vid;
1267                 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
1268                         if (!vlan_group_get_device(adapter->vlgrp, vid))
1269                                 continue;
1270                         atl1_vlan_rx_add_vid(adapter->netdev, vid);
1271                 }
1272         }
1273 }
1274
1275 static u16 tpd_avail(struct atl1_tpd_ring *tpd_ring)
1276 {
1277         u16 next_to_clean = atomic_read(&tpd_ring->next_to_clean);
1278         u16 next_to_use = atomic_read(&tpd_ring->next_to_use);
1279         return ((next_to_clean >
1280                  next_to_use) ? next_to_clean - next_to_use -
1281                 1 : tpd_ring->count + next_to_clean - next_to_use - 1);
1282 }
1283
1284 static int atl1_tso(struct atl1_adapter *adapter, struct sk_buff *skb,
1285                          struct tso_param *tso)
1286 {
1287         /* We enter this function holding a spinlock. */
1288         u8 ipofst;
1289         int err;
1290
1291         if (skb_shinfo(skb)->gso_size) {
1292                 if (skb_header_cloned(skb)) {
1293                         err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1294                         if (unlikely(err))
1295                                 return err;
1296                 }
1297
1298                 if (skb->protocol == ntohs(ETH_P_IP)) {
1299                         skb->nh.iph->tot_len = 0;
1300                         skb->nh.iph->check = 0;
1301                         skb->h.th->check =
1302                             ~csum_tcpudp_magic(skb->nh.iph->saddr,
1303                                                skb->nh.iph->daddr, 0,
1304                                                IPPROTO_TCP, 0);
1305                         ipofst = skb->nh.raw - skb->data;
1306                         if (ipofst != ENET_HEADER_SIZE) /* 802.3 frame */
1307                                 tso->tsopl |= 1 << TSO_PARAM_ETHTYPE_SHIFT;
1308
1309                         tso->tsopl |= (skb->nh.iph->ihl &
1310                                 CSUM_PARAM_IPHL_MASK) << CSUM_PARAM_IPHL_SHIFT;
1311                         tso->tsopl |= ((skb->h.th->doff << 2) &
1312                                 TSO_PARAM_TCPHDRLEN_MASK) << TSO_PARAM_TCPHDRLEN_SHIFT;
1313                         tso->tsopl |= (skb_shinfo(skb)->gso_size &
1314                                 TSO_PARAM_MSS_MASK) << TSO_PARAM_MSS_SHIFT;
1315                         tso->tsopl |= 1 << TSO_PARAM_IPCKSUM_SHIFT;
1316                         tso->tsopl |= 1 << TSO_PARAM_TCPCKSUM_SHIFT;
1317                         tso->tsopl |= 1 << TSO_PARAM_SEGMENT_SHIFT;
1318                         return true;
1319                 }
1320         }
1321         return false;
1322 }
1323
1324 static int atl1_tx_csum(struct atl1_adapter *adapter, struct sk_buff *skb,
1325                         struct csum_param *csum)
1326 {
1327         u8 css, cso;
1328
1329         if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
1330                 cso = skb->h.raw - skb->data;
1331                 css = (skb->h.raw + skb->csum) - skb->data;
1332                 if (unlikely(cso & 0x1)) {
1333                         printk(KERN_DEBUG "%s: payload offset != even number\n",
1334                                 atl1_driver_name);
1335                         return -1;
1336                 }
1337                 csum->csumpl |= (cso & CSUM_PARAM_PLOADOFFSET_MASK) <<
1338                         CSUM_PARAM_PLOADOFFSET_SHIFT;
1339                 csum->csumpl |= (css & CSUM_PARAM_XSUMOFFSET_MASK) <<
1340                         CSUM_PARAM_XSUMOFFSET_SHIFT;
1341                 csum->csumpl |= 1 << CSUM_PARAM_CUSTOMCKSUM_SHIFT;
1342                 return true;
1343         }
1344
1345         return true;
1346 }
1347
1348 static void atl1_tx_map(struct atl1_adapter *adapter,
1349                                 struct sk_buff *skb, bool tcp_seg)
1350 {
1351         /* We enter this function holding a spinlock. */
1352         struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1353         struct atl1_buffer *buffer_info;
1354         struct page *page;
1355         int first_buf_len = skb->len;
1356         unsigned long offset;
1357         unsigned int nr_frags;
1358         unsigned int f;
1359         u16 tpd_next_to_use;
1360         u16 proto_hdr_len;
1361         u16 i, m, len12;
1362
1363         first_buf_len -= skb->data_len;
1364         nr_frags = skb_shinfo(skb)->nr_frags;
1365         tpd_next_to_use = atomic_read(&tpd_ring->next_to_use);
1366         buffer_info = &tpd_ring->buffer_info[tpd_next_to_use];
1367         if (unlikely(buffer_info->skb))
1368                 BUG();
1369         buffer_info->skb = NULL;        /* put skb in last TPD */
1370
1371         if (tcp_seg) {
1372                 /* TSO/GSO */
1373                 proto_hdr_len =
1374                     ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
1375                 buffer_info->length = proto_hdr_len;
1376                 page = virt_to_page(skb->data);
1377                 offset = (unsigned long)skb->data & ~PAGE_MASK;
1378                 buffer_info->dma = pci_map_page(adapter->pdev, page,
1379                                                 offset, proto_hdr_len,
1380                                                 PCI_DMA_TODEVICE);
1381
1382                 if (++tpd_next_to_use == tpd_ring->count)
1383                         tpd_next_to_use = 0;
1384
1385                 if (first_buf_len > proto_hdr_len) {
1386                         len12 = first_buf_len - proto_hdr_len;
1387                         m = (len12 + MAX_TX_BUF_LEN - 1) / MAX_TX_BUF_LEN;
1388                         for (i = 0; i < m; i++) {
1389                                 buffer_info =
1390                                     &tpd_ring->buffer_info[tpd_next_to_use];
1391                                 buffer_info->skb = NULL;
1392                                 buffer_info->length =
1393                                     (MAX_TX_BUF_LEN >=
1394                                      len12) ? MAX_TX_BUF_LEN : len12;
1395                                 len12 -= buffer_info->length;
1396                                 page = virt_to_page(skb->data +
1397                                                  (proto_hdr_len +
1398                                                   i * MAX_TX_BUF_LEN));
1399                                 offset = (unsigned long)(skb->data +
1400                                                         (proto_hdr_len +
1401                                                         i * MAX_TX_BUF_LEN)) &
1402                                                         ~PAGE_MASK;
1403                                 buffer_info->dma =
1404                                     pci_map_page(adapter->pdev, page, offset,
1405                                                  buffer_info->length,
1406                                                  PCI_DMA_TODEVICE);
1407                                 if (++tpd_next_to_use == tpd_ring->count)
1408                                         tpd_next_to_use = 0;
1409                         }
1410                 }
1411         } else {
1412                 /* not TSO/GSO */
1413                 buffer_info->length = first_buf_len;
1414                 page = virt_to_page(skb->data);
1415                 offset = (unsigned long)skb->data & ~PAGE_MASK;
1416                 buffer_info->dma = pci_map_page(adapter->pdev, page,
1417                                                 offset, first_buf_len,
1418                                                 PCI_DMA_TODEVICE);
1419                 if (++tpd_next_to_use == tpd_ring->count)
1420                         tpd_next_to_use = 0;
1421         }
1422
1423         for (f = 0; f < nr_frags; f++) {
1424                 struct skb_frag_struct *frag;
1425                 u16 lenf, i, m;
1426
1427                 frag = &skb_shinfo(skb)->frags[f];
1428                 lenf = frag->size;
1429
1430                 m = (lenf + MAX_TX_BUF_LEN - 1) / MAX_TX_BUF_LEN;
1431                 for (i = 0; i < m; i++) {
1432                         buffer_info = &tpd_ring->buffer_info[tpd_next_to_use];
1433                         if (unlikely(buffer_info->skb))
1434                                 BUG();
1435                         buffer_info->skb = NULL;
1436                         buffer_info->length =
1437                             (lenf > MAX_TX_BUF_LEN) ? MAX_TX_BUF_LEN : lenf;
1438                         lenf -= buffer_info->length;
1439                         buffer_info->dma =
1440                             pci_map_page(adapter->pdev, frag->page,
1441                                          frag->page_offset + i * MAX_TX_BUF_LEN,
1442                                          buffer_info->length, PCI_DMA_TODEVICE);
1443
1444                         if (++tpd_next_to_use == tpd_ring->count)
1445                                 tpd_next_to_use = 0;
1446                 }
1447         }
1448
1449         /* last tpd's buffer-info */
1450         buffer_info->skb = skb;
1451 }
1452
1453 static void atl1_tx_queue(struct atl1_adapter *adapter, int count,
1454                                union tpd_descr *descr)
1455 {
1456         /* We enter this function holding a spinlock. */
1457         struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1458         int j;
1459         u32 val;
1460         struct atl1_buffer *buffer_info;
1461         struct tx_packet_desc *tpd;
1462         u16 tpd_next_to_use = atomic_read(&tpd_ring->next_to_use);
1463
1464         for (j = 0; j < count; j++) {
1465                 buffer_info = &tpd_ring->buffer_info[tpd_next_to_use];
1466                 tpd = ATL1_TPD_DESC(&adapter->tpd_ring, tpd_next_to_use);
1467                 tpd->desc.csum.csumpu = descr->csum.csumpu;
1468                 tpd->desc.csum.csumpl = descr->csum.csumpl;
1469                 tpd->desc.tso.tsopu = descr->tso.tsopu;
1470                 tpd->desc.tso.tsopl = descr->tso.tsopl;
1471                 tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
1472                 tpd->desc.data = descr->data;
1473                 tpd->desc.csum.csumpu |= (cpu_to_le16(buffer_info->length) &
1474                         CSUM_PARAM_BUFLEN_MASK) << CSUM_PARAM_BUFLEN_SHIFT;
1475
1476                 val = (descr->tso.tsopl >> TSO_PARAM_SEGMENT_SHIFT) &
1477                         TSO_PARAM_SEGMENT_MASK;
1478                 if (val && !j)
1479                         tpd->desc.tso.tsopl |= 1 << TSO_PARAM_HDRFLAG_SHIFT;
1480
1481                 if (j == (count - 1))
1482                         tpd->desc.csum.csumpl |= 1 << CSUM_PARAM_EOP_SHIFT;
1483
1484                 if (++tpd_next_to_use == tpd_ring->count)
1485                         tpd_next_to_use = 0;
1486         }
1487         /*
1488          * Force memory writes to complete before letting h/w
1489          * know there are new descriptors to fetch.  (Only
1490          * applicable for weak-ordered memory model archs,
1491          * such as IA-64).
1492          */
1493         wmb();
1494
1495         atomic_set(&tpd_ring->next_to_use, (int)tpd_next_to_use);
1496 }
1497
1498 static void atl1_update_mailbox(struct atl1_adapter *adapter)
1499 {
1500         unsigned long flags;
1501         u32 tpd_next_to_use;
1502         u32 rfd_next_to_use;
1503         u32 rrd_next_to_clean;
1504         u32 value;
1505
1506         spin_lock_irqsave(&adapter->mb_lock, flags);
1507
1508         tpd_next_to_use = atomic_read(&adapter->tpd_ring.next_to_use);
1509         rfd_next_to_use = atomic_read(&adapter->rfd_ring.next_to_use);
1510         rrd_next_to_clean = atomic_read(&adapter->rrd_ring.next_to_clean);
1511
1512         value = ((rfd_next_to_use & MB_RFD_PROD_INDX_MASK) <<
1513                 MB_RFD_PROD_INDX_SHIFT) |
1514                 ((rrd_next_to_clean & MB_RRD_CONS_INDX_MASK) <<
1515                 MB_RRD_CONS_INDX_SHIFT) |
1516                 ((tpd_next_to_use & MB_TPD_PROD_INDX_MASK) <<
1517                 MB_TPD_PROD_INDX_SHIFT);
1518         iowrite32(value, adapter->hw.hw_addr + REG_MAILBOX);
1519
1520         spin_unlock_irqrestore(&adapter->mb_lock, flags);
1521 }
1522
1523 static int atl1_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1524 {
1525         struct atl1_adapter *adapter = netdev_priv(netdev);
1526         int len = skb->len;
1527         int tso;
1528         int count = 1;
1529         int ret_val;
1530         u32 val;
1531         union tpd_descr param;
1532         u16 frag_size;
1533         u16 vlan_tag;
1534         unsigned long flags;
1535         unsigned int nr_frags = 0;
1536         unsigned int mss = 0;
1537         unsigned int f;
1538         unsigned int proto_hdr_len;
1539
1540         len -= skb->data_len;
1541
1542         if (unlikely(skb->len == 0)) {
1543                 dev_kfree_skb_any(skb);
1544                 return NETDEV_TX_OK;
1545         }
1546
1547         param.data = 0;
1548         param.tso.tsopu = 0;
1549         param.tso.tsopl = 0;
1550         param.csum.csumpu = 0;
1551         param.csum.csumpl = 0;
1552
1553         /* nr_frags will be nonzero if we're doing scatter/gather (SG) */
1554         nr_frags = skb_shinfo(skb)->nr_frags;
1555         for (f = 0; f < nr_frags; f++) {
1556                 frag_size = skb_shinfo(skb)->frags[f].size;
1557                 if (frag_size)
1558                         count +=
1559                             (frag_size + MAX_TX_BUF_LEN - 1) / MAX_TX_BUF_LEN;
1560         }
1561
1562         /* mss will be nonzero if we're doing segment offload (TSO/GSO) */
1563         mss = skb_shinfo(skb)->gso_size;
1564         if (mss) {
1565                 if (skb->protocol == ntohs(ETH_P_IP)) {
1566                         proto_hdr_len = ((skb->h.raw - skb->data) +
1567                                          (skb->h.th->doff << 2));
1568                         if (unlikely(proto_hdr_len > len)) {
1569                                 dev_kfree_skb_any(skb);
1570                                 return NETDEV_TX_OK;
1571                         }
1572                         /* need additional TPD ? */
1573                         if (proto_hdr_len != len)
1574                                 count += (len - proto_hdr_len +
1575                                         MAX_TX_BUF_LEN - 1) / MAX_TX_BUF_LEN;
1576                 }
1577         }
1578
1579         local_irq_save(flags);
1580         if (!spin_trylock(&adapter->lock)) {
1581                 /* Can't get lock - tell upper layer to requeue */
1582                 local_irq_restore(flags);
1583                 printk(KERN_DEBUG "%s: TX locked\n", atl1_driver_name);
1584                 return NETDEV_TX_LOCKED;
1585         }
1586
1587         if (tpd_avail(&adapter->tpd_ring) < count) {
1588                 /* not enough descriptors */
1589                 netif_stop_queue(netdev);
1590                 spin_unlock_irqrestore(&adapter->lock, flags);
1591                 printk(KERN_DEBUG "%s: TX busy\n", atl1_driver_name);
1592                 return NETDEV_TX_BUSY;
1593         }
1594
1595         param.data = 0;
1596
1597         if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
1598                 vlan_tag = vlan_tx_tag_get(skb);
1599                 vlan_tag = (vlan_tag << 4) | (vlan_tag >> 13) |
1600                         ((vlan_tag >> 9) & 0x8);
1601                 param.csum.csumpl |= 1 << CSUM_PARAM_INSVLAG_SHIFT;
1602                 param.csum.csumpu |= (vlan_tag & CSUM_PARAM_VALANTAG_MASK) <<
1603                         CSUM_PARAM_VALAN_SHIFT;
1604         }
1605
1606         tso = atl1_tso(adapter, skb, &param.tso);
1607         if (tso < 0) {
1608                 spin_unlock_irqrestore(&adapter->lock, flags);
1609                 dev_kfree_skb_any(skb);
1610                 return NETDEV_TX_OK;
1611         }
1612
1613         if (!tso) {
1614                 ret_val = atl1_tx_csum(adapter, skb, &param.csum);
1615                 if (ret_val < 0) {
1616                         spin_unlock_irqrestore(&adapter->lock, flags);
1617                         dev_kfree_skb_any(skb);
1618                         return NETDEV_TX_OK;
1619                 }
1620         }
1621
1622         val = (param.csum.csumpl >> CSUM_PARAM_SEGMENT_SHIFT) &
1623                 CSUM_PARAM_SEGMENT_MASK;
1624         atl1_tx_map(adapter, skb, 1 == val);
1625         atl1_tx_queue(adapter, count, &param);
1626         netdev->trans_start = jiffies;
1627         spin_unlock_irqrestore(&adapter->lock, flags);
1628         atl1_update_mailbox(adapter);
1629         return NETDEV_TX_OK;
1630 }
1631
1632 /*
1633  * atl1_get_stats - Get System Network Statistics
1634  * @netdev: network interface device structure
1635  *
1636  * Returns the address of the device statistics structure.
1637  * The statistics are actually updated from the timer callback.
1638  */
1639 static struct net_device_stats *atl1_get_stats(struct net_device *netdev)
1640 {
1641         struct atl1_adapter *adapter = netdev_priv(netdev);
1642         return &adapter->net_stats;
1643 }
1644
1645 /*
1646  * atl1_clean_rx_ring - Free RFD Buffers
1647  * @adapter: board private structure
1648  */
1649 static void atl1_clean_rx_ring(struct atl1_adapter *adapter)
1650 {
1651         struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1652         struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1653         struct atl1_buffer *buffer_info;
1654         struct pci_dev *pdev = adapter->pdev;
1655         unsigned long size;
1656         unsigned int i;
1657
1658         /* Free all the Rx ring sk_buffs */
1659         for (i = 0; i < rfd_ring->count; i++) {
1660                 buffer_info = &rfd_ring->buffer_info[i];
1661                 if (buffer_info->dma) {
1662                         pci_unmap_page(pdev,
1663                                         buffer_info->dma,
1664                                         buffer_info->length,
1665                                         PCI_DMA_FROMDEVICE);
1666                         buffer_info->dma = 0;
1667                 }
1668                 if (buffer_info->skb) {
1669                         dev_kfree_skb(buffer_info->skb);
1670                         buffer_info->skb = NULL;
1671                 }
1672         }
1673
1674         size = sizeof(struct atl1_buffer) * rfd_ring->count;
1675         memset(rfd_ring->buffer_info, 0, size);
1676
1677         /* Zero out the descriptor ring */
1678         memset(rfd_ring->desc, 0, rfd_ring->size);
1679
1680         rfd_ring->next_to_clean = 0;
1681         atomic_set(&rfd_ring->next_to_use, 0);
1682
1683         rrd_ring->next_to_use = 0;
1684         atomic_set(&rrd_ring->next_to_clean, 0);
1685 }
1686
1687 /*
1688  * atl1_clean_tx_ring - Free Tx Buffers
1689  * @adapter: board private structure
1690  */
1691 static void atl1_clean_tx_ring(struct atl1_adapter *adapter)
1692 {
1693         struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1694         struct atl1_buffer *buffer_info;
1695         struct pci_dev *pdev = adapter->pdev;
1696         unsigned long size;
1697         unsigned int i;
1698
1699         /* Free all the Tx ring sk_buffs */
1700         for (i = 0; i < tpd_ring->count; i++) {
1701                 buffer_info = &tpd_ring->buffer_info[i];
1702                 if (buffer_info->dma) {
1703                         pci_unmap_page(pdev, buffer_info->dma,
1704                                        buffer_info->length, PCI_DMA_TODEVICE);
1705                         buffer_info->dma = 0;
1706                 }
1707         }
1708
1709         for (i = 0; i < tpd_ring->count; i++) {
1710                 buffer_info = &tpd_ring->buffer_info[i];
1711                 if (buffer_info->skb) {
1712                         dev_kfree_skb_any(buffer_info->skb);
1713                         buffer_info->skb = NULL;
1714                 }
1715         }
1716
1717         size = sizeof(struct atl1_buffer) * tpd_ring->count;
1718         memset(tpd_ring->buffer_info, 0, size);
1719
1720         /* Zero out the descriptor ring */
1721         memset(tpd_ring->desc, 0, tpd_ring->size);
1722
1723         atomic_set(&tpd_ring->next_to_use, 0);
1724         atomic_set(&tpd_ring->next_to_clean, 0);
1725 }
1726
1727 /*
1728  * atl1_free_ring_resources - Free Tx / RX descriptor Resources
1729  * @adapter: board private structure
1730  *
1731  * Free all transmit software resources
1732  */
1733 void atl1_free_ring_resources(struct atl1_adapter *adapter)
1734 {
1735         struct pci_dev *pdev = adapter->pdev;
1736         struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1737         struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1738         struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1739         struct atl1_ring_header *ring_header = &adapter->ring_header;
1740
1741         atl1_clean_tx_ring(adapter);
1742         atl1_clean_rx_ring(adapter);
1743
1744         kfree(tpd_ring->buffer_info);
1745         pci_free_consistent(pdev, ring_header->size, ring_header->desc,
1746                             ring_header->dma);
1747
1748         tpd_ring->buffer_info = NULL;
1749         tpd_ring->desc = NULL;
1750         tpd_ring->dma = 0;
1751
1752         rfd_ring->buffer_info = NULL;
1753         rfd_ring->desc = NULL;
1754         rfd_ring->dma = 0;
1755
1756         rrd_ring->desc = NULL;
1757         rrd_ring->dma = 0;
1758 }
1759
1760 s32 atl1_up(struct atl1_adapter *adapter)
1761 {
1762         struct net_device *netdev = adapter->netdev;
1763         int err;
1764         int irq_flags = IRQF_SAMPLE_RANDOM;
1765
1766         /* hardware has been reset, we need to reload some things */
1767         atl1_set_multi(netdev);
1768         atl1_restore_vlan(adapter);
1769         err = atl1_alloc_rx_buffers(adapter);
1770         if (unlikely(!err))             /* no RX BUFFER allocated */
1771                 return -ENOMEM;
1772
1773         if (unlikely(atl1_configure(adapter))) {
1774                 err = -EIO;
1775                 goto err_up;
1776         }
1777
1778         err = pci_enable_msi(adapter->pdev);
1779         if (err) {
1780                 dev_info(&adapter->pdev->dev,
1781                         "Unable to enable MSI: %d\n", err);
1782                 irq_flags |= IRQF_SHARED;
1783         }
1784
1785         err = request_irq(adapter->pdev->irq, &atl1_intr, irq_flags,
1786                         netdev->name, netdev);
1787         if (unlikely(err))
1788                 goto err_up;
1789
1790         mod_timer(&adapter->watchdog_timer, jiffies);
1791         atl1_irq_enable(adapter);
1792         atl1_check_link(adapter);
1793         return 0;
1794
1795         /* FIXME: unreachable code! -- CHS */
1796         /* free irq disable any interrupt */
1797         iowrite32(0, adapter->hw.hw_addr + REG_IMR);
1798         free_irq(adapter->pdev->irq, netdev);
1799
1800 err_up:
1801         pci_disable_msi(adapter->pdev);
1802         /* free rx_buffers */
1803         atl1_clean_rx_ring(adapter);
1804         return err;
1805 }
1806
1807 void atl1_down(struct atl1_adapter *adapter)
1808 {
1809         struct net_device *netdev = adapter->netdev;
1810
1811         del_timer_sync(&adapter->watchdog_timer);
1812         del_timer_sync(&adapter->phy_config_timer);
1813         adapter->phy_timer_pending = false;
1814
1815         atl1_irq_disable(adapter);
1816         free_irq(adapter->pdev->irq, netdev);
1817         pci_disable_msi(adapter->pdev);
1818         atl1_reset_hw(&adapter->hw);
1819         adapter->cmb.cmb->int_stats = 0;
1820
1821         adapter->link_speed = SPEED_0;
1822         adapter->link_duplex = -1;
1823         netif_carrier_off(netdev);
1824         netif_stop_queue(netdev);
1825
1826         atl1_clean_tx_ring(adapter);
1827         atl1_clean_rx_ring(adapter);
1828 }
1829
1830 /*
1831  * atl1_change_mtu - Change the Maximum Transfer Unit
1832  * @netdev: network interface device structure
1833  * @new_mtu: new value for maximum frame size
1834  *
1835  * Returns 0 on success, negative on failure
1836  */
1837 static int atl1_change_mtu(struct net_device *netdev, int new_mtu)
1838 {
1839         struct atl1_adapter *adapter = netdev_priv(netdev);
1840         int old_mtu = netdev->mtu;
1841         int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
1842
1843         if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
1844             (max_frame > MAX_JUMBO_FRAME_SIZE)) {
1845                 printk(KERN_WARNING "%s: invalid MTU setting\n",
1846                         atl1_driver_name);
1847                 return -EINVAL;
1848         }
1849
1850         adapter->hw.max_frame_size = max_frame;
1851         adapter->hw.tx_jumbo_task_th = (max_frame + 7) >> 3;
1852         adapter->rx_buffer_len = (max_frame + 7) & ~7;
1853         adapter->hw.rx_jumbo_th = adapter->rx_buffer_len / 8;
1854
1855         netdev->mtu = new_mtu;
1856         if ((old_mtu != new_mtu) && netif_running(netdev)) {
1857                 atl1_down(adapter);
1858                 atl1_up(adapter);
1859         }
1860
1861         return 0;
1862 }
1863
1864 /*
1865  * atl1_set_mac - Change the Ethernet Address of the NIC
1866  * @netdev: network interface device structure
1867  * @p: pointer to an address structure
1868  *
1869  * Returns 0 on success, negative on failure
1870  */
1871 static int atl1_set_mac(struct net_device *netdev, void *p)
1872 {
1873         struct atl1_adapter *adapter = netdev_priv(netdev);
1874         struct sockaddr *addr = p;
1875
1876         if (netif_running(netdev))
1877                 return -EBUSY;
1878
1879         if (!is_valid_ether_addr(addr->sa_data))
1880                 return -EADDRNOTAVAIL;
1881
1882         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1883         memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
1884
1885         atl1_set_mac_addr(&adapter->hw);
1886         return 0;
1887 }
1888
1889 /*
1890  * atl1_watchdog - Timer Call-back
1891  * @data: pointer to netdev cast into an unsigned long
1892  */
1893 static void atl1_watchdog(unsigned long data)
1894 {
1895         struct atl1_adapter *adapter = (struct atl1_adapter *)data;
1896
1897         /* Reset the timer */
1898         mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1899 }
1900
1901 static int mdio_read(struct net_device *netdev, int phy_id, int reg_num)
1902 {
1903         struct atl1_adapter *adapter = netdev_priv(netdev);
1904         u16 result;
1905
1906         atl1_read_phy_reg(&adapter->hw, reg_num & 0x1f, &result);
1907
1908         return result;
1909 }
1910
1911 static void mdio_write(struct net_device *netdev, int phy_id, int reg_num, int val)
1912 {
1913         struct atl1_adapter *adapter = netdev_priv(netdev);
1914
1915         atl1_write_phy_reg(&adapter->hw, reg_num, val);
1916 }
1917
1918 /*
1919  * atl1_mii_ioctl -
1920  * @netdev:
1921  * @ifreq:
1922  * @cmd:
1923  */
1924 static int atl1_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1925 {
1926         struct atl1_adapter *adapter = netdev_priv(netdev);
1927         unsigned long flags;
1928         int retval;
1929
1930         if (!netif_running(netdev))
1931                 return -EINVAL;
1932
1933         spin_lock_irqsave(&adapter->lock, flags);
1934         retval = generic_mii_ioctl(&adapter->mii, if_mii(ifr), cmd, NULL);
1935         spin_unlock_irqrestore(&adapter->lock, flags);
1936
1937         return retval;
1938 }
1939
1940 /*
1941  * atl1_ioctl -
1942  * @netdev:
1943  * @ifreq:
1944  * @cmd:
1945  */
1946 static int atl1_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1947 {
1948         switch (cmd) {
1949         case SIOCGMIIPHY:
1950         case SIOCGMIIREG:
1951         case SIOCSMIIREG:
1952                 return atl1_mii_ioctl(netdev, ifr, cmd);
1953         default:
1954                 return -EOPNOTSUPP;
1955         }
1956 }
1957
1958 /*
1959  * atl1_tx_timeout - Respond to a Tx Hang
1960  * @netdev: network interface device structure
1961  */
1962 static void atl1_tx_timeout(struct net_device *netdev)
1963 {
1964         struct atl1_adapter *adapter = netdev_priv(netdev);
1965         /* Do the reset outside of interrupt context */
1966         schedule_work(&adapter->tx_timeout_task);
1967 }
1968
1969 /*
1970  * atl1_phy_config - Timer Call-back
1971  * @data: pointer to netdev cast into an unsigned long
1972  */
1973 static void atl1_phy_config(unsigned long data)
1974 {
1975         struct atl1_adapter *adapter = (struct atl1_adapter *)data;
1976         struct atl1_hw *hw = &adapter->hw;
1977         unsigned long flags;
1978
1979         spin_lock_irqsave(&adapter->lock, flags);
1980         adapter->phy_timer_pending = false;
1981         atl1_write_phy_reg(hw, MII_ADVERTISE, hw->mii_autoneg_adv_reg);
1982         atl1_write_phy_reg(hw, MII_AT001_CR, hw->mii_1000t_ctrl_reg);
1983         atl1_write_phy_reg(hw, MII_BMCR, MII_CR_RESET | MII_CR_AUTO_NEG_EN);
1984         spin_unlock_irqrestore(&adapter->lock, flags);
1985 }
1986
1987 int atl1_reset(struct atl1_adapter *adapter)
1988 {
1989         int ret;
1990
1991         ret = atl1_reset_hw(&adapter->hw);
1992         if (ret != ATL1_SUCCESS)
1993                 return ret;
1994         return atl1_init_hw(&adapter->hw);
1995 }
1996
1997 /*
1998  * atl1_open - Called when a network interface is made active
1999  * @netdev: network interface device structure
2000  *
2001  * Returns 0 on success, negative value on failure
2002  *
2003  * The open entry point is called when a network interface is made
2004  * active by the system (IFF_UP).  At this point all resources needed
2005  * for transmit and receive operations are allocated, the interrupt
2006  * handler is registered with the OS, the watchdog timer is started,
2007  * and the stack is notified that the interface is ready.
2008  */
2009 static int atl1_open(struct net_device *netdev)
2010 {
2011         struct atl1_adapter *adapter = netdev_priv(netdev);
2012         int err;
2013
2014         /* allocate transmit descriptors */
2015         err = atl1_setup_ring_resources(adapter);
2016         if (err)
2017                 return err;
2018
2019         err = atl1_up(adapter);
2020         if (err)
2021                 goto err_up;
2022
2023         return 0;
2024
2025 err_up:
2026         atl1_reset(adapter);
2027         return err;
2028 }
2029
2030 /*
2031  * atl1_close - Disables a network interface
2032  * @netdev: network interface device structure
2033  *
2034  * Returns 0, this is not allowed to fail
2035  *
2036  * The close entry point is called when an interface is de-activated
2037  * by the OS.  The hardware is still under the drivers control, but
2038  * needs to be disabled.  A global MAC reset is issued to stop the
2039  * hardware, and all transmit and receive resources are freed.
2040  */
2041 static int atl1_close(struct net_device *netdev)
2042 {
2043         struct atl1_adapter *adapter = netdev_priv(netdev);
2044         atl1_down(adapter);
2045         atl1_free_ring_resources(adapter);
2046         return 0;
2047 }
2048
2049 /*
2050  * If TPD Buffer size equal to 0, PCIE DMAR_TO_INT
2051  * will assert. We do soft reset <0x1400=1> according
2052  * with the SPEC. BUT, it seemes that PCIE or DMA
2053  * state-machine will not be reset. DMAR_TO_INT will
2054  * assert again and again.
2055  */
2056 static void atl1_tx_timeout_task(struct work_struct *work)
2057 {
2058         struct atl1_adapter *adapter =
2059                 container_of(work, struct atl1_adapter, tx_timeout_task);
2060         struct net_device *netdev = adapter->netdev;
2061
2062         netif_device_detach(netdev);
2063         atl1_down(adapter);
2064         atl1_up(adapter);
2065         netif_device_attach(netdev);
2066 }
2067
2068 /*
2069  * atl1_link_chg_task - deal with link change event Out of interrupt context
2070  */
2071 static void atl1_link_chg_task(struct work_struct *work)
2072 {
2073         struct atl1_adapter *adapter =
2074                container_of(work, struct atl1_adapter, link_chg_task);
2075         unsigned long flags;
2076
2077         spin_lock_irqsave(&adapter->lock, flags);
2078         atl1_check_link(adapter);
2079         spin_unlock_irqrestore(&adapter->lock, flags);
2080 }
2081
2082 /*
2083  * atl1_pcie_patch - Patch for PCIE module
2084  */
2085 static void atl1_pcie_patch(struct atl1_adapter *adapter)
2086 {
2087         u32 value;
2088         value = 0x6500;
2089         iowrite32(value, adapter->hw.hw_addr + 0x12FC);
2090         /* pcie flow control mode change */
2091         value = ioread32(adapter->hw.hw_addr + 0x1008);
2092         value |= 0x8000;
2093         iowrite32(value, adapter->hw.hw_addr + 0x1008);
2094 }
2095
2096 /*
2097  * When ACPI resume on some VIA MotherBoard, the Interrupt Disable bit/0x400
2098  * on PCI Command register is disable.
2099  * The function enable this bit.
2100  * Brackett, 2006/03/15
2101  */
2102 static void atl1_via_workaround(struct atl1_adapter *adapter)
2103 {
2104         unsigned long value;
2105
2106         value = ioread16(adapter->hw.hw_addr + PCI_COMMAND);
2107         if (value & PCI_COMMAND_INTX_DISABLE)
2108                 value &= ~PCI_COMMAND_INTX_DISABLE;
2109         iowrite32(value, adapter->hw.hw_addr + PCI_COMMAND);
2110 }
2111
2112 /*
2113  * atl1_probe - Device Initialization Routine
2114  * @pdev: PCI device information struct
2115  * @ent: entry in atl1_pci_tbl
2116  *
2117  * Returns 0 on success, negative on failure
2118  *
2119  * atl1_probe initializes an adapter identified by a pci_dev structure.
2120  * The OS initialization, configuring of the adapter private structure,
2121  * and a hardware reset occur.
2122  */
2123 static int __devinit atl1_probe(struct pci_dev *pdev,
2124                               const struct pci_device_id *ent)
2125 {
2126         struct net_device *netdev;
2127         struct atl1_adapter *adapter;
2128         static int cards_found = 0;
2129         bool pci_using_64 = true;
2130         int err;
2131
2132         err = pci_enable_device(pdev);
2133         if (err)
2134                 return err;
2135
2136         err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
2137         if (err) {
2138                 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
2139                 if (err) {
2140                         printk(KERN_DEBUG
2141                                 "%s: no usable DMA configuration, aborting\n",
2142                                 atl1_driver_name);
2143                         goto err_dma;
2144                 }
2145                 pci_using_64 = false;
2146         }
2147         /* Mark all PCI regions associated with PCI device
2148          * pdev as being reserved by owner atl1_driver_name
2149          */
2150         err = pci_request_regions(pdev, atl1_driver_name);
2151         if (err)
2152                 goto err_request_regions;
2153
2154         /* Enables bus-mastering on the device and calls
2155          * pcibios_set_master to do the needed arch specific settings
2156          */
2157         pci_set_master(pdev);
2158
2159         netdev = alloc_etherdev(sizeof(struct atl1_adapter));
2160         if (!netdev) {
2161                 err = -ENOMEM;
2162                 goto err_alloc_etherdev;
2163         }
2164         SET_MODULE_OWNER(netdev);
2165         SET_NETDEV_DEV(netdev, &pdev->dev);
2166
2167         pci_set_drvdata(pdev, netdev);
2168         adapter = netdev_priv(netdev);
2169         adapter->netdev = netdev;
2170         adapter->pdev = pdev;
2171         adapter->hw.back = adapter;
2172
2173         adapter->hw.hw_addr = pci_iomap(pdev, 0, 0);
2174         if (!adapter->hw.hw_addr) {
2175                 err = -EIO;
2176                 goto err_pci_iomap;
2177         }
2178         /* get device revision number */
2179         adapter->hw.dev_rev = ioread16(adapter->hw.hw_addr + (REG_MASTER_CTRL + 2));
2180
2181         /* set default ring resource counts */
2182         adapter->rfd_ring.count = adapter->rrd_ring.count = ATL1_DEFAULT_RFD;
2183         adapter->tpd_ring.count = ATL1_DEFAULT_TPD;
2184
2185         adapter->mii.dev = netdev;
2186         adapter->mii.mdio_read = mdio_read;
2187         adapter->mii.mdio_write = mdio_write;
2188         adapter->mii.phy_id_mask = 0x1f;
2189         adapter->mii.reg_num_mask = 0x1f;
2190
2191         netdev->open = &atl1_open;
2192         netdev->stop = &atl1_close;
2193         netdev->hard_start_xmit = &atl1_xmit_frame;
2194         netdev->get_stats = &atl1_get_stats;
2195         netdev->set_multicast_list = &atl1_set_multi;
2196         netdev->set_mac_address = &atl1_set_mac;
2197         netdev->change_mtu = &atl1_change_mtu;
2198         netdev->do_ioctl = &atl1_ioctl;
2199         netdev->tx_timeout = &atl1_tx_timeout;
2200         netdev->watchdog_timeo = 5 * HZ;
2201         netdev->vlan_rx_register = atl1_vlan_rx_register;
2202         netdev->vlan_rx_add_vid = atl1_vlan_rx_add_vid;
2203         netdev->vlan_rx_kill_vid = atl1_vlan_rx_kill_vid;
2204         netdev->ethtool_ops = &atl1_ethtool_ops;
2205         adapter->bd_number = cards_found;
2206         adapter->pci_using_64 = pci_using_64;
2207
2208         /* setup the private structure */
2209         err = atl1_sw_init(adapter);
2210         if (err)
2211                 goto err_common;
2212
2213         netdev->features = NETIF_F_HW_CSUM;
2214         netdev->features |= NETIF_F_SG;
2215         netdev->features |= (NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX);
2216
2217         /*
2218          * FIXME - Until tso performance gets fixed, disable the feature.
2219          * Enable it with ethtool -K if desired.
2220          */
2221         /* netdev->features |= NETIF_F_TSO; */
2222
2223         if (pci_using_64)
2224                 netdev->features |= NETIF_F_HIGHDMA;
2225
2226         netdev->features |= NETIF_F_LLTX;
2227
2228         /*
2229          * patch for some L1 of old version,
2230          * the final version of L1 may not need these
2231          * patches
2232          */
2233         /* atl1_pcie_patch(adapter); */
2234
2235         /* really reset GPHY core */
2236         iowrite16(0, adapter->hw.hw_addr + REG_GPHY_ENABLE);
2237
2238         /*
2239          * reset the controller to
2240          * put the device in a known good starting state
2241          */
2242         if (atl1_reset_hw(&adapter->hw)) {
2243                 err = -EIO;
2244                 goto err_common;
2245         }
2246
2247         /* copy the MAC address out of the EEPROM */
2248         atl1_read_mac_addr(&adapter->hw);
2249         memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
2250
2251         if (!is_valid_ether_addr(netdev->dev_addr)) {
2252                 err = -EIO;
2253                 goto err_common;
2254         }
2255
2256         atl1_check_options(adapter);
2257
2258         /* pre-init the MAC, and setup link */
2259         err = atl1_init_hw(&adapter->hw);
2260         if (err) {
2261                 err = -EIO;
2262                 goto err_common;
2263         }
2264
2265         atl1_pcie_patch(adapter);
2266         /* assume we have no link for now */
2267         netif_carrier_off(netdev);
2268         netif_stop_queue(netdev);
2269
2270         init_timer(&adapter->watchdog_timer);
2271         adapter->watchdog_timer.function = &atl1_watchdog;
2272         adapter->watchdog_timer.data = (unsigned long)adapter;
2273
2274         init_timer(&adapter->phy_config_timer);
2275         adapter->phy_config_timer.function = &atl1_phy_config;
2276         adapter->phy_config_timer.data = (unsigned long)adapter;
2277         adapter->phy_timer_pending = false;
2278
2279         INIT_WORK(&adapter->tx_timeout_task, atl1_tx_timeout_task);
2280
2281         INIT_WORK(&adapter->link_chg_task, atl1_link_chg_task);
2282
2283         INIT_WORK(&adapter->pcie_dma_to_rst_task, atl1_tx_timeout_task);
2284
2285         err = register_netdev(netdev);
2286         if (err)
2287                 goto err_common;
2288
2289         cards_found++;
2290         atl1_via_workaround(adapter);
2291         return 0;
2292
2293 err_common:
2294         pci_iounmap(pdev, adapter->hw.hw_addr);
2295 err_pci_iomap:
2296         free_netdev(netdev);
2297 err_alloc_etherdev:
2298         pci_release_regions(pdev);
2299 err_dma:
2300 err_request_regions:
2301         pci_disable_device(pdev);
2302         return err;
2303 }
2304
2305 /*
2306  * atl1_remove - Device Removal Routine
2307  * @pdev: PCI device information struct
2308  *
2309  * atl1_remove is called by the PCI subsystem to alert the driver
2310  * that it should release a PCI device.  The could be caused by a
2311  * Hot-Plug event, or because the driver is going to be removed from
2312  * memory.
2313  */
2314 static void __devexit atl1_remove(struct pci_dev *pdev)
2315 {
2316         struct net_device *netdev = pci_get_drvdata(pdev);
2317         struct atl1_adapter *adapter;
2318         /* Device not available. Return. */
2319         if (!netdev)
2320                 return;
2321
2322         adapter = netdev_priv(netdev);
2323         iowrite16(0, adapter->hw.hw_addr + REG_GPHY_ENABLE);
2324         unregister_netdev(netdev);
2325         pci_iounmap(pdev, adapter->hw.hw_addr);
2326         pci_release_regions(pdev);
2327         free_netdev(netdev);
2328         pci_disable_device(pdev);
2329 }
2330
2331 #ifdef CONFIG_PM
2332 static int atl1_suspend(struct pci_dev *pdev, pm_message_t state)
2333 {
2334         struct net_device *netdev = pci_get_drvdata(pdev);
2335         struct atl1_adapter *adapter = netdev_priv(netdev);
2336         struct atl1_hw *hw = &adapter->hw;
2337         u32 ctrl = 0;
2338         u32 wufc = adapter->wol;
2339
2340         netif_device_detach(netdev);
2341         if (netif_running(netdev))
2342                 atl1_down(adapter);
2343
2344         atl1_read_phy_reg(hw, MII_BMSR, (u16 *) & ctrl);
2345         atl1_read_phy_reg(hw, MII_BMSR, (u16 *) & ctrl);
2346         if (ctrl & BMSR_LSTATUS)
2347                 wufc &= ~ATL1_WUFC_LNKC;
2348
2349         /* reduce speed to 10/100M */
2350         if (wufc) {
2351                 atl1_phy_enter_power_saving(hw);
2352                 /* if resume, let driver to re- setup link */
2353                 hw->phy_configured = false;
2354                 atl1_set_mac_addr(hw);
2355                 atl1_set_multi(netdev);
2356
2357                 ctrl = 0;
2358                 /* turn on magic packet wol */
2359                 if (wufc & ATL1_WUFC_MAG)
2360                         ctrl = WOL_MAGIC_EN | WOL_MAGIC_PME_EN;
2361
2362                 /* turn on Link change WOL */
2363                 if (wufc & ATL1_WUFC_LNKC)
2364                         ctrl |= (WOL_LINK_CHG_EN | WOL_LINK_CHG_PME_EN);
2365                 iowrite32(ctrl, hw->hw_addr + REG_WOL_CTRL);
2366
2367                 /* turn on all-multi mode if wake on multicast is enabled */
2368                 ctrl = ioread32(hw->hw_addr + REG_MAC_CTRL);
2369                 ctrl &= ~MAC_CTRL_DBG;
2370                 ctrl &= ~MAC_CTRL_PROMIS_EN;
2371                 if (wufc & ATL1_WUFC_MC)
2372                         ctrl |= MAC_CTRL_MC_ALL_EN;
2373                 else
2374                         ctrl &= ~MAC_CTRL_MC_ALL_EN;
2375
2376                 /* turn on broadcast mode if wake on-BC is enabled */
2377                 if (wufc & ATL1_WUFC_BC)
2378                         ctrl |= MAC_CTRL_BC_EN;
2379                 else
2380                         ctrl &= ~MAC_CTRL_BC_EN;
2381
2382                 /* enable RX */
2383                 ctrl |= MAC_CTRL_RX_EN;
2384                 iowrite32(ctrl, hw->hw_addr + REG_MAC_CTRL);
2385                 pci_enable_wake(pdev, PCI_D3hot, 1);
2386                 pci_enable_wake(pdev, PCI_D3cold, 1);   /* 4 == D3 cold */
2387         } else {
2388                 iowrite32(0, hw->hw_addr + REG_WOL_CTRL);
2389                 pci_enable_wake(pdev, PCI_D3hot, 0);
2390                 pci_enable_wake(pdev, PCI_D3cold, 0);   /* 4 == D3 cold */
2391         }
2392
2393         pci_save_state(pdev);
2394         pci_disable_device(pdev);
2395
2396         pci_set_power_state(pdev, PCI_D3hot);
2397
2398         return 0;
2399 }
2400
2401 static int atl1_resume(struct pci_dev *pdev)
2402 {
2403         struct net_device *netdev = pci_get_drvdata(pdev);
2404         struct atl1_adapter *adapter = netdev_priv(netdev);
2405         u32 ret_val;
2406
2407         pci_set_power_state(pdev, 0);
2408         pci_restore_state(pdev);
2409
2410         ret_val = pci_enable_device(pdev);
2411         pci_enable_wake(pdev, PCI_D3hot, 0);
2412         pci_enable_wake(pdev, PCI_D3cold, 0);
2413
2414         iowrite32(0, adapter->hw.hw_addr + REG_WOL_CTRL);
2415         atl1_reset(adapter);
2416
2417         if (netif_running(netdev))
2418                 atl1_up(adapter);
2419         netif_device_attach(netdev);
2420
2421         atl1_via_workaround(adapter);
2422
2423         return 0;
2424 }
2425 #else
2426 #define atl1_suspend NULL
2427 #define atl1_resume NULL
2428 #endif
2429
2430 static struct pci_driver atl1_driver = {
2431         .name = atl1_driver_name,
2432         .id_table = atl1_pci_tbl,
2433         .probe = atl1_probe,
2434         .remove = __devexit_p(atl1_remove),
2435         /* Power Managment Hooks */
2436         /* probably broken right now -- CHS */
2437         .suspend = atl1_suspend,
2438         .resume = atl1_resume
2439 };
2440
2441 /*
2442  * atl1_exit_module - Driver Exit Cleanup Routine
2443  *
2444  * atl1_exit_module is called just before the driver is removed
2445  * from memory.
2446  */
2447 static void __exit atl1_exit_module(void)
2448 {
2449         pci_unregister_driver(&atl1_driver);
2450 }
2451
2452 /*
2453  * atl1_init_module - Driver Registration Routine
2454  *
2455  * atl1_init_module is the first routine called when the driver is
2456  * loaded. All it does is register with the PCI subsystem.
2457  */
2458 static int __init atl1_init_module(void)
2459 {
2460         printk(KERN_INFO "%s - version %s\n", atl1_driver_string, DRIVER_VERSION);
2461         printk(KERN_INFO "%s\n", atl1_copyright);
2462         return pci_register_driver(&atl1_driver);
2463 }
2464
2465 module_init(atl1_init_module);
2466 module_exit(atl1_exit_module);