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