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