Merge branch 'linus' into test
[linux-2.6] / drivers / net / atlx / atl1.c
1 /*
2  * Copyright(c) 2005 - 2006 Attansic Corporation. All rights reserved.
3  * Copyright(c) 2006 - 2007 Chris Snook <csnook@redhat.com>
4  * Copyright(c) 2006 - 2008 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@atheros.com>
28  * Jie Yang <jie.yang@atheros.com>
29  * Chris Snook <csnook@redhat.com>
30  * Jay Cliburn <jcliburn@gmail.com>
31  *
32  * This version is adapted from the Attansic reference driver.
33  *
34  * TODO:
35  * Add more ethtool functions.
36  * Fix abstruse irq enable/disable condition described here:
37  *      http://marc.theaimsgroup.com/?l=linux-netdev&m=116398508500553&w=2
38  *
39  * NEEDS TESTING:
40  * VLAN
41  * multicast
42  * promiscuous mode
43  * interrupt coalescing
44  * SMP torture testing
45  */
46
47 #include <asm/atomic.h>
48 #include <asm/byteorder.h>
49
50 #include <linux/compiler.h>
51 #include <linux/crc32.h>
52 #include <linux/delay.h>
53 #include <linux/dma-mapping.h>
54 #include <linux/etherdevice.h>
55 #include <linux/hardirq.h>
56 #include <linux/if_ether.h>
57 #include <linux/if_vlan.h>
58 #include <linux/in.h>
59 #include <linux/interrupt.h>
60 #include <linux/ip.h>
61 #include <linux/irqflags.h>
62 #include <linux/irqreturn.h>
63 #include <linux/jiffies.h>
64 #include <linux/mii.h>
65 #include <linux/module.h>
66 #include <linux/moduleparam.h>
67 #include <linux/net.h>
68 #include <linux/netdevice.h>
69 #include <linux/pci.h>
70 #include <linux/pci_ids.h>
71 #include <linux/pm.h>
72 #include <linux/skbuff.h>
73 #include <linux/slab.h>
74 #include <linux/spinlock.h>
75 #include <linux/string.h>
76 #include <linux/tcp.h>
77 #include <linux/timer.h>
78 #include <linux/types.h>
79 #include <linux/workqueue.h>
80
81 #include <net/checksum.h>
82
83 #include "atl1.h"
84
85 /* Temporary hack for merging atl1 and atl2 */
86 #include "atlx.c"
87
88 /*
89  * This is the only thing that needs to be changed to adjust the
90  * maximum number of ports that the driver can manage.
91  */
92 #define ATL1_MAX_NIC 4
93
94 #define OPTION_UNSET    -1
95 #define OPTION_DISABLED 0
96 #define OPTION_ENABLED  1
97
98 #define ATL1_PARAM_INIT { [0 ... ATL1_MAX_NIC] = OPTION_UNSET }
99
100 /*
101  * Interrupt Moderate Timer in units of 2 us
102  *
103  * Valid Range: 10-65535
104  *
105  * Default Value: 100 (200us)
106  */
107 static int __devinitdata int_mod_timer[ATL1_MAX_NIC+1] = ATL1_PARAM_INIT;
108 static int num_int_mod_timer;
109 module_param_array_named(int_mod_timer, int_mod_timer, int,
110         &num_int_mod_timer, 0);
111 MODULE_PARM_DESC(int_mod_timer, "Interrupt moderator timer");
112
113 #define DEFAULT_INT_MOD_CNT     100     /* 200us */
114 #define MAX_INT_MOD_CNT         65000
115 #define MIN_INT_MOD_CNT         50
116
117 struct atl1_option {
118         enum { enable_option, range_option, list_option } type;
119         char *name;
120         char *err;
121         int def;
122         union {
123                 struct {        /* range_option info */
124                         int min;
125                         int max;
126                 } r;
127                 struct {        /* list_option info */
128                         int nr;
129                         struct atl1_opt_list {
130                                 int i;
131                                 char *str;
132                         } *p;
133                 } l;
134         } arg;
135 };
136
137 static int __devinit atl1_validate_option(int *value, struct atl1_option *opt,
138         struct pci_dev *pdev)
139 {
140         if (*value == OPTION_UNSET) {
141                 *value = opt->def;
142                 return 0;
143         }
144
145         switch (opt->type) {
146         case enable_option:
147                 switch (*value) {
148                 case OPTION_ENABLED:
149                         dev_info(&pdev->dev, "%s enabled\n", opt->name);
150                         return 0;
151                 case OPTION_DISABLED:
152                         dev_info(&pdev->dev, "%s disabled\n", opt->name);
153                         return 0;
154                 }
155                 break;
156         case range_option:
157                 if (*value >= opt->arg.r.min && *value <= opt->arg.r.max) {
158                         dev_info(&pdev->dev, "%s set to %i\n", opt->name,
159                                 *value);
160                         return 0;
161                 }
162                 break;
163         case list_option:{
164                         int i;
165                         struct atl1_opt_list *ent;
166
167                         for (i = 0; i < opt->arg.l.nr; i++) {
168                                 ent = &opt->arg.l.p[i];
169                                 if (*value == ent->i) {
170                                         if (ent->str[0] != '\0')
171                                                 dev_info(&pdev->dev, "%s\n",
172                                                         ent->str);
173                                         return 0;
174                                 }
175                         }
176                 }
177                 break;
178
179         default:
180                 break;
181         }
182
183         dev_info(&pdev->dev, "invalid %s specified (%i) %s\n",
184                 opt->name, *value, opt->err);
185         *value = opt->def;
186         return -1;
187 }
188
189 /*
190  * atl1_check_options - Range Checking for Command Line Parameters
191  * @adapter: board private structure
192  *
193  * This routine checks all command line parameters for valid user
194  * input.  If an invalid value is given, or if no user specified
195  * value exists, a default value is used.  The final value is stored
196  * in a variable in the adapter structure.
197  */
198 void __devinit atl1_check_options(struct atl1_adapter *adapter)
199 {
200         struct pci_dev *pdev = adapter->pdev;
201         int bd = adapter->bd_number;
202         if (bd >= ATL1_MAX_NIC) {
203                 dev_notice(&pdev->dev, "no configuration for board#%i\n", bd);
204                 dev_notice(&pdev->dev, "using defaults for all values\n");
205         }
206         {                       /* Interrupt Moderate Timer */
207                 struct atl1_option opt = {
208                         .type = range_option,
209                         .name = "Interrupt Moderator Timer",
210                         .err = "using default of "
211                                 __MODULE_STRING(DEFAULT_INT_MOD_CNT),
212                         .def = DEFAULT_INT_MOD_CNT,
213                         .arg = {.r = {.min = MIN_INT_MOD_CNT,
214                                         .max = MAX_INT_MOD_CNT} }
215                 };
216                 int val;
217                 if (num_int_mod_timer > bd) {
218                         val = int_mod_timer[bd];
219                         atl1_validate_option(&val, &opt, pdev);
220                         adapter->imt = (u16) val;
221                 } else
222                         adapter->imt = (u16) (opt.def);
223         }
224 }
225
226 /*
227  * atl1_pci_tbl - PCI Device ID Table
228  */
229 static const struct pci_device_id atl1_pci_tbl[] = {
230         {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L1)},
231         /* required last entry */
232         {0,}
233 };
234 MODULE_DEVICE_TABLE(pci, atl1_pci_tbl);
235
236 static const u32 atl1_default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
237         NETIF_MSG_LINK | NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP;
238
239 static int debug = -1;
240 module_param(debug, int, 0);
241 MODULE_PARM_DESC(debug, "Message level (0=none,...,16=all)");
242
243 /*
244  * Reset the transmit and receive units; mask and clear all interrupts.
245  * hw - Struct containing variables accessed by shared code
246  * return : 0  or  idle status (if error)
247  */
248 static s32 atl1_reset_hw(struct atl1_hw *hw)
249 {
250         struct pci_dev *pdev = hw->back->pdev;
251         struct atl1_adapter *adapter = hw->back;
252         u32 icr;
253         int i;
254
255         /*
256          * Clear Interrupt mask to stop board from generating
257          * interrupts & Clear any pending interrupt events
258          */
259         /*
260          * iowrite32(0, hw->hw_addr + REG_IMR);
261          * iowrite32(0xffffffff, hw->hw_addr + REG_ISR);
262          */
263
264         /*
265          * Issue Soft Reset to the MAC.  This will reset the chip's
266          * transmit, receive, DMA.  It will not effect
267          * the current PCI configuration.  The global reset bit is self-
268          * clearing, and should clear within a microsecond.
269          */
270         iowrite32(MASTER_CTRL_SOFT_RST, hw->hw_addr + REG_MASTER_CTRL);
271         ioread32(hw->hw_addr + REG_MASTER_CTRL);
272
273         iowrite16(1, hw->hw_addr + REG_PHY_ENABLE);
274         ioread16(hw->hw_addr + REG_PHY_ENABLE);
275
276         /* delay about 1ms */
277         msleep(1);
278
279         /* Wait at least 10ms for All module to be Idle */
280         for (i = 0; i < 10; i++) {
281                 icr = ioread32(hw->hw_addr + REG_IDLE_STATUS);
282                 if (!icr)
283                         break;
284                 /* delay 1 ms */
285                 msleep(1);
286                 /* FIXME: still the right way to do this? */
287                 cpu_relax();
288         }
289
290         if (icr) {
291                 if (netif_msg_hw(adapter))
292                         dev_dbg(&pdev->dev, "ICR = 0x%x\n", icr);
293                 return icr;
294         }
295
296         return 0;
297 }
298
299 /* function about EEPROM
300  *
301  * check_eeprom_exist
302  * return 0 if eeprom exist
303  */
304 static int atl1_check_eeprom_exist(struct atl1_hw *hw)
305 {
306         u32 value;
307         value = ioread32(hw->hw_addr + REG_SPI_FLASH_CTRL);
308         if (value & SPI_FLASH_CTRL_EN_VPD) {
309                 value &= ~SPI_FLASH_CTRL_EN_VPD;
310                 iowrite32(value, hw->hw_addr + REG_SPI_FLASH_CTRL);
311         }
312
313         value = ioread16(hw->hw_addr + REG_PCIE_CAP_LIST);
314         return ((value & 0xFF00) == 0x6C00) ? 0 : 1;
315 }
316
317 static bool atl1_read_eeprom(struct atl1_hw *hw, u32 offset, u32 *p_value)
318 {
319         int i;
320         u32 control;
321
322         if (offset & 3)
323                 /* address do not align */
324                 return false;
325
326         iowrite32(0, hw->hw_addr + REG_VPD_DATA);
327         control = (offset & VPD_CAP_VPD_ADDR_MASK) << VPD_CAP_VPD_ADDR_SHIFT;
328         iowrite32(control, hw->hw_addr + REG_VPD_CAP);
329         ioread32(hw->hw_addr + REG_VPD_CAP);
330
331         for (i = 0; i < 10; i++) {
332                 msleep(2);
333                 control = ioread32(hw->hw_addr + REG_VPD_CAP);
334                 if (control & VPD_CAP_VPD_FLAG)
335                         break;
336         }
337         if (control & VPD_CAP_VPD_FLAG) {
338                 *p_value = ioread32(hw->hw_addr + REG_VPD_DATA);
339                 return true;
340         }
341         /* timeout */
342         return false;
343 }
344
345 /*
346  * Reads the value from a PHY register
347  * hw - Struct containing variables accessed by shared code
348  * reg_addr - address of the PHY register to read
349  */
350 s32 atl1_read_phy_reg(struct atl1_hw *hw, u16 reg_addr, u16 *phy_data)
351 {
352         u32 val;
353         int i;
354
355         val = ((u32) (reg_addr & MDIO_REG_ADDR_MASK)) << MDIO_REG_ADDR_SHIFT |
356                 MDIO_START | MDIO_SUP_PREAMBLE | MDIO_RW | MDIO_CLK_25_4 <<
357                 MDIO_CLK_SEL_SHIFT;
358         iowrite32(val, hw->hw_addr + REG_MDIO_CTRL);
359         ioread32(hw->hw_addr + REG_MDIO_CTRL);
360
361         for (i = 0; i < MDIO_WAIT_TIMES; i++) {
362                 udelay(2);
363                 val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
364                 if (!(val & (MDIO_START | MDIO_BUSY)))
365                         break;
366         }
367         if (!(val & (MDIO_START | MDIO_BUSY))) {
368                 *phy_data = (u16) val;
369                 return 0;
370         }
371         return ATLX_ERR_PHY;
372 }
373
374 #define CUSTOM_SPI_CS_SETUP     2
375 #define CUSTOM_SPI_CLK_HI       2
376 #define CUSTOM_SPI_CLK_LO       2
377 #define CUSTOM_SPI_CS_HOLD      2
378 #define CUSTOM_SPI_CS_HI        3
379
380 static bool atl1_spi_read(struct atl1_hw *hw, u32 addr, u32 *buf)
381 {
382         int i;
383         u32 value;
384
385         iowrite32(0, hw->hw_addr + REG_SPI_DATA);
386         iowrite32(addr, hw->hw_addr + REG_SPI_ADDR);
387
388         value = SPI_FLASH_CTRL_WAIT_READY |
389             (CUSTOM_SPI_CS_SETUP & SPI_FLASH_CTRL_CS_SETUP_MASK) <<
390             SPI_FLASH_CTRL_CS_SETUP_SHIFT | (CUSTOM_SPI_CLK_HI &
391                                              SPI_FLASH_CTRL_CLK_HI_MASK) <<
392             SPI_FLASH_CTRL_CLK_HI_SHIFT | (CUSTOM_SPI_CLK_LO &
393                                            SPI_FLASH_CTRL_CLK_LO_MASK) <<
394             SPI_FLASH_CTRL_CLK_LO_SHIFT | (CUSTOM_SPI_CS_HOLD &
395                                            SPI_FLASH_CTRL_CS_HOLD_MASK) <<
396             SPI_FLASH_CTRL_CS_HOLD_SHIFT | (CUSTOM_SPI_CS_HI &
397                                             SPI_FLASH_CTRL_CS_HI_MASK) <<
398             SPI_FLASH_CTRL_CS_HI_SHIFT | (1 & SPI_FLASH_CTRL_INS_MASK) <<
399             SPI_FLASH_CTRL_INS_SHIFT;
400
401         iowrite32(value, hw->hw_addr + REG_SPI_FLASH_CTRL);
402
403         value |= SPI_FLASH_CTRL_START;
404         iowrite32(value, hw->hw_addr + REG_SPI_FLASH_CTRL);
405         ioread32(hw->hw_addr + REG_SPI_FLASH_CTRL);
406
407         for (i = 0; i < 10; i++) {
408                 msleep(1);
409                 value = ioread32(hw->hw_addr + REG_SPI_FLASH_CTRL);
410                 if (!(value & SPI_FLASH_CTRL_START))
411                         break;
412         }
413
414         if (value & SPI_FLASH_CTRL_START)
415                 return false;
416
417         *buf = ioread32(hw->hw_addr + REG_SPI_DATA);
418
419         return true;
420 }
421
422 /*
423  * get_permanent_address
424  * return 0 if get valid mac address,
425  */
426 static int atl1_get_permanent_address(struct atl1_hw *hw)
427 {
428         u32 addr[2];
429         u32 i, control;
430         u16 reg;
431         u8 eth_addr[ETH_ALEN];
432         bool key_valid;
433
434         if (is_valid_ether_addr(hw->perm_mac_addr))
435                 return 0;
436
437         /* init */
438         addr[0] = addr[1] = 0;
439
440         if (!atl1_check_eeprom_exist(hw)) {
441                 reg = 0;
442                 key_valid = false;
443                 /* Read out all EEPROM content */
444                 i = 0;
445                 while (1) {
446                         if (atl1_read_eeprom(hw, i + 0x100, &control)) {
447                                 if (key_valid) {
448                                         if (reg == REG_MAC_STA_ADDR)
449                                                 addr[0] = control;
450                                         else if (reg == (REG_MAC_STA_ADDR + 4))
451                                                 addr[1] = control;
452                                         key_valid = false;
453                                 } else if ((control & 0xff) == 0x5A) {
454                                         key_valid = true;
455                                         reg = (u16) (control >> 16);
456                                 } else
457                                         break;
458                         } else
459                                 /* read error */
460                                 break;
461                         i += 4;
462                 }
463
464                 *(u32 *) &eth_addr[2] = swab32(addr[0]);
465                 *(u16 *) &eth_addr[0] = swab16(*(u16 *) &addr[1]);
466                 if (is_valid_ether_addr(eth_addr)) {
467                         memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
468                         return 0;
469                 }
470         }
471
472         /* see if SPI FLAGS exist ? */
473         addr[0] = addr[1] = 0;
474         reg = 0;
475         key_valid = false;
476         i = 0;
477         while (1) {
478                 if (atl1_spi_read(hw, i + 0x1f000, &control)) {
479                         if (key_valid) {
480                                 if (reg == REG_MAC_STA_ADDR)
481                                         addr[0] = control;
482                                 else if (reg == (REG_MAC_STA_ADDR + 4))
483                                         addr[1] = control;
484                                 key_valid = false;
485                         } else if ((control & 0xff) == 0x5A) {
486                                 key_valid = true;
487                                 reg = (u16) (control >> 16);
488                         } else
489                                 /* data end */
490                                 break;
491                 } else
492                         /* read error */
493                         break;
494                 i += 4;
495         }
496
497         *(u32 *) &eth_addr[2] = swab32(addr[0]);
498         *(u16 *) &eth_addr[0] = swab16(*(u16 *) &addr[1]);
499         if (is_valid_ether_addr(eth_addr)) {
500                 memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
501                 return 0;
502         }
503
504         /*
505          * On some motherboards, the MAC address is written by the
506          * BIOS directly to the MAC register during POST, and is
507          * not stored in eeprom.  If all else thus far has failed
508          * to fetch the permanent MAC address, try reading it directly.
509          */
510         addr[0] = ioread32(hw->hw_addr + REG_MAC_STA_ADDR);
511         addr[1] = ioread16(hw->hw_addr + (REG_MAC_STA_ADDR + 4));
512         *(u32 *) &eth_addr[2] = swab32(addr[0]);
513         *(u16 *) &eth_addr[0] = swab16(*(u16 *) &addr[1]);
514         if (is_valid_ether_addr(eth_addr)) {
515                 memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
516                 return 0;
517         }
518
519         return 1;
520 }
521
522 /*
523  * Reads the adapter's MAC address from the EEPROM
524  * hw - Struct containing variables accessed by shared code
525  */
526 s32 atl1_read_mac_addr(struct atl1_hw *hw)
527 {
528         u16 i;
529
530         if (atl1_get_permanent_address(hw))
531                 random_ether_addr(hw->perm_mac_addr);
532
533         for (i = 0; i < ETH_ALEN; i++)
534                 hw->mac_addr[i] = hw->perm_mac_addr[i];
535         return 0;
536 }
537
538 /*
539  * Hashes an address to determine its location in the multicast table
540  * hw - Struct containing variables accessed by shared code
541  * mc_addr - the multicast address to hash
542  *
543  * atl1_hash_mc_addr
544  *  purpose
545  *      set hash value for a multicast address
546  *      hash calcu processing :
547  *          1. calcu 32bit CRC for multicast address
548  *          2. reverse crc with MSB to LSB
549  */
550 u32 atl1_hash_mc_addr(struct atl1_hw *hw, u8 *mc_addr)
551 {
552         u32 crc32, value = 0;
553         int i;
554
555         crc32 = ether_crc_le(6, mc_addr);
556         for (i = 0; i < 32; i++)
557                 value |= (((crc32 >> i) & 1) << (31 - i));
558
559         return value;
560 }
561
562 /*
563  * Sets the bit in the multicast table corresponding to the hash value.
564  * hw - Struct containing variables accessed by shared code
565  * hash_value - Multicast address hash value
566  */
567 void atl1_hash_set(struct atl1_hw *hw, u32 hash_value)
568 {
569         u32 hash_bit, hash_reg;
570         u32 mta;
571
572         /*
573          * The HASH Table  is a register array of 2 32-bit registers.
574          * It is treated like an array of 64 bits.  We want to set
575          * bit BitArray[hash_value]. So we figure out what register
576          * the bit is in, read it, OR in the new bit, then write
577          * back the new value.  The register is determined by the
578          * upper 7 bits of the hash value and the bit within that
579          * register are determined by the lower 5 bits of the value.
580          */
581         hash_reg = (hash_value >> 31) & 0x1;
582         hash_bit = (hash_value >> 26) & 0x1F;
583         mta = ioread32((hw->hw_addr + REG_RX_HASH_TABLE) + (hash_reg << 2));
584         mta |= (1 << hash_bit);
585         iowrite32(mta, (hw->hw_addr + REG_RX_HASH_TABLE) + (hash_reg << 2));
586 }
587
588 /*
589  * Writes a value to a PHY register
590  * hw - Struct containing variables accessed by shared code
591  * reg_addr - address of the PHY register to write
592  * data - data to write to the PHY
593  */
594 static s32 atl1_write_phy_reg(struct atl1_hw *hw, u32 reg_addr, u16 phy_data)
595 {
596         int i;
597         u32 val;
598
599         val = ((u32) (phy_data & MDIO_DATA_MASK)) << MDIO_DATA_SHIFT |
600             (reg_addr & MDIO_REG_ADDR_MASK) << MDIO_REG_ADDR_SHIFT |
601             MDIO_SUP_PREAMBLE |
602             MDIO_START | MDIO_CLK_25_4 << MDIO_CLK_SEL_SHIFT;
603         iowrite32(val, hw->hw_addr + REG_MDIO_CTRL);
604         ioread32(hw->hw_addr + REG_MDIO_CTRL);
605
606         for (i = 0; i < MDIO_WAIT_TIMES; i++) {
607                 udelay(2);
608                 val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
609                 if (!(val & (MDIO_START | MDIO_BUSY)))
610                         break;
611         }
612
613         if (!(val & (MDIO_START | MDIO_BUSY)))
614                 return 0;
615
616         return ATLX_ERR_PHY;
617 }
618
619 /*
620  * Make L001's PHY out of Power Saving State (bug)
621  * hw - Struct containing variables accessed by shared code
622  * when power on, L001's PHY always on Power saving State
623  * (Gigabit Link forbidden)
624  */
625 static s32 atl1_phy_leave_power_saving(struct atl1_hw *hw)
626 {
627         s32 ret;
628         ret = atl1_write_phy_reg(hw, 29, 0x0029);
629         if (ret)
630                 return ret;
631         return atl1_write_phy_reg(hw, 30, 0);
632 }
633
634 /*
635  * Resets the PHY and make all config validate
636  * hw - Struct containing variables accessed by shared code
637  *
638  * Sets bit 15 and 12 of the MII Control regiser (for F001 bug)
639  */
640 static s32 atl1_phy_reset(struct atl1_hw *hw)
641 {
642         struct pci_dev *pdev = hw->back->pdev;
643         struct atl1_adapter *adapter = hw->back;
644         s32 ret_val;
645         u16 phy_data;
646
647         if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
648             hw->media_type == MEDIA_TYPE_1000M_FULL)
649                 phy_data = MII_CR_RESET | MII_CR_AUTO_NEG_EN;
650         else {
651                 switch (hw->media_type) {
652                 case MEDIA_TYPE_100M_FULL:
653                         phy_data =
654                             MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
655                             MII_CR_RESET;
656                         break;
657                 case MEDIA_TYPE_100M_HALF:
658                         phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
659                         break;
660                 case MEDIA_TYPE_10M_FULL:
661                         phy_data =
662                             MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
663                         break;
664                 default:
665                         /* MEDIA_TYPE_10M_HALF: */
666                         phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
667                         break;
668                 }
669         }
670
671         ret_val = atl1_write_phy_reg(hw, MII_BMCR, phy_data);
672         if (ret_val) {
673                 u32 val;
674                 int i;
675                 /* pcie serdes link may be down! */
676                 if (netif_msg_hw(adapter))
677                         dev_dbg(&pdev->dev, "pcie phy link down\n");
678
679                 for (i = 0; i < 25; i++) {
680                         msleep(1);
681                         val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
682                         if (!(val & (MDIO_START | MDIO_BUSY)))
683                                 break;
684                 }
685
686                 if ((val & (MDIO_START | MDIO_BUSY)) != 0) {
687                         if (netif_msg_hw(adapter))
688                                 dev_warn(&pdev->dev,
689                                         "pcie link down at least 25ms\n");
690                         return ret_val;
691                 }
692         }
693         return 0;
694 }
695
696 /*
697  * Configures PHY autoneg and flow control advertisement settings
698  * hw - Struct containing variables accessed by shared code
699  */
700 static s32 atl1_phy_setup_autoneg_adv(struct atl1_hw *hw)
701 {
702         s32 ret_val;
703         s16 mii_autoneg_adv_reg;
704         s16 mii_1000t_ctrl_reg;
705
706         /* Read the MII Auto-Neg Advertisement Register (Address 4). */
707         mii_autoneg_adv_reg = MII_AR_DEFAULT_CAP_MASK;
708
709         /* Read the MII 1000Base-T Control Register (Address 9). */
710         mii_1000t_ctrl_reg = MII_ATLX_CR_1000T_DEFAULT_CAP_MASK;
711
712         /*
713          * First we clear all the 10/100 mb speed bits in the Auto-Neg
714          * Advertisement Register (Address 4) and the 1000 mb speed bits in
715          * the  1000Base-T Control Register (Address 9).
716          */
717         mii_autoneg_adv_reg &= ~MII_AR_SPEED_MASK;
718         mii_1000t_ctrl_reg &= ~MII_ATLX_CR_1000T_SPEED_MASK;
719
720         /*
721          * Need to parse media_type  and set up
722          * the appropriate PHY registers.
723          */
724         switch (hw->media_type) {
725         case MEDIA_TYPE_AUTO_SENSOR:
726                 mii_autoneg_adv_reg |= (MII_AR_10T_HD_CAPS |
727                                         MII_AR_10T_FD_CAPS |
728                                         MII_AR_100TX_HD_CAPS |
729                                         MII_AR_100TX_FD_CAPS);
730                 mii_1000t_ctrl_reg |= MII_ATLX_CR_1000T_FD_CAPS;
731                 break;
732
733         case MEDIA_TYPE_1000M_FULL:
734                 mii_1000t_ctrl_reg |= MII_ATLX_CR_1000T_FD_CAPS;
735                 break;
736
737         case MEDIA_TYPE_100M_FULL:
738                 mii_autoneg_adv_reg |= MII_AR_100TX_FD_CAPS;
739                 break;
740
741         case MEDIA_TYPE_100M_HALF:
742                 mii_autoneg_adv_reg |= MII_AR_100TX_HD_CAPS;
743                 break;
744
745         case MEDIA_TYPE_10M_FULL:
746                 mii_autoneg_adv_reg |= MII_AR_10T_FD_CAPS;
747                 break;
748
749         default:
750                 mii_autoneg_adv_reg |= MII_AR_10T_HD_CAPS;
751                 break;
752         }
753
754         /* flow control fixed to enable all */
755         mii_autoneg_adv_reg |= (MII_AR_ASM_DIR | MII_AR_PAUSE);
756
757         hw->mii_autoneg_adv_reg = mii_autoneg_adv_reg;
758         hw->mii_1000t_ctrl_reg = mii_1000t_ctrl_reg;
759
760         ret_val = atl1_write_phy_reg(hw, MII_ADVERTISE, mii_autoneg_adv_reg);
761         if (ret_val)
762                 return ret_val;
763
764         ret_val = atl1_write_phy_reg(hw, MII_ATLX_CR, mii_1000t_ctrl_reg);
765         if (ret_val)
766                 return ret_val;
767
768         return 0;
769 }
770
771 /*
772  * Configures link settings.
773  * hw - Struct containing variables accessed by shared code
774  * Assumes the hardware has previously been reset and the
775  * transmitter and receiver are not enabled.
776  */
777 static s32 atl1_setup_link(struct atl1_hw *hw)
778 {
779         struct pci_dev *pdev = hw->back->pdev;
780         struct atl1_adapter *adapter = hw->back;
781         s32 ret_val;
782
783         /*
784          * Options:
785          *  PHY will advertise value(s) parsed from
786          *  autoneg_advertised and fc
787          *  no matter what autoneg is , We will not wait link result.
788          */
789         ret_val = atl1_phy_setup_autoneg_adv(hw);
790         if (ret_val) {
791                 if (netif_msg_link(adapter))
792                         dev_dbg(&pdev->dev,
793                                 "error setting up autonegotiation\n");
794                 return ret_val;
795         }
796         /* SW.Reset , En-Auto-Neg if needed */
797         ret_val = atl1_phy_reset(hw);
798         if (ret_val) {
799                 if (netif_msg_link(adapter))
800                         dev_dbg(&pdev->dev, "error resetting phy\n");
801                 return ret_val;
802         }
803         hw->phy_configured = true;
804         return ret_val;
805 }
806
807 static void atl1_init_flash_opcode(struct atl1_hw *hw)
808 {
809         if (hw->flash_vendor >= ARRAY_SIZE(flash_table))
810                 /* Atmel */
811                 hw->flash_vendor = 0;
812
813         /* Init OP table */
814         iowrite8(flash_table[hw->flash_vendor].cmd_program,
815                 hw->hw_addr + REG_SPI_FLASH_OP_PROGRAM);
816         iowrite8(flash_table[hw->flash_vendor].cmd_sector_erase,
817                 hw->hw_addr + REG_SPI_FLASH_OP_SC_ERASE);
818         iowrite8(flash_table[hw->flash_vendor].cmd_chip_erase,
819                 hw->hw_addr + REG_SPI_FLASH_OP_CHIP_ERASE);
820         iowrite8(flash_table[hw->flash_vendor].cmd_rdid,
821                 hw->hw_addr + REG_SPI_FLASH_OP_RDID);
822         iowrite8(flash_table[hw->flash_vendor].cmd_wren,
823                 hw->hw_addr + REG_SPI_FLASH_OP_WREN);
824         iowrite8(flash_table[hw->flash_vendor].cmd_rdsr,
825                 hw->hw_addr + REG_SPI_FLASH_OP_RDSR);
826         iowrite8(flash_table[hw->flash_vendor].cmd_wrsr,
827                 hw->hw_addr + REG_SPI_FLASH_OP_WRSR);
828         iowrite8(flash_table[hw->flash_vendor].cmd_read,
829                 hw->hw_addr + REG_SPI_FLASH_OP_READ);
830 }
831
832 /*
833  * Performs basic configuration of the adapter.
834  * hw - Struct containing variables accessed by shared code
835  * Assumes that the controller has previously been reset and is in a
836  * post-reset uninitialized state. Initializes multicast table,
837  * and  Calls routines to setup link
838  * Leaves the transmit and receive units disabled and uninitialized.
839  */
840 static s32 atl1_init_hw(struct atl1_hw *hw)
841 {
842         u32 ret_val = 0;
843
844         /* Zero out the Multicast HASH table */
845         iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE);
846         /* clear the old settings from the multicast hash table */
847         iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2));
848
849         atl1_init_flash_opcode(hw);
850
851         if (!hw->phy_configured) {
852                 /* enable GPHY LinkChange Interrrupt */
853                 ret_val = atl1_write_phy_reg(hw, 18, 0xC00);
854                 if (ret_val)
855                         return ret_val;
856                 /* make PHY out of power-saving state */
857                 ret_val = atl1_phy_leave_power_saving(hw);
858                 if (ret_val)
859                         return ret_val;
860                 /* Call a subroutine to configure the link */
861                 ret_val = atl1_setup_link(hw);
862         }
863         return ret_val;
864 }
865
866 /*
867  * Detects the current speed and duplex settings of the hardware.
868  * hw - Struct containing variables accessed by shared code
869  * speed - Speed of the connection
870  * duplex - Duplex setting of the connection
871  */
872 static s32 atl1_get_speed_and_duplex(struct atl1_hw *hw, u16 *speed, u16 *duplex)
873 {
874         struct pci_dev *pdev = hw->back->pdev;
875         struct atl1_adapter *adapter = hw->back;
876         s32 ret_val;
877         u16 phy_data;
878
879         /* ; --- Read   PHY Specific Status Register (17) */
880         ret_val = atl1_read_phy_reg(hw, MII_ATLX_PSSR, &phy_data);
881         if (ret_val)
882                 return ret_val;
883
884         if (!(phy_data & MII_ATLX_PSSR_SPD_DPLX_RESOLVED))
885                 return ATLX_ERR_PHY_RES;
886
887         switch (phy_data & MII_ATLX_PSSR_SPEED) {
888         case MII_ATLX_PSSR_1000MBS:
889                 *speed = SPEED_1000;
890                 break;
891         case MII_ATLX_PSSR_100MBS:
892                 *speed = SPEED_100;
893                 break;
894         case MII_ATLX_PSSR_10MBS:
895                 *speed = SPEED_10;
896                 break;
897         default:
898                 if (netif_msg_hw(adapter))
899                         dev_dbg(&pdev->dev, "error getting speed\n");
900                 return ATLX_ERR_PHY_SPEED;
901                 break;
902         }
903         if (phy_data & MII_ATLX_PSSR_DPLX)
904                 *duplex = FULL_DUPLEX;
905         else
906                 *duplex = HALF_DUPLEX;
907
908         return 0;
909 }
910
911 void atl1_set_mac_addr(struct atl1_hw *hw)
912 {
913         u32 value;
914         /*
915          * 00-0B-6A-F6-00-DC
916          * 0:  6AF600DC   1: 000B
917          * low dword
918          */
919         value = (((u32) hw->mac_addr[2]) << 24) |
920             (((u32) hw->mac_addr[3]) << 16) |
921             (((u32) hw->mac_addr[4]) << 8) | (((u32) hw->mac_addr[5]));
922         iowrite32(value, hw->hw_addr + REG_MAC_STA_ADDR);
923         /* high dword */
924         value = (((u32) hw->mac_addr[0]) << 8) | (((u32) hw->mac_addr[1]));
925         iowrite32(value, (hw->hw_addr + REG_MAC_STA_ADDR) + (1 << 2));
926 }
927
928 /*
929  * atl1_sw_init - Initialize general software structures (struct atl1_adapter)
930  * @adapter: board private structure to initialize
931  *
932  * atl1_sw_init initializes the Adapter private data structure.
933  * Fields are initialized based on PCI device information and
934  * OS network device settings (MTU size).
935  */
936 static int __devinit atl1_sw_init(struct atl1_adapter *adapter)
937 {
938         struct atl1_hw *hw = &adapter->hw;
939         struct net_device *netdev = adapter->netdev;
940
941         hw->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
942         hw->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
943
944         adapter->wol = 0;
945         adapter->rx_buffer_len = (hw->max_frame_size + 7) & ~7;
946         adapter->ict = 50000;           /* 100ms */
947         adapter->link_speed = SPEED_0;  /* hardware init */
948         adapter->link_duplex = FULL_DUPLEX;
949
950         hw->phy_configured = false;
951         hw->preamble_len = 7;
952         hw->ipgt = 0x60;
953         hw->min_ifg = 0x50;
954         hw->ipgr1 = 0x40;
955         hw->ipgr2 = 0x60;
956         hw->max_retry = 0xf;
957         hw->lcol = 0x37;
958         hw->jam_ipg = 7;
959         hw->rfd_burst = 8;
960         hw->rrd_burst = 8;
961         hw->rfd_fetch_gap = 1;
962         hw->rx_jumbo_th = adapter->rx_buffer_len / 8;
963         hw->rx_jumbo_lkah = 1;
964         hw->rrd_ret_timer = 16;
965         hw->tpd_burst = 4;
966         hw->tpd_fetch_th = 16;
967         hw->txf_burst = 0x100;
968         hw->tx_jumbo_task_th = (hw->max_frame_size + 7) >> 3;
969         hw->tpd_fetch_gap = 1;
970         hw->rcb_value = atl1_rcb_64;
971         hw->dma_ord = atl1_dma_ord_enh;
972         hw->dmar_block = atl1_dma_req_256;
973         hw->dmaw_block = atl1_dma_req_256;
974         hw->cmb_rrd = 4;
975         hw->cmb_tpd = 4;
976         hw->cmb_rx_timer = 1;   /* about 2us */
977         hw->cmb_tx_timer = 1;   /* about 2us */
978         hw->smb_timer = 100000; /* about 200ms */
979
980         spin_lock_init(&adapter->lock);
981         spin_lock_init(&adapter->mb_lock);
982
983         return 0;
984 }
985
986 static int mdio_read(struct net_device *netdev, int phy_id, int reg_num)
987 {
988         struct atl1_adapter *adapter = netdev_priv(netdev);
989         u16 result;
990
991         atl1_read_phy_reg(&adapter->hw, reg_num & 0x1f, &result);
992
993         return result;
994 }
995
996 static void mdio_write(struct net_device *netdev, int phy_id, int reg_num,
997         int val)
998 {
999         struct atl1_adapter *adapter = netdev_priv(netdev);
1000
1001         atl1_write_phy_reg(&adapter->hw, reg_num, val);
1002 }
1003
1004 /*
1005  * atl1_mii_ioctl -
1006  * @netdev:
1007  * @ifreq:
1008  * @cmd:
1009  */
1010 static int atl1_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1011 {
1012         struct atl1_adapter *adapter = netdev_priv(netdev);
1013         unsigned long flags;
1014         int retval;
1015
1016         if (!netif_running(netdev))
1017                 return -EINVAL;
1018
1019         spin_lock_irqsave(&adapter->lock, flags);
1020         retval = generic_mii_ioctl(&adapter->mii, if_mii(ifr), cmd, NULL);
1021         spin_unlock_irqrestore(&adapter->lock, flags);
1022
1023         return retval;
1024 }
1025
1026 /*
1027  * atl1_setup_mem_resources - allocate Tx / RX descriptor resources
1028  * @adapter: board private structure
1029  *
1030  * Return 0 on success, negative on failure
1031  */
1032 static s32 atl1_setup_ring_resources(struct atl1_adapter *adapter)
1033 {
1034         struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1035         struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1036         struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1037         struct atl1_ring_header *ring_header = &adapter->ring_header;
1038         struct pci_dev *pdev = adapter->pdev;
1039         int size;
1040         u8 offset = 0;
1041
1042         size = sizeof(struct atl1_buffer) * (tpd_ring->count + rfd_ring->count);
1043         tpd_ring->buffer_info = kzalloc(size, GFP_KERNEL);
1044         if (unlikely(!tpd_ring->buffer_info)) {
1045                 if (netif_msg_drv(adapter))
1046                         dev_err(&pdev->dev, "kzalloc failed , size = D%d\n",
1047                                 size);
1048                 goto err_nomem;
1049         }
1050         rfd_ring->buffer_info =
1051                 (struct atl1_buffer *)(tpd_ring->buffer_info + tpd_ring->count);
1052
1053         /*
1054          * real ring DMA buffer
1055          * each ring/block may need up to 8 bytes for alignment, hence the
1056          * additional 40 bytes tacked onto the end.
1057          */
1058         ring_header->size = size =
1059                 sizeof(struct tx_packet_desc) * tpd_ring->count
1060                 + sizeof(struct rx_free_desc) * rfd_ring->count
1061                 + sizeof(struct rx_return_desc) * rrd_ring->count
1062                 + sizeof(struct coals_msg_block)
1063                 + sizeof(struct stats_msg_block)
1064                 + 40;
1065
1066         ring_header->desc = pci_alloc_consistent(pdev, ring_header->size,
1067                 &ring_header->dma);
1068         if (unlikely(!ring_header->desc)) {
1069                 if (netif_msg_drv(adapter))
1070                         dev_err(&pdev->dev, "pci_alloc_consistent failed\n");
1071                 goto err_nomem;
1072         }
1073
1074         memset(ring_header->desc, 0, ring_header->size);
1075
1076         /* init TPD ring */
1077         tpd_ring->dma = ring_header->dma;
1078         offset = (tpd_ring->dma & 0x7) ? (8 - (ring_header->dma & 0x7)) : 0;
1079         tpd_ring->dma += offset;
1080         tpd_ring->desc = (u8 *) ring_header->desc + offset;
1081         tpd_ring->size = sizeof(struct tx_packet_desc) * tpd_ring->count;
1082
1083         /* init RFD ring */
1084         rfd_ring->dma = tpd_ring->dma + tpd_ring->size;
1085         offset = (rfd_ring->dma & 0x7) ? (8 - (rfd_ring->dma & 0x7)) : 0;
1086         rfd_ring->dma += offset;
1087         rfd_ring->desc = (u8 *) tpd_ring->desc + (tpd_ring->size + offset);
1088         rfd_ring->size = sizeof(struct rx_free_desc) * rfd_ring->count;
1089
1090
1091         /* init RRD ring */
1092         rrd_ring->dma = rfd_ring->dma + rfd_ring->size;
1093         offset = (rrd_ring->dma & 0x7) ? (8 - (rrd_ring->dma & 0x7)) : 0;
1094         rrd_ring->dma += offset;
1095         rrd_ring->desc = (u8 *) rfd_ring->desc + (rfd_ring->size + offset);
1096         rrd_ring->size = sizeof(struct rx_return_desc) * rrd_ring->count;
1097
1098
1099         /* init CMB */
1100         adapter->cmb.dma = rrd_ring->dma + rrd_ring->size;
1101         offset = (adapter->cmb.dma & 0x7) ? (8 - (adapter->cmb.dma & 0x7)) : 0;
1102         adapter->cmb.dma += offset;
1103         adapter->cmb.cmb = (struct coals_msg_block *)
1104                 ((u8 *) rrd_ring->desc + (rrd_ring->size + offset));
1105
1106         /* init SMB */
1107         adapter->smb.dma = adapter->cmb.dma + sizeof(struct coals_msg_block);
1108         offset = (adapter->smb.dma & 0x7) ? (8 - (adapter->smb.dma & 0x7)) : 0;
1109         adapter->smb.dma += offset;
1110         adapter->smb.smb = (struct stats_msg_block *)
1111                 ((u8 *) adapter->cmb.cmb +
1112                 (sizeof(struct coals_msg_block) + offset));
1113
1114         return 0;
1115
1116 err_nomem:
1117         kfree(tpd_ring->buffer_info);
1118         return -ENOMEM;
1119 }
1120
1121 static void atl1_init_ring_ptrs(struct atl1_adapter *adapter)
1122 {
1123         struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1124         struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1125         struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1126
1127         atomic_set(&tpd_ring->next_to_use, 0);
1128         atomic_set(&tpd_ring->next_to_clean, 0);
1129
1130         rfd_ring->next_to_clean = 0;
1131         atomic_set(&rfd_ring->next_to_use, 0);
1132
1133         rrd_ring->next_to_use = 0;
1134         atomic_set(&rrd_ring->next_to_clean, 0);
1135 }
1136
1137 /*
1138  * atl1_clean_rx_ring - Free RFD Buffers
1139  * @adapter: board private structure
1140  */
1141 static void atl1_clean_rx_ring(struct atl1_adapter *adapter)
1142 {
1143         struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1144         struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1145         struct atl1_buffer *buffer_info;
1146         struct pci_dev *pdev = adapter->pdev;
1147         unsigned long size;
1148         unsigned int i;
1149
1150         /* Free all the Rx ring sk_buffs */
1151         for (i = 0; i < rfd_ring->count; i++) {
1152                 buffer_info = &rfd_ring->buffer_info[i];
1153                 if (buffer_info->dma) {
1154                         pci_unmap_page(pdev, buffer_info->dma,
1155                                 buffer_info->length, PCI_DMA_FROMDEVICE);
1156                         buffer_info->dma = 0;
1157                 }
1158                 if (buffer_info->skb) {
1159                         dev_kfree_skb(buffer_info->skb);
1160                         buffer_info->skb = NULL;
1161                 }
1162         }
1163
1164         size = sizeof(struct atl1_buffer) * rfd_ring->count;
1165         memset(rfd_ring->buffer_info, 0, size);
1166
1167         /* Zero out the descriptor ring */
1168         memset(rfd_ring->desc, 0, rfd_ring->size);
1169
1170         rfd_ring->next_to_clean = 0;
1171         atomic_set(&rfd_ring->next_to_use, 0);
1172
1173         rrd_ring->next_to_use = 0;
1174         atomic_set(&rrd_ring->next_to_clean, 0);
1175 }
1176
1177 /*
1178  * atl1_clean_tx_ring - Free Tx Buffers
1179  * @adapter: board private structure
1180  */
1181 static void atl1_clean_tx_ring(struct atl1_adapter *adapter)
1182 {
1183         struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1184         struct atl1_buffer *buffer_info;
1185         struct pci_dev *pdev = adapter->pdev;
1186         unsigned long size;
1187         unsigned int i;
1188
1189         /* Free all the Tx ring sk_buffs */
1190         for (i = 0; i < tpd_ring->count; i++) {
1191                 buffer_info = &tpd_ring->buffer_info[i];
1192                 if (buffer_info->dma) {
1193                         pci_unmap_page(pdev, buffer_info->dma,
1194                                 buffer_info->length, PCI_DMA_TODEVICE);
1195                         buffer_info->dma = 0;
1196                 }
1197         }
1198
1199         for (i = 0; i < tpd_ring->count; i++) {
1200                 buffer_info = &tpd_ring->buffer_info[i];
1201                 if (buffer_info->skb) {
1202                         dev_kfree_skb_any(buffer_info->skb);
1203                         buffer_info->skb = NULL;
1204                 }
1205         }
1206
1207         size = sizeof(struct atl1_buffer) * tpd_ring->count;
1208         memset(tpd_ring->buffer_info, 0, size);
1209
1210         /* Zero out the descriptor ring */
1211         memset(tpd_ring->desc, 0, tpd_ring->size);
1212
1213         atomic_set(&tpd_ring->next_to_use, 0);
1214         atomic_set(&tpd_ring->next_to_clean, 0);
1215 }
1216
1217 /*
1218  * atl1_free_ring_resources - Free Tx / RX descriptor Resources
1219  * @adapter: board private structure
1220  *
1221  * Free all transmit software resources
1222  */
1223 static void atl1_free_ring_resources(struct atl1_adapter *adapter)
1224 {
1225         struct pci_dev *pdev = adapter->pdev;
1226         struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1227         struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1228         struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1229         struct atl1_ring_header *ring_header = &adapter->ring_header;
1230
1231         atl1_clean_tx_ring(adapter);
1232         atl1_clean_rx_ring(adapter);
1233
1234         kfree(tpd_ring->buffer_info);
1235         pci_free_consistent(pdev, ring_header->size, ring_header->desc,
1236                 ring_header->dma);
1237
1238         tpd_ring->buffer_info = NULL;
1239         tpd_ring->desc = NULL;
1240         tpd_ring->dma = 0;
1241
1242         rfd_ring->buffer_info = NULL;
1243         rfd_ring->desc = NULL;
1244         rfd_ring->dma = 0;
1245
1246         rrd_ring->desc = NULL;
1247         rrd_ring->dma = 0;
1248 }
1249
1250 static void atl1_setup_mac_ctrl(struct atl1_adapter *adapter)
1251 {
1252         u32 value;
1253         struct atl1_hw *hw = &adapter->hw;
1254         struct net_device *netdev = adapter->netdev;
1255         /* Config MAC CTRL Register */
1256         value = MAC_CTRL_TX_EN | MAC_CTRL_RX_EN;
1257         /* duplex */
1258         if (FULL_DUPLEX == adapter->link_duplex)
1259                 value |= MAC_CTRL_DUPLX;
1260         /* speed */
1261         value |= ((u32) ((SPEED_1000 == adapter->link_speed) ?
1262                          MAC_CTRL_SPEED_1000 : MAC_CTRL_SPEED_10_100) <<
1263                   MAC_CTRL_SPEED_SHIFT);
1264         /* flow control */
1265         value |= (MAC_CTRL_TX_FLOW | MAC_CTRL_RX_FLOW);
1266         /* PAD & CRC */
1267         value |= (MAC_CTRL_ADD_CRC | MAC_CTRL_PAD);
1268         /* preamble length */
1269         value |= (((u32) adapter->hw.preamble_len
1270                    & MAC_CTRL_PRMLEN_MASK) << MAC_CTRL_PRMLEN_SHIFT);
1271         /* vlan */
1272         if (adapter->vlgrp)
1273                 value |= MAC_CTRL_RMV_VLAN;
1274         /* rx checksum
1275            if (adapter->rx_csum)
1276            value |= MAC_CTRL_RX_CHKSUM_EN;
1277          */
1278         /* filter mode */
1279         value |= MAC_CTRL_BC_EN;
1280         if (netdev->flags & IFF_PROMISC)
1281                 value |= MAC_CTRL_PROMIS_EN;
1282         else if (netdev->flags & IFF_ALLMULTI)
1283                 value |= MAC_CTRL_MC_ALL_EN;
1284         /* value |= MAC_CTRL_LOOPBACK; */
1285         iowrite32(value, hw->hw_addr + REG_MAC_CTRL);
1286 }
1287
1288 static u32 atl1_check_link(struct atl1_adapter *adapter)
1289 {
1290         struct atl1_hw *hw = &adapter->hw;
1291         struct net_device *netdev = adapter->netdev;
1292         u32 ret_val;
1293         u16 speed, duplex, phy_data;
1294         int reconfig = 0;
1295
1296         /* MII_BMSR must read twice */
1297         atl1_read_phy_reg(hw, MII_BMSR, &phy_data);
1298         atl1_read_phy_reg(hw, MII_BMSR, &phy_data);
1299         if (!(phy_data & BMSR_LSTATUS)) {
1300                 /* link down */
1301                 if (netif_carrier_ok(netdev)) {
1302                         /* old link state: Up */
1303                         if (netif_msg_link(adapter))
1304                                 dev_info(&adapter->pdev->dev, "link is down\n");
1305                         adapter->link_speed = SPEED_0;
1306                         netif_carrier_off(netdev);
1307                 }
1308                 return 0;
1309         }
1310
1311         /* Link Up */
1312         ret_val = atl1_get_speed_and_duplex(hw, &speed, &duplex);
1313         if (ret_val)
1314                 return ret_val;
1315
1316         switch (hw->media_type) {
1317         case MEDIA_TYPE_1000M_FULL:
1318                 if (speed != SPEED_1000 || duplex != FULL_DUPLEX)
1319                         reconfig = 1;
1320                 break;
1321         case MEDIA_TYPE_100M_FULL:
1322                 if (speed != SPEED_100 || duplex != FULL_DUPLEX)
1323                         reconfig = 1;
1324                 break;
1325         case MEDIA_TYPE_100M_HALF:
1326                 if (speed != SPEED_100 || duplex != HALF_DUPLEX)
1327                         reconfig = 1;
1328                 break;
1329         case MEDIA_TYPE_10M_FULL:
1330                 if (speed != SPEED_10 || duplex != FULL_DUPLEX)
1331                         reconfig = 1;
1332                 break;
1333         case MEDIA_TYPE_10M_HALF:
1334                 if (speed != SPEED_10 || duplex != HALF_DUPLEX)
1335                         reconfig = 1;
1336                 break;
1337         }
1338
1339         /* link result is our setting */
1340         if (!reconfig) {
1341                 if (adapter->link_speed != speed
1342                     || adapter->link_duplex != duplex) {
1343                         adapter->link_speed = speed;
1344                         adapter->link_duplex = duplex;
1345                         atl1_setup_mac_ctrl(adapter);
1346                         if (netif_msg_link(adapter))
1347                                 dev_info(&adapter->pdev->dev,
1348                                         "%s link is up %d Mbps %s\n",
1349                                         netdev->name, adapter->link_speed,
1350                                         adapter->link_duplex == FULL_DUPLEX ?
1351                                         "full duplex" : "half duplex");
1352                 }
1353                 if (!netif_carrier_ok(netdev)) {
1354                         /* Link down -> Up */
1355                         netif_carrier_on(netdev);
1356                 }
1357                 return 0;
1358         }
1359
1360         /* change original link status */
1361         if (netif_carrier_ok(netdev)) {
1362                 adapter->link_speed = SPEED_0;
1363                 netif_carrier_off(netdev);
1364                 netif_stop_queue(netdev);
1365         }
1366
1367         if (hw->media_type != MEDIA_TYPE_AUTO_SENSOR &&
1368             hw->media_type != MEDIA_TYPE_1000M_FULL) {
1369                 switch (hw->media_type) {
1370                 case MEDIA_TYPE_100M_FULL:
1371                         phy_data = MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
1372                                    MII_CR_RESET;
1373                         break;
1374                 case MEDIA_TYPE_100M_HALF:
1375                         phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
1376                         break;
1377                 case MEDIA_TYPE_10M_FULL:
1378                         phy_data =
1379                             MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
1380                         break;
1381                 default:
1382                         /* MEDIA_TYPE_10M_HALF: */
1383                         phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
1384                         break;
1385                 }
1386                 atl1_write_phy_reg(hw, MII_BMCR, phy_data);
1387                 return 0;
1388         }
1389
1390         /* auto-neg, insert timer to re-config phy */
1391         if (!adapter->phy_timer_pending) {
1392                 adapter->phy_timer_pending = true;
1393                 mod_timer(&adapter->phy_config_timer, jiffies + 3 * HZ);
1394         }
1395
1396         return 0;
1397 }
1398
1399 static void set_flow_ctrl_old(struct atl1_adapter *adapter)
1400 {
1401         u32 hi, lo, value;
1402
1403         /* RFD Flow Control */
1404         value = adapter->rfd_ring.count;
1405         hi = value / 16;
1406         if (hi < 2)
1407                 hi = 2;
1408         lo = value * 7 / 8;
1409
1410         value = ((hi & RXQ_RXF_PAUSE_TH_HI_MASK) << RXQ_RXF_PAUSE_TH_HI_SHIFT) |
1411                 ((lo & RXQ_RXF_PAUSE_TH_LO_MASK) << RXQ_RXF_PAUSE_TH_LO_SHIFT);
1412         iowrite32(value, adapter->hw.hw_addr + REG_RXQ_RXF_PAUSE_THRESH);
1413
1414         /* RRD Flow Control */
1415         value = adapter->rrd_ring.count;
1416         lo = value / 16;
1417         hi = value * 7 / 8;
1418         if (lo < 2)
1419                 lo = 2;
1420         value = ((hi & RXQ_RRD_PAUSE_TH_HI_MASK) << RXQ_RRD_PAUSE_TH_HI_SHIFT) |
1421                 ((lo & RXQ_RRD_PAUSE_TH_LO_MASK) << RXQ_RRD_PAUSE_TH_LO_SHIFT);
1422         iowrite32(value, adapter->hw.hw_addr + REG_RXQ_RRD_PAUSE_THRESH);
1423 }
1424
1425 static void set_flow_ctrl_new(struct atl1_hw *hw)
1426 {
1427         u32 hi, lo, value;
1428
1429         /* RXF Flow Control */
1430         value = ioread32(hw->hw_addr + REG_SRAM_RXF_LEN);
1431         lo = value / 16;
1432         if (lo < 192)
1433                 lo = 192;
1434         hi = value * 7 / 8;
1435         if (hi < lo)
1436                 hi = lo + 16;
1437         value = ((hi & RXQ_RXF_PAUSE_TH_HI_MASK) << RXQ_RXF_PAUSE_TH_HI_SHIFT) |
1438                 ((lo & RXQ_RXF_PAUSE_TH_LO_MASK) << RXQ_RXF_PAUSE_TH_LO_SHIFT);
1439         iowrite32(value, hw->hw_addr + REG_RXQ_RXF_PAUSE_THRESH);
1440
1441         /* RRD Flow Control */
1442         value = ioread32(hw->hw_addr + REG_SRAM_RRD_LEN);
1443         lo = value / 8;
1444         hi = value * 7 / 8;
1445         if (lo < 2)
1446                 lo = 2;
1447         if (hi < lo)
1448                 hi = lo + 3;
1449         value = ((hi & RXQ_RRD_PAUSE_TH_HI_MASK) << RXQ_RRD_PAUSE_TH_HI_SHIFT) |
1450                 ((lo & RXQ_RRD_PAUSE_TH_LO_MASK) << RXQ_RRD_PAUSE_TH_LO_SHIFT);
1451         iowrite32(value, hw->hw_addr + REG_RXQ_RRD_PAUSE_THRESH);
1452 }
1453
1454 /*
1455  * atl1_configure - Configure Transmit&Receive Unit after Reset
1456  * @adapter: board private structure
1457  *
1458  * Configure the Tx /Rx unit of the MAC after a reset.
1459  */
1460 static u32 atl1_configure(struct atl1_adapter *adapter)
1461 {
1462         struct atl1_hw *hw = &adapter->hw;
1463         u32 value;
1464
1465         /* clear interrupt status */
1466         iowrite32(0xffffffff, adapter->hw.hw_addr + REG_ISR);
1467
1468         /* set MAC Address */
1469         value = (((u32) hw->mac_addr[2]) << 24) |
1470                 (((u32) hw->mac_addr[3]) << 16) |
1471                 (((u32) hw->mac_addr[4]) << 8) |
1472                 (((u32) hw->mac_addr[5]));
1473         iowrite32(value, hw->hw_addr + REG_MAC_STA_ADDR);
1474         value = (((u32) hw->mac_addr[0]) << 8) | (((u32) hw->mac_addr[1]));
1475         iowrite32(value, hw->hw_addr + (REG_MAC_STA_ADDR + 4));
1476
1477         /* tx / rx ring */
1478
1479         /* HI base address */
1480         iowrite32((u32) ((adapter->tpd_ring.dma & 0xffffffff00000000ULL) >> 32),
1481                 hw->hw_addr + REG_DESC_BASE_ADDR_HI);
1482         /* LO base address */
1483         iowrite32((u32) (adapter->rfd_ring.dma & 0x00000000ffffffffULL),
1484                 hw->hw_addr + REG_DESC_RFD_ADDR_LO);
1485         iowrite32((u32) (adapter->rrd_ring.dma & 0x00000000ffffffffULL),
1486                 hw->hw_addr + REG_DESC_RRD_ADDR_LO);
1487         iowrite32((u32) (adapter->tpd_ring.dma & 0x00000000ffffffffULL),
1488                 hw->hw_addr + REG_DESC_TPD_ADDR_LO);
1489         iowrite32((u32) (adapter->cmb.dma & 0x00000000ffffffffULL),
1490                 hw->hw_addr + REG_DESC_CMB_ADDR_LO);
1491         iowrite32((u32) (adapter->smb.dma & 0x00000000ffffffffULL),
1492                 hw->hw_addr + REG_DESC_SMB_ADDR_LO);
1493
1494         /* element count */
1495         value = adapter->rrd_ring.count;
1496         value <<= 16;
1497         value += adapter->rfd_ring.count;
1498         iowrite32(value, hw->hw_addr + REG_DESC_RFD_RRD_RING_SIZE);
1499         iowrite32(adapter->tpd_ring.count, hw->hw_addr +
1500                 REG_DESC_TPD_RING_SIZE);
1501
1502         /* Load Ptr */
1503         iowrite32(1, hw->hw_addr + REG_LOAD_PTR);
1504
1505         /* config Mailbox */
1506         value = ((atomic_read(&adapter->tpd_ring.next_to_use)
1507                   & MB_TPD_PROD_INDX_MASK) << MB_TPD_PROD_INDX_SHIFT) |
1508                 ((atomic_read(&adapter->rrd_ring.next_to_clean)
1509                 & MB_RRD_CONS_INDX_MASK) << MB_RRD_CONS_INDX_SHIFT) |
1510                 ((atomic_read(&adapter->rfd_ring.next_to_use)
1511                 & MB_RFD_PROD_INDX_MASK) << MB_RFD_PROD_INDX_SHIFT);
1512         iowrite32(value, hw->hw_addr + REG_MAILBOX);
1513
1514         /* config IPG/IFG */
1515         value = (((u32) hw->ipgt & MAC_IPG_IFG_IPGT_MASK)
1516                  << MAC_IPG_IFG_IPGT_SHIFT) |
1517                 (((u32) hw->min_ifg & MAC_IPG_IFG_MIFG_MASK)
1518                 << MAC_IPG_IFG_MIFG_SHIFT) |
1519                 (((u32) hw->ipgr1 & MAC_IPG_IFG_IPGR1_MASK)
1520                 << MAC_IPG_IFG_IPGR1_SHIFT) |
1521                 (((u32) hw->ipgr2 & MAC_IPG_IFG_IPGR2_MASK)
1522                 << MAC_IPG_IFG_IPGR2_SHIFT);
1523         iowrite32(value, hw->hw_addr + REG_MAC_IPG_IFG);
1524
1525         /* config  Half-Duplex Control */
1526         value = ((u32) hw->lcol & MAC_HALF_DUPLX_CTRL_LCOL_MASK) |
1527                 (((u32) hw->max_retry & MAC_HALF_DUPLX_CTRL_RETRY_MASK)
1528                 << MAC_HALF_DUPLX_CTRL_RETRY_SHIFT) |
1529                 MAC_HALF_DUPLX_CTRL_EXC_DEF_EN |
1530                 (0xa << MAC_HALF_DUPLX_CTRL_ABEBT_SHIFT) |
1531                 (((u32) hw->jam_ipg & MAC_HALF_DUPLX_CTRL_JAMIPG_MASK)
1532                 << MAC_HALF_DUPLX_CTRL_JAMIPG_SHIFT);
1533         iowrite32(value, hw->hw_addr + REG_MAC_HALF_DUPLX_CTRL);
1534
1535         /* set Interrupt Moderator Timer */
1536         iowrite16(adapter->imt, hw->hw_addr + REG_IRQ_MODU_TIMER_INIT);
1537         iowrite32(MASTER_CTRL_ITIMER_EN, hw->hw_addr + REG_MASTER_CTRL);
1538
1539         /* set Interrupt Clear Timer */
1540         iowrite16(adapter->ict, hw->hw_addr + REG_CMBDISDMA_TIMER);
1541
1542         /* set max frame size hw will accept */
1543         iowrite32(hw->max_frame_size, hw->hw_addr + REG_MTU);
1544
1545         /* jumbo size & rrd retirement timer */
1546         value = (((u32) hw->rx_jumbo_th & RXQ_JMBOSZ_TH_MASK)
1547                  << RXQ_JMBOSZ_TH_SHIFT) |
1548                 (((u32) hw->rx_jumbo_lkah & RXQ_JMBO_LKAH_MASK)
1549                 << RXQ_JMBO_LKAH_SHIFT) |
1550                 (((u32) hw->rrd_ret_timer & RXQ_RRD_TIMER_MASK)
1551                 << RXQ_RRD_TIMER_SHIFT);
1552         iowrite32(value, hw->hw_addr + REG_RXQ_JMBOSZ_RRDTIM);
1553
1554         /* Flow Control */
1555         switch (hw->dev_rev) {
1556         case 0x8001:
1557         case 0x9001:
1558         case 0x9002:
1559         case 0x9003:
1560                 set_flow_ctrl_old(adapter);
1561                 break;
1562         default:
1563                 set_flow_ctrl_new(hw);
1564                 break;
1565         }
1566
1567         /* config TXQ */
1568         value = (((u32) hw->tpd_burst & TXQ_CTRL_TPD_BURST_NUM_MASK)
1569                  << TXQ_CTRL_TPD_BURST_NUM_SHIFT) |
1570                 (((u32) hw->txf_burst & TXQ_CTRL_TXF_BURST_NUM_MASK)
1571                 << TXQ_CTRL_TXF_BURST_NUM_SHIFT) |
1572                 (((u32) hw->tpd_fetch_th & TXQ_CTRL_TPD_FETCH_TH_MASK)
1573                 << TXQ_CTRL_TPD_FETCH_TH_SHIFT) | TXQ_CTRL_ENH_MODE |
1574                 TXQ_CTRL_EN;
1575         iowrite32(value, hw->hw_addr + REG_TXQ_CTRL);
1576
1577         /* min tpd fetch gap & tx jumbo packet size threshold for taskoffload */
1578         value = (((u32) hw->tx_jumbo_task_th & TX_JUMBO_TASK_TH_MASK)
1579                 << TX_JUMBO_TASK_TH_SHIFT) |
1580                 (((u32) hw->tpd_fetch_gap & TX_TPD_MIN_IPG_MASK)
1581                 << TX_TPD_MIN_IPG_SHIFT);
1582         iowrite32(value, hw->hw_addr + REG_TX_JUMBO_TASK_TH_TPD_IPG);
1583
1584         /* config RXQ */
1585         value = (((u32) hw->rfd_burst & RXQ_CTRL_RFD_BURST_NUM_MASK)
1586                 << RXQ_CTRL_RFD_BURST_NUM_SHIFT) |
1587                 (((u32) hw->rrd_burst & RXQ_CTRL_RRD_BURST_THRESH_MASK)
1588                 << RXQ_CTRL_RRD_BURST_THRESH_SHIFT) |
1589                 (((u32) hw->rfd_fetch_gap & RXQ_CTRL_RFD_PREF_MIN_IPG_MASK)
1590                 << RXQ_CTRL_RFD_PREF_MIN_IPG_SHIFT) | RXQ_CTRL_CUT_THRU_EN |
1591                 RXQ_CTRL_EN;
1592         iowrite32(value, hw->hw_addr + REG_RXQ_CTRL);
1593
1594         /* config DMA Engine */
1595         value = ((((u32) hw->dmar_block) & DMA_CTRL_DMAR_BURST_LEN_MASK)
1596                 << DMA_CTRL_DMAR_BURST_LEN_SHIFT) |
1597                 ((((u32) hw->dmaw_block) & DMA_CTRL_DMAW_BURST_LEN_MASK)
1598                 << DMA_CTRL_DMAW_BURST_LEN_SHIFT) | DMA_CTRL_DMAR_EN |
1599                 DMA_CTRL_DMAW_EN;
1600         value |= (u32) hw->dma_ord;
1601         if (atl1_rcb_128 == hw->rcb_value)
1602                 value |= DMA_CTRL_RCB_VALUE;
1603         iowrite32(value, hw->hw_addr + REG_DMA_CTRL);
1604
1605         /* config CMB / SMB */
1606         value = (hw->cmb_tpd > adapter->tpd_ring.count) ?
1607                 hw->cmb_tpd : adapter->tpd_ring.count;
1608         value <<= 16;
1609         value |= hw->cmb_rrd;
1610         iowrite32(value, hw->hw_addr + REG_CMB_WRITE_TH);
1611         value = hw->cmb_rx_timer | ((u32) hw->cmb_tx_timer << 16);
1612         iowrite32(value, hw->hw_addr + REG_CMB_WRITE_TIMER);
1613         iowrite32(hw->smb_timer, hw->hw_addr + REG_SMB_TIMER);
1614
1615         /* --- enable CMB / SMB */
1616         value = CSMB_CTRL_CMB_EN | CSMB_CTRL_SMB_EN;
1617         iowrite32(value, hw->hw_addr + REG_CSMB_CTRL);
1618
1619         value = ioread32(adapter->hw.hw_addr + REG_ISR);
1620         if (unlikely((value & ISR_PHY_LINKDOWN) != 0))
1621                 value = 1;      /* config failed */
1622         else
1623                 value = 0;
1624
1625         /* clear all interrupt status */
1626         iowrite32(0x3fffffff, adapter->hw.hw_addr + REG_ISR);
1627         iowrite32(0, adapter->hw.hw_addr + REG_ISR);
1628         return value;
1629 }
1630
1631 /*
1632  * atl1_pcie_patch - Patch for PCIE module
1633  */
1634 static void atl1_pcie_patch(struct atl1_adapter *adapter)
1635 {
1636         u32 value;
1637
1638         /* much vendor magic here */
1639         value = 0x6500;
1640         iowrite32(value, adapter->hw.hw_addr + 0x12FC);
1641         /* pcie flow control mode change */
1642         value = ioread32(adapter->hw.hw_addr + 0x1008);
1643         value |= 0x8000;
1644         iowrite32(value, adapter->hw.hw_addr + 0x1008);
1645 }
1646
1647 /*
1648  * When ACPI resume on some VIA MotherBoard, the Interrupt Disable bit/0x400
1649  * on PCI Command register is disable.
1650  * The function enable this bit.
1651  * Brackett, 2006/03/15
1652  */
1653 static void atl1_via_workaround(struct atl1_adapter *adapter)
1654 {
1655         unsigned long value;
1656
1657         value = ioread16(adapter->hw.hw_addr + PCI_COMMAND);
1658         if (value & PCI_COMMAND_INTX_DISABLE)
1659                 value &= ~PCI_COMMAND_INTX_DISABLE;
1660         iowrite32(value, adapter->hw.hw_addr + PCI_COMMAND);
1661 }
1662
1663 static void atl1_inc_smb(struct atl1_adapter *adapter)
1664 {
1665         struct stats_msg_block *smb = adapter->smb.smb;
1666
1667         /* Fill out the OS statistics structure */
1668         adapter->soft_stats.rx_packets += smb->rx_ok;
1669         adapter->soft_stats.tx_packets += smb->tx_ok;
1670         adapter->soft_stats.rx_bytes += smb->rx_byte_cnt;
1671         adapter->soft_stats.tx_bytes += smb->tx_byte_cnt;
1672         adapter->soft_stats.multicast += smb->rx_mcast;
1673         adapter->soft_stats.collisions += (smb->tx_1_col + smb->tx_2_col * 2 +
1674                 smb->tx_late_col + smb->tx_abort_col * adapter->hw.max_retry);
1675
1676         /* Rx Errors */
1677         adapter->soft_stats.rx_errors += (smb->rx_frag + smb->rx_fcs_err +
1678                 smb->rx_len_err + smb->rx_sz_ov + smb->rx_rxf_ov +
1679                 smb->rx_rrd_ov + smb->rx_align_err);
1680         adapter->soft_stats.rx_fifo_errors += smb->rx_rxf_ov;
1681         adapter->soft_stats.rx_length_errors += smb->rx_len_err;
1682         adapter->soft_stats.rx_crc_errors += smb->rx_fcs_err;
1683         adapter->soft_stats.rx_frame_errors += smb->rx_align_err;
1684         adapter->soft_stats.rx_missed_errors += (smb->rx_rrd_ov +
1685                 smb->rx_rxf_ov);
1686
1687         adapter->soft_stats.rx_pause += smb->rx_pause;
1688         adapter->soft_stats.rx_rrd_ov += smb->rx_rrd_ov;
1689         adapter->soft_stats.rx_trunc += smb->rx_sz_ov;
1690
1691         /* Tx Errors */
1692         adapter->soft_stats.tx_errors += (smb->tx_late_col +
1693                 smb->tx_abort_col + smb->tx_underrun + smb->tx_trunc);
1694         adapter->soft_stats.tx_fifo_errors += smb->tx_underrun;
1695         adapter->soft_stats.tx_aborted_errors += smb->tx_abort_col;
1696         adapter->soft_stats.tx_window_errors += smb->tx_late_col;
1697
1698         adapter->soft_stats.excecol += smb->tx_abort_col;
1699         adapter->soft_stats.deffer += smb->tx_defer;
1700         adapter->soft_stats.scc += smb->tx_1_col;
1701         adapter->soft_stats.mcc += smb->tx_2_col;
1702         adapter->soft_stats.latecol += smb->tx_late_col;
1703         adapter->soft_stats.tx_underun += smb->tx_underrun;
1704         adapter->soft_stats.tx_trunc += smb->tx_trunc;
1705         adapter->soft_stats.tx_pause += smb->tx_pause;
1706
1707         adapter->net_stats.rx_packets = adapter->soft_stats.rx_packets;
1708         adapter->net_stats.tx_packets = adapter->soft_stats.tx_packets;
1709         adapter->net_stats.rx_bytes = adapter->soft_stats.rx_bytes;
1710         adapter->net_stats.tx_bytes = adapter->soft_stats.tx_bytes;
1711         adapter->net_stats.multicast = adapter->soft_stats.multicast;
1712         adapter->net_stats.collisions = adapter->soft_stats.collisions;
1713         adapter->net_stats.rx_errors = adapter->soft_stats.rx_errors;
1714         adapter->net_stats.rx_over_errors =
1715                 adapter->soft_stats.rx_missed_errors;
1716         adapter->net_stats.rx_length_errors =
1717                 adapter->soft_stats.rx_length_errors;
1718         adapter->net_stats.rx_crc_errors = adapter->soft_stats.rx_crc_errors;
1719         adapter->net_stats.rx_frame_errors =
1720                 adapter->soft_stats.rx_frame_errors;
1721         adapter->net_stats.rx_fifo_errors = adapter->soft_stats.rx_fifo_errors;
1722         adapter->net_stats.rx_missed_errors =
1723                 adapter->soft_stats.rx_missed_errors;
1724         adapter->net_stats.tx_errors = adapter->soft_stats.tx_errors;
1725         adapter->net_stats.tx_fifo_errors = adapter->soft_stats.tx_fifo_errors;
1726         adapter->net_stats.tx_aborted_errors =
1727                 adapter->soft_stats.tx_aborted_errors;
1728         adapter->net_stats.tx_window_errors =
1729                 adapter->soft_stats.tx_window_errors;
1730         adapter->net_stats.tx_carrier_errors =
1731                 adapter->soft_stats.tx_carrier_errors;
1732 }
1733
1734 static void atl1_update_mailbox(struct atl1_adapter *adapter)
1735 {
1736         unsigned long flags;
1737         u32 tpd_next_to_use;
1738         u32 rfd_next_to_use;
1739         u32 rrd_next_to_clean;
1740         u32 value;
1741
1742         spin_lock_irqsave(&adapter->mb_lock, flags);
1743
1744         tpd_next_to_use = atomic_read(&adapter->tpd_ring.next_to_use);
1745         rfd_next_to_use = atomic_read(&adapter->rfd_ring.next_to_use);
1746         rrd_next_to_clean = atomic_read(&adapter->rrd_ring.next_to_clean);
1747
1748         value = ((rfd_next_to_use & MB_RFD_PROD_INDX_MASK) <<
1749                 MB_RFD_PROD_INDX_SHIFT) |
1750                 ((rrd_next_to_clean & MB_RRD_CONS_INDX_MASK) <<
1751                 MB_RRD_CONS_INDX_SHIFT) |
1752                 ((tpd_next_to_use & MB_TPD_PROD_INDX_MASK) <<
1753                 MB_TPD_PROD_INDX_SHIFT);
1754         iowrite32(value, adapter->hw.hw_addr + REG_MAILBOX);
1755
1756         spin_unlock_irqrestore(&adapter->mb_lock, flags);
1757 }
1758
1759 static void atl1_clean_alloc_flag(struct atl1_adapter *adapter,
1760         struct rx_return_desc *rrd, u16 offset)
1761 {
1762         struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1763
1764         while (rfd_ring->next_to_clean != (rrd->buf_indx + offset)) {
1765                 rfd_ring->buffer_info[rfd_ring->next_to_clean].alloced = 0;
1766                 if (++rfd_ring->next_to_clean == rfd_ring->count) {
1767                         rfd_ring->next_to_clean = 0;
1768                 }
1769         }
1770 }
1771
1772 static void atl1_update_rfd_index(struct atl1_adapter *adapter,
1773         struct rx_return_desc *rrd)
1774 {
1775         u16 num_buf;
1776
1777         num_buf = (rrd->xsz.xsum_sz.pkt_size + adapter->rx_buffer_len - 1) /
1778                 adapter->rx_buffer_len;
1779         if (rrd->num_buf == num_buf)
1780                 /* clean alloc flag for bad rrd */
1781                 atl1_clean_alloc_flag(adapter, rrd, num_buf);
1782 }
1783
1784 static void atl1_rx_checksum(struct atl1_adapter *adapter,
1785         struct rx_return_desc *rrd, struct sk_buff *skb)
1786 {
1787         struct pci_dev *pdev = adapter->pdev;
1788
1789         /*
1790          * The L1 hardware contains a bug that erroneously sets the
1791          * PACKET_FLAG_ERR and ERR_FLAG_L4_CHKSUM bits whenever a
1792          * fragmented IP packet is received, even though the packet
1793          * is perfectly valid and its checksum is correct. There's
1794          * no way to distinguish between one of these good packets
1795          * and a packet that actually contains a TCP/UDP checksum
1796          * error, so all we can do is allow it to be handed up to
1797          * the higher layers and let it be sorted out there.
1798          */
1799
1800         skb->ip_summed = CHECKSUM_NONE;
1801
1802         if (unlikely(rrd->pkt_flg & PACKET_FLAG_ERR)) {
1803                 if (rrd->err_flg & (ERR_FLAG_CRC | ERR_FLAG_TRUNC |
1804                                         ERR_FLAG_CODE | ERR_FLAG_OV)) {
1805                         adapter->hw_csum_err++;
1806                         if (netif_msg_rx_err(adapter))
1807                                 dev_printk(KERN_DEBUG, &pdev->dev,
1808                                         "rx checksum error\n");
1809                         return;
1810                 }
1811         }
1812
1813         /* not IPv4 */
1814         if (!(rrd->pkt_flg & PACKET_FLAG_IPV4))
1815                 /* checksum is invalid, but it's not an IPv4 pkt, so ok */
1816                 return;
1817
1818         /* IPv4 packet */
1819         if (likely(!(rrd->err_flg &
1820                 (ERR_FLAG_IP_CHKSUM | ERR_FLAG_L4_CHKSUM)))) {
1821                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1822                 adapter->hw_csum_good++;
1823                 return;
1824         }
1825
1826         return;
1827 }
1828
1829 /*
1830  * atl1_alloc_rx_buffers - Replace used receive buffers
1831  * @adapter: address of board private structure
1832  */
1833 static u16 atl1_alloc_rx_buffers(struct atl1_adapter *adapter)
1834 {
1835         struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1836         struct pci_dev *pdev = adapter->pdev;
1837         struct page *page;
1838         unsigned long offset;
1839         struct atl1_buffer *buffer_info, *next_info;
1840         struct sk_buff *skb;
1841         u16 num_alloc = 0;
1842         u16 rfd_next_to_use, next_next;
1843         struct rx_free_desc *rfd_desc;
1844
1845         next_next = rfd_next_to_use = atomic_read(&rfd_ring->next_to_use);
1846         if (++next_next == rfd_ring->count)
1847                 next_next = 0;
1848         buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
1849         next_info = &rfd_ring->buffer_info[next_next];
1850
1851         while (!buffer_info->alloced && !next_info->alloced) {
1852                 if (buffer_info->skb) {
1853                         buffer_info->alloced = 1;
1854                         goto next;
1855                 }
1856
1857                 rfd_desc = ATL1_RFD_DESC(rfd_ring, rfd_next_to_use);
1858
1859                 skb = netdev_alloc_skb(adapter->netdev,
1860                                        adapter->rx_buffer_len + NET_IP_ALIGN);
1861                 if (unlikely(!skb)) {
1862                         /* Better luck next round */
1863                         adapter->net_stats.rx_dropped++;
1864                         break;
1865                 }
1866
1867                 /*
1868                  * Make buffer alignment 2 beyond a 16 byte boundary
1869                  * this will result in a 16 byte aligned IP header after
1870                  * the 14 byte MAC header is removed
1871                  */
1872                 skb_reserve(skb, NET_IP_ALIGN);
1873
1874                 buffer_info->alloced = 1;
1875                 buffer_info->skb = skb;
1876                 buffer_info->length = (u16) adapter->rx_buffer_len;
1877                 page = virt_to_page(skb->data);
1878                 offset = (unsigned long)skb->data & ~PAGE_MASK;
1879                 buffer_info->dma = pci_map_page(pdev, page, offset,
1880                                                 adapter->rx_buffer_len,
1881                                                 PCI_DMA_FROMDEVICE);
1882                 rfd_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
1883                 rfd_desc->buf_len = cpu_to_le16(adapter->rx_buffer_len);
1884                 rfd_desc->coalese = 0;
1885
1886 next:
1887                 rfd_next_to_use = next_next;
1888                 if (unlikely(++next_next == rfd_ring->count))
1889                         next_next = 0;
1890
1891                 buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
1892                 next_info = &rfd_ring->buffer_info[next_next];
1893                 num_alloc++;
1894         }
1895
1896         if (num_alloc) {
1897                 /*
1898                  * Force memory writes to complete before letting h/w
1899                  * know there are new descriptors to fetch.  (Only
1900                  * applicable for weak-ordered memory model archs,
1901                  * such as IA-64).
1902                  */
1903                 wmb();
1904                 atomic_set(&rfd_ring->next_to_use, (int)rfd_next_to_use);
1905         }
1906         return num_alloc;
1907 }
1908
1909 static void atl1_intr_rx(struct atl1_adapter *adapter)
1910 {
1911         int i, count;
1912         u16 length;
1913         u16 rrd_next_to_clean;
1914         u32 value;
1915         struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1916         struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1917         struct atl1_buffer *buffer_info;
1918         struct rx_return_desc *rrd;
1919         struct sk_buff *skb;
1920
1921         count = 0;
1922
1923         rrd_next_to_clean = atomic_read(&rrd_ring->next_to_clean);
1924
1925         while (1) {
1926                 rrd = ATL1_RRD_DESC(rrd_ring, rrd_next_to_clean);
1927                 i = 1;
1928                 if (likely(rrd->xsz.valid)) {   /* packet valid */
1929 chk_rrd:
1930                         /* check rrd status */
1931                         if (likely(rrd->num_buf == 1))
1932                                 goto rrd_ok;
1933                         else if (netif_msg_rx_err(adapter)) {
1934                                 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1935                                         "unexpected RRD buffer count\n");
1936                                 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1937                                         "rx_buf_len = %d\n",
1938                                         adapter->rx_buffer_len);
1939                                 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1940                                         "RRD num_buf = %d\n",
1941                                         rrd->num_buf);
1942                                 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1943                                         "RRD pkt_len = %d\n",
1944                                         rrd->xsz.xsum_sz.pkt_size);
1945                                 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1946                                         "RRD pkt_flg = 0x%08X\n",
1947                                         rrd->pkt_flg);
1948                                 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1949                                         "RRD err_flg = 0x%08X\n",
1950                                         rrd->err_flg);
1951                                 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1952                                         "RRD vlan_tag = 0x%08X\n",
1953                                         rrd->vlan_tag);
1954                         }
1955
1956                         /* rrd seems to be bad */
1957                         if (unlikely(i-- > 0)) {
1958                                 /* rrd may not be DMAed completely */
1959                                 udelay(1);
1960                                 goto chk_rrd;
1961                         }
1962                         /* bad rrd */
1963                         if (netif_msg_rx_err(adapter))
1964                                 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1965                                         "bad RRD\n");
1966                         /* see if update RFD index */
1967                         if (rrd->num_buf > 1)
1968                                 atl1_update_rfd_index(adapter, rrd);
1969
1970                         /* update rrd */
1971                         rrd->xsz.valid = 0;
1972                         if (++rrd_next_to_clean == rrd_ring->count)
1973                                 rrd_next_to_clean = 0;
1974                         count++;
1975                         continue;
1976                 } else {        /* current rrd still not be updated */
1977
1978                         break;
1979                 }
1980 rrd_ok:
1981                 /* clean alloc flag for bad rrd */
1982                 atl1_clean_alloc_flag(adapter, rrd, 0);
1983
1984                 buffer_info = &rfd_ring->buffer_info[rrd->buf_indx];
1985                 if (++rfd_ring->next_to_clean == rfd_ring->count)
1986                         rfd_ring->next_to_clean = 0;
1987
1988                 /* update rrd next to clean */
1989                 if (++rrd_next_to_clean == rrd_ring->count)
1990                         rrd_next_to_clean = 0;
1991                 count++;
1992
1993                 if (unlikely(rrd->pkt_flg & PACKET_FLAG_ERR)) {
1994                         if (!(rrd->err_flg &
1995                                 (ERR_FLAG_IP_CHKSUM | ERR_FLAG_L4_CHKSUM
1996                                 | ERR_FLAG_LEN))) {
1997                                 /* packet error, don't need upstream */
1998                                 buffer_info->alloced = 0;
1999                                 rrd->xsz.valid = 0;
2000                                 continue;
2001                         }
2002                 }
2003
2004                 /* Good Receive */
2005                 pci_unmap_page(adapter->pdev, buffer_info->dma,
2006                                buffer_info->length, PCI_DMA_FROMDEVICE);
2007                 buffer_info->dma = 0;
2008                 skb = buffer_info->skb;
2009                 length = le16_to_cpu(rrd->xsz.xsum_sz.pkt_size);
2010
2011                 skb_put(skb, length - ETH_FCS_LEN);
2012
2013                 /* Receive Checksum Offload */
2014                 atl1_rx_checksum(adapter, rrd, skb);
2015                 skb->protocol = eth_type_trans(skb, adapter->netdev);
2016
2017                 if (adapter->vlgrp && (rrd->pkt_flg & PACKET_FLAG_VLAN_INS)) {
2018                         u16 vlan_tag = (rrd->vlan_tag >> 4) |
2019                                         ((rrd->vlan_tag & 7) << 13) |
2020                                         ((rrd->vlan_tag & 8) << 9);
2021                         vlan_hwaccel_rx(skb, adapter->vlgrp, vlan_tag);
2022                 } else
2023                         netif_rx(skb);
2024
2025                 /* let protocol layer free skb */
2026                 buffer_info->skb = NULL;
2027                 buffer_info->alloced = 0;
2028                 rrd->xsz.valid = 0;
2029
2030                 adapter->netdev->last_rx = jiffies;
2031         }
2032
2033         atomic_set(&rrd_ring->next_to_clean, rrd_next_to_clean);
2034
2035         atl1_alloc_rx_buffers(adapter);
2036
2037         /* update mailbox ? */
2038         if (count) {
2039                 u32 tpd_next_to_use;
2040                 u32 rfd_next_to_use;
2041
2042                 spin_lock(&adapter->mb_lock);
2043
2044                 tpd_next_to_use = atomic_read(&adapter->tpd_ring.next_to_use);
2045                 rfd_next_to_use =
2046                     atomic_read(&adapter->rfd_ring.next_to_use);
2047                 rrd_next_to_clean =
2048                     atomic_read(&adapter->rrd_ring.next_to_clean);
2049                 value = ((rfd_next_to_use & MB_RFD_PROD_INDX_MASK) <<
2050                         MB_RFD_PROD_INDX_SHIFT) |
2051                         ((rrd_next_to_clean & MB_RRD_CONS_INDX_MASK) <<
2052                         MB_RRD_CONS_INDX_SHIFT) |
2053                         ((tpd_next_to_use & MB_TPD_PROD_INDX_MASK) <<
2054                         MB_TPD_PROD_INDX_SHIFT);
2055                 iowrite32(value, adapter->hw.hw_addr + REG_MAILBOX);
2056                 spin_unlock(&adapter->mb_lock);
2057         }
2058 }
2059
2060 static void atl1_intr_tx(struct atl1_adapter *adapter)
2061 {
2062         struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
2063         struct atl1_buffer *buffer_info;
2064         u16 sw_tpd_next_to_clean;
2065         u16 cmb_tpd_next_to_clean;
2066
2067         sw_tpd_next_to_clean = atomic_read(&tpd_ring->next_to_clean);
2068         cmb_tpd_next_to_clean = le16_to_cpu(adapter->cmb.cmb->tpd_cons_idx);
2069
2070         while (cmb_tpd_next_to_clean != sw_tpd_next_to_clean) {
2071                 struct tx_packet_desc *tpd;
2072
2073                 tpd = ATL1_TPD_DESC(tpd_ring, sw_tpd_next_to_clean);
2074                 buffer_info = &tpd_ring->buffer_info[sw_tpd_next_to_clean];
2075                 if (buffer_info->dma) {
2076                         pci_unmap_page(adapter->pdev, buffer_info->dma,
2077                                        buffer_info->length, PCI_DMA_TODEVICE);
2078                         buffer_info->dma = 0;
2079                 }
2080
2081                 if (buffer_info->skb) {
2082                         dev_kfree_skb_irq(buffer_info->skb);
2083                         buffer_info->skb = NULL;
2084                 }
2085
2086                 if (++sw_tpd_next_to_clean == tpd_ring->count)
2087                         sw_tpd_next_to_clean = 0;
2088         }
2089         atomic_set(&tpd_ring->next_to_clean, sw_tpd_next_to_clean);
2090
2091         if (netif_queue_stopped(adapter->netdev)
2092             && netif_carrier_ok(adapter->netdev))
2093                 netif_wake_queue(adapter->netdev);
2094 }
2095
2096 static u16 atl1_tpd_avail(struct atl1_tpd_ring *tpd_ring)
2097 {
2098         u16 next_to_clean = atomic_read(&tpd_ring->next_to_clean);
2099         u16 next_to_use = atomic_read(&tpd_ring->next_to_use);
2100         return ((next_to_clean > next_to_use) ?
2101                 next_to_clean - next_to_use - 1 :
2102                 tpd_ring->count + next_to_clean - next_to_use - 1);
2103 }
2104
2105 static int atl1_tso(struct atl1_adapter *adapter, struct sk_buff *skb,
2106         struct tx_packet_desc *ptpd)
2107 {
2108         u8 hdr_len, ip_off;
2109         u32 real_len;
2110         int err;
2111
2112         if (skb_shinfo(skb)->gso_size) {
2113                 if (skb_header_cloned(skb)) {
2114                         err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2115                         if (unlikely(err))
2116                                 return -1;
2117                 }
2118
2119                 if (skb->protocol == htons(ETH_P_IP)) {
2120                         struct iphdr *iph = ip_hdr(skb);
2121
2122                         real_len = (((unsigned char *)iph - skb->data) +
2123                                 ntohs(iph->tot_len));
2124                         if (real_len < skb->len)
2125                                 pskb_trim(skb, real_len);
2126                         hdr_len = (skb_transport_offset(skb) + tcp_hdrlen(skb));
2127                         if (skb->len == hdr_len) {
2128                                 iph->check = 0;
2129                                 tcp_hdr(skb)->check =
2130                                         ~csum_tcpudp_magic(iph->saddr,
2131                                         iph->daddr, tcp_hdrlen(skb),
2132                                         IPPROTO_TCP, 0);
2133                                 ptpd->word3 |= (iph->ihl & TPD_IPHL_MASK) <<
2134                                         TPD_IPHL_SHIFT;
2135                                 ptpd->word3 |= ((tcp_hdrlen(skb) >> 2) &
2136                                         TPD_TCPHDRLEN_MASK) <<
2137                                         TPD_TCPHDRLEN_SHIFT;
2138                                 ptpd->word3 |= 1 << TPD_IP_CSUM_SHIFT;
2139                                 ptpd->word3 |= 1 << TPD_TCP_CSUM_SHIFT;
2140                                 return 1;
2141                         }
2142
2143                         iph->check = 0;
2144                         tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2145                                         iph->daddr, 0, IPPROTO_TCP, 0);
2146                         ip_off = (unsigned char *)iph -
2147                                 (unsigned char *) skb_network_header(skb);
2148                         if (ip_off == 8) /* 802.3-SNAP frame */
2149                                 ptpd->word3 |= 1 << TPD_ETHTYPE_SHIFT;
2150                         else if (ip_off != 0)
2151                                 return -2;
2152
2153                         ptpd->word3 |= (iph->ihl & TPD_IPHL_MASK) <<
2154                                 TPD_IPHL_SHIFT;
2155                         ptpd->word3 |= ((tcp_hdrlen(skb) >> 2) &
2156                                 TPD_TCPHDRLEN_MASK) << TPD_TCPHDRLEN_SHIFT;
2157                         ptpd->word3 |= (skb_shinfo(skb)->gso_size &
2158                                 TPD_MSS_MASK) << TPD_MSS_SHIFT;
2159                         ptpd->word3 |= 1 << TPD_SEGMENT_EN_SHIFT;
2160                         return 3;
2161                 }
2162         }
2163         return false;
2164 }
2165
2166 static int atl1_tx_csum(struct atl1_adapter *adapter, struct sk_buff *skb,
2167         struct tx_packet_desc *ptpd)
2168 {
2169         u8 css, cso;
2170
2171         if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
2172                 css = (u8) (skb->csum_start - skb_headroom(skb));
2173                 cso = css + (u8) skb->csum_offset;
2174                 if (unlikely(css & 0x1)) {
2175                         /* L1 hardware requires an even number here */
2176                         if (netif_msg_tx_err(adapter))
2177                                 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
2178                                         "payload offset not an even number\n");
2179                         return -1;
2180                 }
2181                 ptpd->word3 |= (css & TPD_PLOADOFFSET_MASK) <<
2182                         TPD_PLOADOFFSET_SHIFT;
2183                 ptpd->word3 |= (cso & TPD_CCSUMOFFSET_MASK) <<
2184                         TPD_CCSUMOFFSET_SHIFT;
2185                 ptpd->word3 |= 1 << TPD_CUST_CSUM_EN_SHIFT;
2186                 return true;
2187         }
2188         return 0;
2189 }
2190
2191 static void atl1_tx_map(struct atl1_adapter *adapter, struct sk_buff *skb,
2192         struct tx_packet_desc *ptpd)
2193 {
2194         struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
2195         struct atl1_buffer *buffer_info;
2196         u16 buf_len = skb->len;
2197         struct page *page;
2198         unsigned long offset;
2199         unsigned int nr_frags;
2200         unsigned int f;
2201         int retval;
2202         u16 next_to_use;
2203         u16 data_len;
2204         u8 hdr_len;
2205
2206         buf_len -= skb->data_len;
2207         nr_frags = skb_shinfo(skb)->nr_frags;
2208         next_to_use = atomic_read(&tpd_ring->next_to_use);
2209         buffer_info = &tpd_ring->buffer_info[next_to_use];
2210         if (unlikely(buffer_info->skb))
2211                 BUG();
2212         /* put skb in last TPD */
2213         buffer_info->skb = NULL;
2214
2215         retval = (ptpd->word3 >> TPD_SEGMENT_EN_SHIFT) & TPD_SEGMENT_EN_MASK;
2216         if (retval) {
2217                 /* TSO */
2218                 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2219                 buffer_info->length = hdr_len;
2220                 page = virt_to_page(skb->data);
2221                 offset = (unsigned long)skb->data & ~PAGE_MASK;
2222                 buffer_info->dma = pci_map_page(adapter->pdev, page,
2223                                                 offset, hdr_len,
2224                                                 PCI_DMA_TODEVICE);
2225
2226                 if (++next_to_use == tpd_ring->count)
2227                         next_to_use = 0;
2228
2229                 if (buf_len > hdr_len) {
2230                         int i, nseg;
2231
2232                         data_len = buf_len - hdr_len;
2233                         nseg = (data_len + ATL1_MAX_TX_BUF_LEN - 1) /
2234                                 ATL1_MAX_TX_BUF_LEN;
2235                         for (i = 0; i < nseg; i++) {
2236                                 buffer_info =
2237                                     &tpd_ring->buffer_info[next_to_use];
2238                                 buffer_info->skb = NULL;
2239                                 buffer_info->length =
2240                                     (ATL1_MAX_TX_BUF_LEN >=
2241                                      data_len) ? ATL1_MAX_TX_BUF_LEN : data_len;
2242                                 data_len -= buffer_info->length;
2243                                 page = virt_to_page(skb->data +
2244                                         (hdr_len + i * ATL1_MAX_TX_BUF_LEN));
2245                                 offset = (unsigned long)(skb->data +
2246                                         (hdr_len + i * ATL1_MAX_TX_BUF_LEN)) &
2247                                         ~PAGE_MASK;
2248                                 buffer_info->dma = pci_map_page(adapter->pdev,
2249                                         page, offset, buffer_info->length,
2250                                         PCI_DMA_TODEVICE);
2251                                 if (++next_to_use == tpd_ring->count)
2252                                         next_to_use = 0;
2253                         }
2254                 }
2255         } else {
2256                 /* not TSO */
2257                 buffer_info->length = buf_len;
2258                 page = virt_to_page(skb->data);
2259                 offset = (unsigned long)skb->data & ~PAGE_MASK;
2260                 buffer_info->dma = pci_map_page(adapter->pdev, page,
2261                         offset, buf_len, PCI_DMA_TODEVICE);
2262                 if (++next_to_use == tpd_ring->count)
2263                         next_to_use = 0;
2264         }
2265
2266         for (f = 0; f < nr_frags; f++) {
2267                 struct skb_frag_struct *frag;
2268                 u16 i, nseg;
2269
2270                 frag = &skb_shinfo(skb)->frags[f];
2271                 buf_len = frag->size;
2272
2273                 nseg = (buf_len + ATL1_MAX_TX_BUF_LEN - 1) /
2274                         ATL1_MAX_TX_BUF_LEN;
2275                 for (i = 0; i < nseg; i++) {
2276                         buffer_info = &tpd_ring->buffer_info[next_to_use];
2277                         if (unlikely(buffer_info->skb))
2278                                 BUG();
2279                         buffer_info->skb = NULL;
2280                         buffer_info->length = (buf_len > ATL1_MAX_TX_BUF_LEN) ?
2281                                 ATL1_MAX_TX_BUF_LEN : buf_len;
2282                         buf_len -= buffer_info->length;
2283                         buffer_info->dma = pci_map_page(adapter->pdev,
2284                                 frag->page,
2285                                 frag->page_offset + (i * ATL1_MAX_TX_BUF_LEN),
2286                                 buffer_info->length, PCI_DMA_TODEVICE);
2287
2288                         if (++next_to_use == tpd_ring->count)
2289                                 next_to_use = 0;
2290                 }
2291         }
2292
2293         /* last tpd's buffer-info */
2294         buffer_info->skb = skb;
2295 }
2296
2297 static void atl1_tx_queue(struct atl1_adapter *adapter, u16 count,
2298        struct tx_packet_desc *ptpd)
2299 {
2300         struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
2301         struct atl1_buffer *buffer_info;
2302         struct tx_packet_desc *tpd;
2303         u16 j;
2304         u32 val;
2305         u16 next_to_use = (u16) atomic_read(&tpd_ring->next_to_use);
2306
2307         for (j = 0; j < count; j++) {
2308                 buffer_info = &tpd_ring->buffer_info[next_to_use];
2309                 tpd = ATL1_TPD_DESC(&adapter->tpd_ring, next_to_use);
2310                 if (tpd != ptpd)
2311                         memcpy(tpd, ptpd, sizeof(struct tx_packet_desc));
2312                 tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
2313                 tpd->word2 = (cpu_to_le16(buffer_info->length) &
2314                         TPD_BUFLEN_MASK) << TPD_BUFLEN_SHIFT;
2315
2316                 /*
2317                  * if this is the first packet in a TSO chain, set
2318                  * TPD_HDRFLAG, otherwise, clear it.
2319                  */
2320                 val = (tpd->word3 >> TPD_SEGMENT_EN_SHIFT) &
2321                         TPD_SEGMENT_EN_MASK;
2322                 if (val) {
2323                         if (!j)
2324                                 tpd->word3 |= 1 << TPD_HDRFLAG_SHIFT;
2325                         else
2326                                 tpd->word3 &= ~(1 << TPD_HDRFLAG_SHIFT);
2327                 }
2328
2329                 if (j == (count - 1))
2330                         tpd->word3 |= 1 << TPD_EOP_SHIFT;
2331
2332                 if (++next_to_use == tpd_ring->count)
2333                         next_to_use = 0;
2334         }
2335         /*
2336          * Force memory writes to complete before letting h/w
2337          * know there are new descriptors to fetch.  (Only
2338          * applicable for weak-ordered memory model archs,
2339          * such as IA-64).
2340          */
2341         wmb();
2342
2343         atomic_set(&tpd_ring->next_to_use, next_to_use);
2344 }
2345
2346 static int atl1_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2347 {
2348         struct atl1_adapter *adapter = netdev_priv(netdev);
2349         struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
2350         int len = skb->len;
2351         int tso;
2352         int count = 1;
2353         int ret_val;
2354         struct tx_packet_desc *ptpd;
2355         u16 frag_size;
2356         u16 vlan_tag;
2357         unsigned int nr_frags = 0;
2358         unsigned int mss = 0;
2359         unsigned int f;
2360         unsigned int proto_hdr_len;
2361
2362         len -= skb->data_len;
2363
2364         if (unlikely(skb->len <= 0)) {
2365                 dev_kfree_skb_any(skb);
2366                 return NETDEV_TX_OK;
2367         }
2368
2369         nr_frags = skb_shinfo(skb)->nr_frags;
2370         for (f = 0; f < nr_frags; f++) {
2371                 frag_size = skb_shinfo(skb)->frags[f].size;
2372                 if (frag_size)
2373                         count += (frag_size + ATL1_MAX_TX_BUF_LEN - 1) /
2374                                 ATL1_MAX_TX_BUF_LEN;
2375         }
2376
2377         mss = skb_shinfo(skb)->gso_size;
2378         if (mss) {
2379                 if (skb->protocol == ntohs(ETH_P_IP)) {
2380                         proto_hdr_len = (skb_transport_offset(skb) +
2381                                          tcp_hdrlen(skb));
2382                         if (unlikely(proto_hdr_len > len)) {
2383                                 dev_kfree_skb_any(skb);
2384                                 return NETDEV_TX_OK;
2385                         }
2386                         /* need additional TPD ? */
2387                         if (proto_hdr_len != len)
2388                                 count += (len - proto_hdr_len +
2389                                         ATL1_MAX_TX_BUF_LEN - 1) /
2390                                         ATL1_MAX_TX_BUF_LEN;
2391                 }
2392         }
2393
2394         if (atl1_tpd_avail(&adapter->tpd_ring) < count) {
2395                 /* not enough descriptors */
2396                 netif_stop_queue(netdev);
2397                 if (netif_msg_tx_queued(adapter))
2398                         dev_printk(KERN_DEBUG, &adapter->pdev->dev,
2399                                 "tx busy\n");
2400                 return NETDEV_TX_BUSY;
2401         }
2402
2403         ptpd = ATL1_TPD_DESC(tpd_ring,
2404                 (u16) atomic_read(&tpd_ring->next_to_use));
2405         memset(ptpd, 0, sizeof(struct tx_packet_desc));
2406
2407         if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
2408                 vlan_tag = vlan_tx_tag_get(skb);
2409                 vlan_tag = (vlan_tag << 4) | (vlan_tag >> 13) |
2410                         ((vlan_tag >> 9) & 0x8);
2411                 ptpd->word3 |= 1 << TPD_INS_VL_TAG_SHIFT;
2412                 ptpd->word3 |= (vlan_tag & TPD_VL_TAGGED_MASK) <<
2413                         TPD_VL_TAGGED_SHIFT;
2414         }
2415
2416         tso = atl1_tso(adapter, skb, ptpd);
2417         if (tso < 0) {
2418                 dev_kfree_skb_any(skb);
2419                 return NETDEV_TX_OK;
2420         }
2421
2422         if (!tso) {
2423                 ret_val = atl1_tx_csum(adapter, skb, ptpd);
2424                 if (ret_val < 0) {
2425                         dev_kfree_skb_any(skb);
2426                         return NETDEV_TX_OK;
2427                 }
2428         }
2429
2430         atl1_tx_map(adapter, skb, ptpd);
2431         atl1_tx_queue(adapter, count, ptpd);
2432         atl1_update_mailbox(adapter);
2433         mmiowb();
2434         netdev->trans_start = jiffies;
2435         return NETDEV_TX_OK;
2436 }
2437
2438 /*
2439  * atl1_intr - Interrupt Handler
2440  * @irq: interrupt number
2441  * @data: pointer to a network interface device structure
2442  * @pt_regs: CPU registers structure
2443  */
2444 static irqreturn_t atl1_intr(int irq, void *data)
2445 {
2446         struct atl1_adapter *adapter = netdev_priv(data);
2447         u32 status;
2448         int max_ints = 10;
2449
2450         status = adapter->cmb.cmb->int_stats;
2451         if (!status)
2452                 return IRQ_NONE;
2453
2454         do {
2455                 /* clear CMB interrupt status at once */
2456                 adapter->cmb.cmb->int_stats = 0;
2457
2458                 if (status & ISR_GPHY)  /* clear phy status */
2459                         atlx_clear_phy_int(adapter);
2460
2461                 /* clear ISR status, and Enable CMB DMA/Disable Interrupt */
2462                 iowrite32(status | ISR_DIS_INT, adapter->hw.hw_addr + REG_ISR);
2463
2464                 /* check if SMB intr */
2465                 if (status & ISR_SMB)
2466                         atl1_inc_smb(adapter);
2467
2468                 /* check if PCIE PHY Link down */
2469                 if (status & ISR_PHY_LINKDOWN) {
2470                         if (netif_msg_intr(adapter))
2471                                 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
2472                                         "pcie phy link down %x\n", status);
2473                         if (netif_running(adapter->netdev)) {   /* reset MAC */
2474                                 iowrite32(0, adapter->hw.hw_addr + REG_IMR);
2475                                 schedule_work(&adapter->pcie_dma_to_rst_task);
2476                                 return IRQ_HANDLED;
2477                         }
2478                 }
2479
2480                 /* check if DMA read/write error ? */
2481                 if (status & (ISR_DMAR_TO_RST | ISR_DMAW_TO_RST)) {
2482                         if (netif_msg_intr(adapter))
2483                                 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
2484                                         "pcie DMA r/w error (status = 0x%x)\n",
2485                                         status);
2486                         iowrite32(0, adapter->hw.hw_addr + REG_IMR);
2487                         schedule_work(&adapter->pcie_dma_to_rst_task);
2488                         return IRQ_HANDLED;
2489                 }
2490
2491                 /* link event */
2492                 if (status & ISR_GPHY) {
2493                         adapter->soft_stats.tx_carrier_errors++;
2494                         atl1_check_for_link(adapter);
2495                 }
2496
2497                 /* transmit event */
2498                 if (status & ISR_CMB_TX)
2499                         atl1_intr_tx(adapter);
2500
2501                 /* rx exception */
2502                 if (unlikely(status & (ISR_RXF_OV | ISR_RFD_UNRUN |
2503                         ISR_RRD_OV | ISR_HOST_RFD_UNRUN |
2504                         ISR_HOST_RRD_OV | ISR_CMB_RX))) {
2505                         if (status & (ISR_RXF_OV | ISR_RFD_UNRUN |
2506                                 ISR_RRD_OV | ISR_HOST_RFD_UNRUN |
2507                                 ISR_HOST_RRD_OV))
2508                                 if (netif_msg_intr(adapter))
2509                                         dev_printk(KERN_DEBUG,
2510                                                 &adapter->pdev->dev,
2511                                                 "rx exception, ISR = 0x%x\n",
2512                                                 status);
2513                         atl1_intr_rx(adapter);
2514                 }
2515
2516                 if (--max_ints < 0)
2517                         break;
2518
2519         } while ((status = adapter->cmb.cmb->int_stats));
2520
2521         /* re-enable Interrupt */
2522         iowrite32(ISR_DIS_SMB | ISR_DIS_DMA, adapter->hw.hw_addr + REG_ISR);
2523         return IRQ_HANDLED;
2524 }
2525
2526 /*
2527  * atl1_watchdog - Timer Call-back
2528  * @data: pointer to netdev cast into an unsigned long
2529  */
2530 static void atl1_watchdog(unsigned long data)
2531 {
2532         struct atl1_adapter *adapter = (struct atl1_adapter *)data;
2533
2534         /* Reset the timer */
2535         mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
2536 }
2537
2538 /*
2539  * atl1_phy_config - Timer Call-back
2540  * @data: pointer to netdev cast into an unsigned long
2541  */
2542 static void atl1_phy_config(unsigned long data)
2543 {
2544         struct atl1_adapter *adapter = (struct atl1_adapter *)data;
2545         struct atl1_hw *hw = &adapter->hw;
2546         unsigned long flags;
2547
2548         spin_lock_irqsave(&adapter->lock, flags);
2549         adapter->phy_timer_pending = false;
2550         atl1_write_phy_reg(hw, MII_ADVERTISE, hw->mii_autoneg_adv_reg);
2551         atl1_write_phy_reg(hw, MII_ATLX_CR, hw->mii_1000t_ctrl_reg);
2552         atl1_write_phy_reg(hw, MII_BMCR, MII_CR_RESET | MII_CR_AUTO_NEG_EN);
2553         spin_unlock_irqrestore(&adapter->lock, flags);
2554 }
2555
2556 /*
2557  * Orphaned vendor comment left intact here:
2558  * <vendor comment>
2559  * If TPD Buffer size equal to 0, PCIE DMAR_TO_INT
2560  * will assert. We do soft reset <0x1400=1> according
2561  * with the SPEC. BUT, it seemes that PCIE or DMA
2562  * state-machine will not be reset. DMAR_TO_INT will
2563  * assert again and again.
2564  * </vendor comment>
2565  */
2566
2567 static int atl1_reset(struct atl1_adapter *adapter)
2568 {
2569         int ret;
2570         ret = atl1_reset_hw(&adapter->hw);
2571         if (ret)
2572                 return ret;
2573         return atl1_init_hw(&adapter->hw);
2574 }
2575
2576 static s32 atl1_up(struct atl1_adapter *adapter)
2577 {
2578         struct net_device *netdev = adapter->netdev;
2579         int err;
2580         int irq_flags = IRQF_SAMPLE_RANDOM;
2581
2582         /* hardware has been reset, we need to reload some things */
2583         atlx_set_multi(netdev);
2584         atl1_init_ring_ptrs(adapter);
2585         atlx_restore_vlan(adapter);
2586         err = atl1_alloc_rx_buffers(adapter);
2587         if (unlikely(!err))
2588                 /* no RX BUFFER allocated */
2589                 return -ENOMEM;
2590
2591         if (unlikely(atl1_configure(adapter))) {
2592                 err = -EIO;
2593                 goto err_up;
2594         }
2595
2596         err = pci_enable_msi(adapter->pdev);
2597         if (err) {
2598                 if (netif_msg_ifup(adapter))
2599                         dev_info(&adapter->pdev->dev,
2600                                 "Unable to enable MSI: %d\n", err);
2601                 irq_flags |= IRQF_SHARED;
2602         }
2603
2604         err = request_irq(adapter->pdev->irq, &atl1_intr, irq_flags,
2605                         netdev->name, netdev);
2606         if (unlikely(err))
2607                 goto err_up;
2608
2609         mod_timer(&adapter->watchdog_timer, jiffies);
2610         atlx_irq_enable(adapter);
2611         atl1_check_link(adapter);
2612         netif_start_queue(netdev);
2613         return 0;
2614
2615 err_up:
2616         pci_disable_msi(adapter->pdev);
2617         /* free rx_buffers */
2618         atl1_clean_rx_ring(adapter);
2619         return err;
2620 }
2621
2622 static void atl1_down(struct atl1_adapter *adapter)
2623 {
2624         struct net_device *netdev = adapter->netdev;
2625
2626         netif_stop_queue(netdev);
2627         del_timer_sync(&adapter->watchdog_timer);
2628         del_timer_sync(&adapter->phy_config_timer);
2629         adapter->phy_timer_pending = false;
2630
2631         atlx_irq_disable(adapter);
2632         free_irq(adapter->pdev->irq, netdev);
2633         pci_disable_msi(adapter->pdev);
2634         atl1_reset_hw(&adapter->hw);
2635         adapter->cmb.cmb->int_stats = 0;
2636
2637         adapter->link_speed = SPEED_0;
2638         adapter->link_duplex = -1;
2639         netif_carrier_off(netdev);
2640
2641         atl1_clean_tx_ring(adapter);
2642         atl1_clean_rx_ring(adapter);
2643 }
2644
2645 static void atl1_tx_timeout_task(struct work_struct *work)
2646 {
2647         struct atl1_adapter *adapter =
2648                 container_of(work, struct atl1_adapter, tx_timeout_task);
2649         struct net_device *netdev = adapter->netdev;
2650
2651         netif_device_detach(netdev);
2652         atl1_down(adapter);
2653         atl1_up(adapter);
2654         netif_device_attach(netdev);
2655 }
2656
2657 /*
2658  * atl1_change_mtu - Change the Maximum Transfer Unit
2659  * @netdev: network interface device structure
2660  * @new_mtu: new value for maximum frame size
2661  *
2662  * Returns 0 on success, negative on failure
2663  */
2664 static int atl1_change_mtu(struct net_device *netdev, int new_mtu)
2665 {
2666         struct atl1_adapter *adapter = netdev_priv(netdev);
2667         int old_mtu = netdev->mtu;
2668         int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2669
2670         if ((max_frame < ETH_ZLEN + ETH_FCS_LEN) ||
2671             (max_frame > MAX_JUMBO_FRAME_SIZE)) {
2672                 if (netif_msg_link(adapter))
2673                         dev_warn(&adapter->pdev->dev, "invalid MTU setting\n");
2674                 return -EINVAL;
2675         }
2676
2677         adapter->hw.max_frame_size = max_frame;
2678         adapter->hw.tx_jumbo_task_th = (max_frame + 7) >> 3;
2679         adapter->rx_buffer_len = (max_frame + 7) & ~7;
2680         adapter->hw.rx_jumbo_th = adapter->rx_buffer_len / 8;
2681
2682         netdev->mtu = new_mtu;
2683         if ((old_mtu != new_mtu) && netif_running(netdev)) {
2684                 atl1_down(adapter);
2685                 atl1_up(adapter);
2686         }
2687
2688         return 0;
2689 }
2690
2691 /*
2692  * atl1_open - Called when a network interface is made active
2693  * @netdev: network interface device structure
2694  *
2695  * Returns 0 on success, negative value on failure
2696  *
2697  * The open entry point is called when a network interface is made
2698  * active by the system (IFF_UP).  At this point all resources needed
2699  * for transmit and receive operations are allocated, the interrupt
2700  * handler is registered with the OS, the watchdog timer is started,
2701  * and the stack is notified that the interface is ready.
2702  */
2703 static int atl1_open(struct net_device *netdev)
2704 {
2705         struct atl1_adapter *adapter = netdev_priv(netdev);
2706         int err;
2707
2708         netif_carrier_off(netdev);
2709
2710         /* allocate transmit descriptors */
2711         err = atl1_setup_ring_resources(adapter);
2712         if (err)
2713                 return err;
2714
2715         err = atl1_up(adapter);
2716         if (err)
2717                 goto err_up;
2718
2719         return 0;
2720
2721 err_up:
2722         atl1_reset(adapter);
2723         return err;
2724 }
2725
2726 /*
2727  * atl1_close - Disables a network interface
2728  * @netdev: network interface device structure
2729  *
2730  * Returns 0, this is not allowed to fail
2731  *
2732  * The close entry point is called when an interface is de-activated
2733  * by the OS.  The hardware is still under the drivers control, but
2734  * needs to be disabled.  A global MAC reset is issued to stop the
2735  * hardware, and all transmit and receive resources are freed.
2736  */
2737 static int atl1_close(struct net_device *netdev)
2738 {
2739         struct atl1_adapter *adapter = netdev_priv(netdev);
2740         atl1_down(adapter);
2741         atl1_free_ring_resources(adapter);
2742         return 0;
2743 }
2744
2745 #ifdef CONFIG_PM
2746 static int atl1_suspend(struct pci_dev *pdev, pm_message_t state)
2747 {
2748         struct net_device *netdev = pci_get_drvdata(pdev);
2749         struct atl1_adapter *adapter = netdev_priv(netdev);
2750         struct atl1_hw *hw = &adapter->hw;
2751         u32 ctrl = 0;
2752         u32 wufc = adapter->wol;
2753         u32 val;
2754         int retval;
2755         u16 speed;
2756         u16 duplex;
2757
2758         netif_device_detach(netdev);
2759         if (netif_running(netdev))
2760                 atl1_down(adapter);
2761
2762         retval = pci_save_state(pdev);
2763         if (retval)
2764                 return retval;
2765
2766         atl1_read_phy_reg(hw, MII_BMSR, (u16 *) & ctrl);
2767         atl1_read_phy_reg(hw, MII_BMSR, (u16 *) & ctrl);
2768         val = ctrl & BMSR_LSTATUS;
2769         if (val)
2770                 wufc &= ~ATLX_WUFC_LNKC;
2771
2772         if (val && wufc) {
2773                 val = atl1_get_speed_and_duplex(hw, &speed, &duplex);
2774                 if (val) {
2775                         if (netif_msg_ifdown(adapter))
2776                                 dev_printk(KERN_DEBUG, &pdev->dev,
2777                                         "error getting speed/duplex\n");
2778                         goto disable_wol;
2779                 }
2780
2781                 ctrl = 0;
2782
2783                 /* enable magic packet WOL */
2784                 if (wufc & ATLX_WUFC_MAG)
2785                         ctrl |= (WOL_MAGIC_EN | WOL_MAGIC_PME_EN);
2786                 iowrite32(ctrl, hw->hw_addr + REG_WOL_CTRL);
2787                 ioread32(hw->hw_addr + REG_WOL_CTRL);
2788
2789                 /* configure the mac */
2790                 ctrl = MAC_CTRL_RX_EN;
2791                 ctrl |= ((u32)((speed == SPEED_1000) ? MAC_CTRL_SPEED_1000 :
2792                         MAC_CTRL_SPEED_10_100) << MAC_CTRL_SPEED_SHIFT);
2793                 if (duplex == FULL_DUPLEX)
2794                         ctrl |= MAC_CTRL_DUPLX;
2795                 ctrl |= (((u32)adapter->hw.preamble_len &
2796                         MAC_CTRL_PRMLEN_MASK) << MAC_CTRL_PRMLEN_SHIFT);
2797                 if (adapter->vlgrp)
2798                         ctrl |= MAC_CTRL_RMV_VLAN;
2799                 if (wufc & ATLX_WUFC_MAG)
2800                         ctrl |= MAC_CTRL_BC_EN;
2801                 iowrite32(ctrl, hw->hw_addr + REG_MAC_CTRL);
2802                 ioread32(hw->hw_addr + REG_MAC_CTRL);
2803
2804                 /* poke the PHY */
2805                 ctrl = ioread32(hw->hw_addr + REG_PCIE_PHYMISC);
2806                 ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
2807                 iowrite32(ctrl, hw->hw_addr + REG_PCIE_PHYMISC);
2808                 ioread32(hw->hw_addr + REG_PCIE_PHYMISC);
2809
2810                 pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
2811                 goto exit;
2812         }
2813
2814         if (!val && wufc) {
2815                 ctrl |= (WOL_LINK_CHG_EN | WOL_LINK_CHG_PME_EN);
2816                 iowrite32(ctrl, hw->hw_addr + REG_WOL_CTRL);
2817                 ioread32(hw->hw_addr + REG_WOL_CTRL);
2818                 iowrite32(0, hw->hw_addr + REG_MAC_CTRL);
2819                 ioread32(hw->hw_addr + REG_MAC_CTRL);
2820                 hw->phy_configured = false;
2821                 pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
2822                 goto exit;
2823         }
2824
2825 disable_wol:
2826         iowrite32(0, hw->hw_addr + REG_WOL_CTRL);
2827         ioread32(hw->hw_addr + REG_WOL_CTRL);
2828         ctrl = ioread32(hw->hw_addr + REG_PCIE_PHYMISC);
2829         ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
2830         iowrite32(ctrl, hw->hw_addr + REG_PCIE_PHYMISC);
2831         ioread32(hw->hw_addr + REG_PCIE_PHYMISC);
2832         hw->phy_configured = false;
2833         pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
2834 exit:
2835         if (netif_running(netdev))
2836                 pci_disable_msi(adapter->pdev);
2837         pci_disable_device(pdev);
2838         pci_set_power_state(pdev, pci_choose_state(pdev, state));
2839
2840         return 0;
2841 }
2842
2843 static int atl1_resume(struct pci_dev *pdev)
2844 {
2845         struct net_device *netdev = pci_get_drvdata(pdev);
2846         struct atl1_adapter *adapter = netdev_priv(netdev);
2847         u32 err;
2848
2849         pci_set_power_state(pdev, PCI_D0);
2850         pci_restore_state(pdev);
2851
2852         err = pci_enable_device(pdev);
2853         if (err) {
2854                 if (netif_msg_ifup(adapter))
2855                         dev_printk(KERN_DEBUG, &pdev->dev,
2856                                 "error enabling pci device\n");
2857                 return err;
2858         }
2859
2860         pci_set_master(pdev);
2861         iowrite32(0, adapter->hw.hw_addr + REG_WOL_CTRL);
2862         pci_enable_wake(pdev, PCI_D3hot, 0);
2863         pci_enable_wake(pdev, PCI_D3cold, 0);
2864
2865         atl1_reset_hw(&adapter->hw);
2866         adapter->cmb.cmb->int_stats = 0;
2867
2868         if (netif_running(netdev))
2869                 atl1_up(adapter);
2870         netif_device_attach(netdev);
2871
2872         return 0;
2873 }
2874 #else
2875 #define atl1_suspend NULL
2876 #define atl1_resume NULL
2877 #endif
2878
2879 static void atl1_shutdown(struct pci_dev *pdev)
2880 {
2881 #ifdef CONFIG_PM
2882         atl1_suspend(pdev, PMSG_SUSPEND);
2883 #endif
2884 }
2885
2886 #ifdef CONFIG_NET_POLL_CONTROLLER
2887 static void atl1_poll_controller(struct net_device *netdev)
2888 {
2889         disable_irq(netdev->irq);
2890         atl1_intr(netdev->irq, netdev);
2891         enable_irq(netdev->irq);
2892 }
2893 #endif
2894
2895 /*
2896  * atl1_probe - Device Initialization Routine
2897  * @pdev: PCI device information struct
2898  * @ent: entry in atl1_pci_tbl
2899  *
2900  * Returns 0 on success, negative on failure
2901  *
2902  * atl1_probe initializes an adapter identified by a pci_dev structure.
2903  * The OS initialization, configuring of the adapter private structure,
2904  * and a hardware reset occur.
2905  */
2906 static int __devinit atl1_probe(struct pci_dev *pdev,
2907         const struct pci_device_id *ent)
2908 {
2909         struct net_device *netdev;
2910         struct atl1_adapter *adapter;
2911         static int cards_found = 0;
2912         int err;
2913
2914         err = pci_enable_device(pdev);
2915         if (err)
2916                 return err;
2917
2918         /*
2919          * The atl1 chip can DMA to 64-bit addresses, but it uses a single
2920          * shared register for the high 32 bits, so only a single, aligned,
2921          * 4 GB physical address range can be used at a time.
2922          *
2923          * Supporting 64-bit DMA on this hardware is more trouble than it's
2924          * worth.  It is far easier to limit to 32-bit DMA than update
2925          * various kernel subsystems to support the mechanics required by a
2926          * fixed-high-32-bit system.
2927          */
2928         err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
2929         if (err) {
2930                 dev_err(&pdev->dev, "no usable DMA configuration\n");
2931                 goto err_dma;
2932         }
2933         /*
2934          * Mark all PCI regions associated with PCI device
2935          * pdev as being reserved by owner atl1_driver_name
2936          */
2937         err = pci_request_regions(pdev, ATLX_DRIVER_NAME);
2938         if (err)
2939                 goto err_request_regions;
2940
2941         /*
2942          * Enables bus-mastering on the device and calls
2943          * pcibios_set_master to do the needed arch specific settings
2944          */
2945         pci_set_master(pdev);
2946
2947         netdev = alloc_etherdev(sizeof(struct atl1_adapter));
2948         if (!netdev) {
2949                 err = -ENOMEM;
2950                 goto err_alloc_etherdev;
2951         }
2952         SET_NETDEV_DEV(netdev, &pdev->dev);
2953
2954         pci_set_drvdata(pdev, netdev);
2955         adapter = netdev_priv(netdev);
2956         adapter->netdev = netdev;
2957         adapter->pdev = pdev;
2958         adapter->hw.back = adapter;
2959         adapter->msg_enable = netif_msg_init(debug, atl1_default_msg);
2960
2961         adapter->hw.hw_addr = pci_iomap(pdev, 0, 0);
2962         if (!adapter->hw.hw_addr) {
2963                 err = -EIO;
2964                 goto err_pci_iomap;
2965         }
2966         /* get device revision number */
2967         adapter->hw.dev_rev = ioread16(adapter->hw.hw_addr +
2968                 (REG_MASTER_CTRL + 2));
2969         if (netif_msg_probe(adapter))
2970                 dev_info(&pdev->dev, "version %s\n", ATLX_DRIVER_VERSION);
2971
2972         /* set default ring resource counts */
2973         adapter->rfd_ring.count = adapter->rrd_ring.count = ATL1_DEFAULT_RFD;
2974         adapter->tpd_ring.count = ATL1_DEFAULT_TPD;
2975
2976         adapter->mii.dev = netdev;
2977         adapter->mii.mdio_read = mdio_read;
2978         adapter->mii.mdio_write = mdio_write;
2979         adapter->mii.phy_id_mask = 0x1f;
2980         adapter->mii.reg_num_mask = 0x1f;
2981
2982         netdev->open = &atl1_open;
2983         netdev->stop = &atl1_close;
2984         netdev->hard_start_xmit = &atl1_xmit_frame;
2985         netdev->get_stats = &atlx_get_stats;
2986         netdev->set_multicast_list = &atlx_set_multi;
2987         netdev->set_mac_address = &atl1_set_mac;
2988         netdev->change_mtu = &atl1_change_mtu;
2989         netdev->do_ioctl = &atlx_ioctl;
2990         netdev->tx_timeout = &atlx_tx_timeout;
2991         netdev->watchdog_timeo = 5 * HZ;
2992 #ifdef CONFIG_NET_POLL_CONTROLLER
2993         netdev->poll_controller = atl1_poll_controller;
2994 #endif
2995         netdev->vlan_rx_register = atlx_vlan_rx_register;
2996
2997         netdev->ethtool_ops = &atl1_ethtool_ops;
2998         adapter->bd_number = cards_found;
2999
3000         /* setup the private structure */
3001         err = atl1_sw_init(adapter);
3002         if (err)
3003                 goto err_common;
3004
3005         netdev->features = NETIF_F_HW_CSUM;
3006         netdev->features |= NETIF_F_SG;
3007         netdev->features |= (NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX);
3008
3009         /*
3010          * patch for some L1 of old version,
3011          * the final version of L1 may not need these
3012          * patches
3013          */
3014         /* atl1_pcie_patch(adapter); */
3015
3016         /* really reset GPHY core */
3017         iowrite16(0, adapter->hw.hw_addr + REG_PHY_ENABLE);
3018
3019         /*
3020          * reset the controller to
3021          * put the device in a known good starting state
3022          */
3023         if (atl1_reset_hw(&adapter->hw)) {
3024                 err = -EIO;
3025                 goto err_common;
3026         }
3027
3028         /* copy the MAC address out of the EEPROM */
3029         atl1_read_mac_addr(&adapter->hw);
3030         memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
3031
3032         if (!is_valid_ether_addr(netdev->dev_addr)) {
3033                 err = -EIO;
3034                 goto err_common;
3035         }
3036
3037         atl1_check_options(adapter);
3038
3039         /* pre-init the MAC, and setup link */
3040         err = atl1_init_hw(&adapter->hw);
3041         if (err) {
3042                 err = -EIO;
3043                 goto err_common;
3044         }
3045
3046         atl1_pcie_patch(adapter);
3047         /* assume we have no link for now */
3048         netif_carrier_off(netdev);
3049         netif_stop_queue(netdev);
3050
3051         init_timer(&adapter->watchdog_timer);
3052         adapter->watchdog_timer.function = &atl1_watchdog;
3053         adapter->watchdog_timer.data = (unsigned long)adapter;
3054
3055         init_timer(&adapter->phy_config_timer);
3056         adapter->phy_config_timer.function = &atl1_phy_config;
3057         adapter->phy_config_timer.data = (unsigned long)adapter;
3058         adapter->phy_timer_pending = false;
3059
3060         INIT_WORK(&adapter->tx_timeout_task, atl1_tx_timeout_task);
3061
3062         INIT_WORK(&adapter->link_chg_task, atlx_link_chg_task);
3063
3064         INIT_WORK(&adapter->pcie_dma_to_rst_task, atl1_tx_timeout_task);
3065
3066         err = register_netdev(netdev);
3067         if (err)
3068                 goto err_common;
3069
3070         cards_found++;
3071         atl1_via_workaround(adapter);
3072         return 0;
3073
3074 err_common:
3075         pci_iounmap(pdev, adapter->hw.hw_addr);
3076 err_pci_iomap:
3077         free_netdev(netdev);
3078 err_alloc_etherdev:
3079         pci_release_regions(pdev);
3080 err_dma:
3081 err_request_regions:
3082         pci_disable_device(pdev);
3083         return err;
3084 }
3085
3086 /*
3087  * atl1_remove - Device Removal Routine
3088  * @pdev: PCI device information struct
3089  *
3090  * atl1_remove is called by the PCI subsystem to alert the driver
3091  * that it should release a PCI device.  The could be caused by a
3092  * Hot-Plug event, or because the driver is going to be removed from
3093  * memory.
3094  */
3095 static void __devexit atl1_remove(struct pci_dev *pdev)
3096 {
3097         struct net_device *netdev = pci_get_drvdata(pdev);
3098         struct atl1_adapter *adapter;
3099         /* Device not available. Return. */
3100         if (!netdev)
3101                 return;
3102
3103         adapter = netdev_priv(netdev);
3104
3105         /*
3106          * Some atl1 boards lack persistent storage for their MAC, and get it
3107          * from the BIOS during POST.  If we've been messing with the MAC
3108          * address, we need to save the permanent one.
3109          */
3110         if (memcmp(adapter->hw.mac_addr, adapter->hw.perm_mac_addr, ETH_ALEN)) {
3111                 memcpy(adapter->hw.mac_addr, adapter->hw.perm_mac_addr,
3112                         ETH_ALEN);
3113                 atl1_set_mac_addr(&adapter->hw);
3114         }
3115
3116         iowrite16(0, adapter->hw.hw_addr + REG_PHY_ENABLE);
3117         unregister_netdev(netdev);
3118         pci_iounmap(pdev, adapter->hw.hw_addr);
3119         pci_release_regions(pdev);
3120         free_netdev(netdev);
3121         pci_disable_device(pdev);
3122 }
3123
3124 static struct pci_driver atl1_driver = {
3125         .name = ATLX_DRIVER_NAME,
3126         .id_table = atl1_pci_tbl,
3127         .probe = atl1_probe,
3128         .remove = __devexit_p(atl1_remove),
3129         .suspend = atl1_suspend,
3130         .resume = atl1_resume,
3131         .shutdown = atl1_shutdown
3132 };
3133
3134 /*
3135  * atl1_exit_module - Driver Exit Cleanup Routine
3136  *
3137  * atl1_exit_module is called just before the driver is removed
3138  * from memory.
3139  */
3140 static void __exit atl1_exit_module(void)
3141 {
3142         pci_unregister_driver(&atl1_driver);
3143 }
3144
3145 /*
3146  * atl1_init_module - Driver Registration Routine
3147  *
3148  * atl1_init_module is the first routine called when the driver is
3149  * loaded. All it does is register with the PCI subsystem.
3150  */
3151 static int __init atl1_init_module(void)
3152 {
3153         return pci_register_driver(&atl1_driver);
3154 }
3155
3156 module_init(atl1_init_module);
3157 module_exit(atl1_exit_module);
3158
3159 struct atl1_stats {
3160         char stat_string[ETH_GSTRING_LEN];
3161         int sizeof_stat;
3162         int stat_offset;
3163 };
3164
3165 #define ATL1_STAT(m) \
3166         sizeof(((struct atl1_adapter *)0)->m), offsetof(struct atl1_adapter, m)
3167
3168 static struct atl1_stats atl1_gstrings_stats[] = {
3169         {"rx_packets", ATL1_STAT(soft_stats.rx_packets)},
3170         {"tx_packets", ATL1_STAT(soft_stats.tx_packets)},
3171         {"rx_bytes", ATL1_STAT(soft_stats.rx_bytes)},
3172         {"tx_bytes", ATL1_STAT(soft_stats.tx_bytes)},
3173         {"rx_errors", ATL1_STAT(soft_stats.rx_errors)},
3174         {"tx_errors", ATL1_STAT(soft_stats.tx_errors)},
3175         {"rx_dropped", ATL1_STAT(net_stats.rx_dropped)},
3176         {"tx_dropped", ATL1_STAT(net_stats.tx_dropped)},
3177         {"multicast", ATL1_STAT(soft_stats.multicast)},
3178         {"collisions", ATL1_STAT(soft_stats.collisions)},
3179         {"rx_length_errors", ATL1_STAT(soft_stats.rx_length_errors)},
3180         {"rx_over_errors", ATL1_STAT(soft_stats.rx_missed_errors)},
3181         {"rx_crc_errors", ATL1_STAT(soft_stats.rx_crc_errors)},
3182         {"rx_frame_errors", ATL1_STAT(soft_stats.rx_frame_errors)},
3183         {"rx_fifo_errors", ATL1_STAT(soft_stats.rx_fifo_errors)},
3184         {"rx_missed_errors", ATL1_STAT(soft_stats.rx_missed_errors)},
3185         {"tx_aborted_errors", ATL1_STAT(soft_stats.tx_aborted_errors)},
3186         {"tx_carrier_errors", ATL1_STAT(soft_stats.tx_carrier_errors)},
3187         {"tx_fifo_errors", ATL1_STAT(soft_stats.tx_fifo_errors)},
3188         {"tx_window_errors", ATL1_STAT(soft_stats.tx_window_errors)},
3189         {"tx_abort_exce_coll", ATL1_STAT(soft_stats.excecol)},
3190         {"tx_abort_late_coll", ATL1_STAT(soft_stats.latecol)},
3191         {"tx_deferred_ok", ATL1_STAT(soft_stats.deffer)},
3192         {"tx_single_coll_ok", ATL1_STAT(soft_stats.scc)},
3193         {"tx_multi_coll_ok", ATL1_STAT(soft_stats.mcc)},
3194         {"tx_underun", ATL1_STAT(soft_stats.tx_underun)},
3195         {"tx_trunc", ATL1_STAT(soft_stats.tx_trunc)},
3196         {"tx_pause", ATL1_STAT(soft_stats.tx_pause)},
3197         {"rx_pause", ATL1_STAT(soft_stats.rx_pause)},
3198         {"rx_rrd_ov", ATL1_STAT(soft_stats.rx_rrd_ov)},
3199         {"rx_trunc", ATL1_STAT(soft_stats.rx_trunc)}
3200 };
3201
3202 static void atl1_get_ethtool_stats(struct net_device *netdev,
3203         struct ethtool_stats *stats, u64 *data)
3204 {
3205         struct atl1_adapter *adapter = netdev_priv(netdev);
3206         int i;
3207         char *p;
3208
3209         for (i = 0; i < ARRAY_SIZE(atl1_gstrings_stats); i++) {
3210                 p = (char *)adapter+atl1_gstrings_stats[i].stat_offset;
3211                 data[i] = (atl1_gstrings_stats[i].sizeof_stat ==
3212                         sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
3213         }
3214
3215 }
3216
3217 static int atl1_get_sset_count(struct net_device *netdev, int sset)
3218 {
3219         switch (sset) {
3220         case ETH_SS_STATS:
3221                 return ARRAY_SIZE(atl1_gstrings_stats);
3222         default:
3223                 return -EOPNOTSUPP;
3224         }
3225 }
3226
3227 static int atl1_get_settings(struct net_device *netdev,
3228         struct ethtool_cmd *ecmd)
3229 {
3230         struct atl1_adapter *adapter = netdev_priv(netdev);
3231         struct atl1_hw *hw = &adapter->hw;
3232
3233         ecmd->supported = (SUPPORTED_10baseT_Half |
3234                            SUPPORTED_10baseT_Full |
3235                            SUPPORTED_100baseT_Half |
3236                            SUPPORTED_100baseT_Full |
3237                            SUPPORTED_1000baseT_Full |
3238                            SUPPORTED_Autoneg | SUPPORTED_TP);
3239         ecmd->advertising = ADVERTISED_TP;
3240         if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3241             hw->media_type == MEDIA_TYPE_1000M_FULL) {
3242                 ecmd->advertising |= ADVERTISED_Autoneg;
3243                 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR) {
3244                         ecmd->advertising |= ADVERTISED_Autoneg;
3245                         ecmd->advertising |=
3246                             (ADVERTISED_10baseT_Half |
3247                              ADVERTISED_10baseT_Full |
3248                              ADVERTISED_100baseT_Half |
3249                              ADVERTISED_100baseT_Full |
3250                              ADVERTISED_1000baseT_Full);
3251                 } else
3252                         ecmd->advertising |= (ADVERTISED_1000baseT_Full);
3253         }
3254         ecmd->port = PORT_TP;
3255         ecmd->phy_address = 0;
3256         ecmd->transceiver = XCVR_INTERNAL;
3257
3258         if (netif_carrier_ok(adapter->netdev)) {
3259                 u16 link_speed, link_duplex;
3260                 atl1_get_speed_and_duplex(hw, &link_speed, &link_duplex);
3261                 ecmd->speed = link_speed;
3262                 if (link_duplex == FULL_DUPLEX)
3263                         ecmd->duplex = DUPLEX_FULL;
3264                 else
3265                         ecmd->duplex = DUPLEX_HALF;
3266         } else {
3267                 ecmd->speed = -1;
3268                 ecmd->duplex = -1;
3269         }
3270         if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3271             hw->media_type == MEDIA_TYPE_1000M_FULL)
3272                 ecmd->autoneg = AUTONEG_ENABLE;
3273         else
3274                 ecmd->autoneg = AUTONEG_DISABLE;
3275
3276         return 0;
3277 }
3278
3279 static int atl1_set_settings(struct net_device *netdev,
3280         struct ethtool_cmd *ecmd)
3281 {
3282         struct atl1_adapter *adapter = netdev_priv(netdev);
3283         struct atl1_hw *hw = &adapter->hw;
3284         u16 phy_data;
3285         int ret_val = 0;
3286         u16 old_media_type = hw->media_type;
3287
3288         if (netif_running(adapter->netdev)) {
3289                 if (netif_msg_link(adapter))
3290                         dev_dbg(&adapter->pdev->dev,
3291                                 "ethtool shutting down adapter\n");
3292                 atl1_down(adapter);
3293         }
3294
3295         if (ecmd->autoneg == AUTONEG_ENABLE)
3296                 hw->media_type = MEDIA_TYPE_AUTO_SENSOR;
3297         else {
3298                 if (ecmd->speed == SPEED_1000) {
3299                         if (ecmd->duplex != DUPLEX_FULL) {
3300                                 if (netif_msg_link(adapter))
3301                                         dev_warn(&adapter->pdev->dev,
3302                                                 "1000M half is invalid\n");
3303                                 ret_val = -EINVAL;
3304                                 goto exit_sset;
3305                         }
3306                         hw->media_type = MEDIA_TYPE_1000M_FULL;
3307                 } else if (ecmd->speed == SPEED_100) {
3308                         if (ecmd->duplex == DUPLEX_FULL)
3309                                 hw->media_type = MEDIA_TYPE_100M_FULL;
3310                         else
3311                                 hw->media_type = MEDIA_TYPE_100M_HALF;
3312                 } else {
3313                         if (ecmd->duplex == DUPLEX_FULL)
3314                                 hw->media_type = MEDIA_TYPE_10M_FULL;
3315                         else
3316                                 hw->media_type = MEDIA_TYPE_10M_HALF;
3317                 }
3318         }
3319         switch (hw->media_type) {
3320         case MEDIA_TYPE_AUTO_SENSOR:
3321                 ecmd->advertising =
3322                     ADVERTISED_10baseT_Half |
3323                     ADVERTISED_10baseT_Full |
3324                     ADVERTISED_100baseT_Half |
3325                     ADVERTISED_100baseT_Full |
3326                     ADVERTISED_1000baseT_Full |
3327                     ADVERTISED_Autoneg | ADVERTISED_TP;
3328                 break;
3329         case MEDIA_TYPE_1000M_FULL:
3330                 ecmd->advertising =
3331                     ADVERTISED_1000baseT_Full |
3332                     ADVERTISED_Autoneg | ADVERTISED_TP;
3333                 break;
3334         default:
3335                 ecmd->advertising = 0;
3336                 break;
3337         }
3338         if (atl1_phy_setup_autoneg_adv(hw)) {
3339                 ret_val = -EINVAL;
3340                 if (netif_msg_link(adapter))
3341                         dev_warn(&adapter->pdev->dev,
3342                                 "invalid ethtool speed/duplex setting\n");
3343                 goto exit_sset;
3344         }
3345         if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3346             hw->media_type == MEDIA_TYPE_1000M_FULL)
3347                 phy_data = MII_CR_RESET | MII_CR_AUTO_NEG_EN;
3348         else {
3349                 switch (hw->media_type) {
3350                 case MEDIA_TYPE_100M_FULL:
3351                         phy_data =
3352                             MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
3353                             MII_CR_RESET;
3354                         break;
3355                 case MEDIA_TYPE_100M_HALF:
3356                         phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
3357                         break;
3358                 case MEDIA_TYPE_10M_FULL:
3359                         phy_data =
3360                             MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
3361                         break;
3362                 default:
3363                         /* MEDIA_TYPE_10M_HALF: */
3364                         phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
3365                         break;
3366                 }
3367         }
3368         atl1_write_phy_reg(hw, MII_BMCR, phy_data);
3369 exit_sset:
3370         if (ret_val)
3371                 hw->media_type = old_media_type;
3372
3373         if (netif_running(adapter->netdev)) {
3374                 if (netif_msg_link(adapter))
3375                         dev_dbg(&adapter->pdev->dev,
3376                                 "ethtool starting adapter\n");
3377                 atl1_up(adapter);
3378         } else if (!ret_val) {
3379                 if (netif_msg_link(adapter))
3380                         dev_dbg(&adapter->pdev->dev,
3381                                 "ethtool resetting adapter\n");
3382                 atl1_reset(adapter);
3383         }
3384         return ret_val;
3385 }
3386
3387 static void atl1_get_drvinfo(struct net_device *netdev,
3388         struct ethtool_drvinfo *drvinfo)
3389 {
3390         struct atl1_adapter *adapter = netdev_priv(netdev);
3391
3392         strncpy(drvinfo->driver, ATLX_DRIVER_NAME, sizeof(drvinfo->driver));
3393         strncpy(drvinfo->version, ATLX_DRIVER_VERSION,
3394                 sizeof(drvinfo->version));
3395         strncpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
3396         strncpy(drvinfo->bus_info, pci_name(adapter->pdev),
3397                 sizeof(drvinfo->bus_info));
3398         drvinfo->eedump_len = ATL1_EEDUMP_LEN;
3399 }
3400
3401 static void atl1_get_wol(struct net_device *netdev,
3402         struct ethtool_wolinfo *wol)
3403 {
3404         struct atl1_adapter *adapter = netdev_priv(netdev);
3405
3406         wol->supported = WAKE_UCAST | WAKE_MCAST | WAKE_BCAST | WAKE_MAGIC;
3407         wol->wolopts = 0;
3408         if (adapter->wol & ATLX_WUFC_EX)
3409                 wol->wolopts |= WAKE_UCAST;
3410         if (adapter->wol & ATLX_WUFC_MC)
3411                 wol->wolopts |= WAKE_MCAST;
3412         if (adapter->wol & ATLX_WUFC_BC)
3413                 wol->wolopts |= WAKE_BCAST;
3414         if (adapter->wol & ATLX_WUFC_MAG)
3415                 wol->wolopts |= WAKE_MAGIC;
3416         return;
3417 }
3418
3419 static int atl1_set_wol(struct net_device *netdev,
3420         struct ethtool_wolinfo *wol)
3421 {
3422         struct atl1_adapter *adapter = netdev_priv(netdev);
3423
3424         if (wol->wolopts & (WAKE_PHY | WAKE_ARP | WAKE_MAGICSECURE))
3425                 return -EOPNOTSUPP;
3426         adapter->wol = 0;
3427         if (wol->wolopts & WAKE_UCAST)
3428                 adapter->wol |= ATLX_WUFC_EX;
3429         if (wol->wolopts & WAKE_MCAST)
3430                 adapter->wol |= ATLX_WUFC_MC;
3431         if (wol->wolopts & WAKE_BCAST)
3432                 adapter->wol |= ATLX_WUFC_BC;
3433         if (wol->wolopts & WAKE_MAGIC)
3434                 adapter->wol |= ATLX_WUFC_MAG;
3435         return 0;
3436 }
3437
3438 static u32 atl1_get_msglevel(struct net_device *netdev)
3439 {
3440         struct atl1_adapter *adapter = netdev_priv(netdev);
3441         return adapter->msg_enable;
3442 }
3443
3444 static void atl1_set_msglevel(struct net_device *netdev, u32 value)
3445 {
3446         struct atl1_adapter *adapter = netdev_priv(netdev);
3447         adapter->msg_enable = value;
3448 }
3449
3450 static int atl1_get_regs_len(struct net_device *netdev)
3451 {
3452         return ATL1_REG_COUNT * sizeof(u32);
3453 }
3454
3455 static void atl1_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
3456         void *p)
3457 {
3458         struct atl1_adapter *adapter = netdev_priv(netdev);
3459         struct atl1_hw *hw = &adapter->hw;
3460         unsigned int i;
3461         u32 *regbuf = p;
3462
3463         for (i = 0; i < ATL1_REG_COUNT; i++) {
3464                 /*
3465                  * This switch statement avoids reserved regions
3466                  * of register space.
3467                  */
3468                 switch (i) {
3469                 case 6 ... 9:
3470                 case 14:
3471                 case 29 ... 31:
3472                 case 34 ... 63:
3473                 case 75 ... 127:
3474                 case 136 ... 1023:
3475                 case 1027 ... 1087:
3476                 case 1091 ... 1151:
3477                 case 1194 ... 1195:
3478                 case 1200 ... 1201:
3479                 case 1206 ... 1213:
3480                 case 1216 ... 1279:
3481                 case 1290 ... 1311:
3482                 case 1323 ... 1343:
3483                 case 1358 ... 1359:
3484                 case 1368 ... 1375:
3485                 case 1378 ... 1383:
3486                 case 1388 ... 1391:
3487                 case 1393 ... 1395:
3488                 case 1402 ... 1403:
3489                 case 1410 ... 1471:
3490                 case 1522 ... 1535:
3491                         /* reserved region; don't read it */
3492                         regbuf[i] = 0;
3493                         break;
3494                 default:
3495                         /* unreserved region */
3496                         regbuf[i] = ioread32(hw->hw_addr + (i * sizeof(u32)));
3497                 }
3498         }
3499 }
3500
3501 static void atl1_get_ringparam(struct net_device *netdev,
3502         struct ethtool_ringparam *ring)
3503 {
3504         struct atl1_adapter *adapter = netdev_priv(netdev);
3505         struct atl1_tpd_ring *txdr = &adapter->tpd_ring;
3506         struct atl1_rfd_ring *rxdr = &adapter->rfd_ring;
3507
3508         ring->rx_max_pending = ATL1_MAX_RFD;
3509         ring->tx_max_pending = ATL1_MAX_TPD;
3510         ring->rx_mini_max_pending = 0;
3511         ring->rx_jumbo_max_pending = 0;
3512         ring->rx_pending = rxdr->count;
3513         ring->tx_pending = txdr->count;
3514         ring->rx_mini_pending = 0;
3515         ring->rx_jumbo_pending = 0;
3516 }
3517
3518 static int atl1_set_ringparam(struct net_device *netdev,
3519         struct ethtool_ringparam *ring)
3520 {
3521         struct atl1_adapter *adapter = netdev_priv(netdev);
3522         struct atl1_tpd_ring *tpdr = &adapter->tpd_ring;
3523         struct atl1_rrd_ring *rrdr = &adapter->rrd_ring;
3524         struct atl1_rfd_ring *rfdr = &adapter->rfd_ring;
3525
3526         struct atl1_tpd_ring tpd_old, tpd_new;
3527         struct atl1_rfd_ring rfd_old, rfd_new;
3528         struct atl1_rrd_ring rrd_old, rrd_new;
3529         struct atl1_ring_header rhdr_old, rhdr_new;
3530         int err;
3531
3532         tpd_old = adapter->tpd_ring;
3533         rfd_old = adapter->rfd_ring;
3534         rrd_old = adapter->rrd_ring;
3535         rhdr_old = adapter->ring_header;
3536
3537         if (netif_running(adapter->netdev))
3538                 atl1_down(adapter);
3539
3540         rfdr->count = (u16) max(ring->rx_pending, (u32) ATL1_MIN_RFD);
3541         rfdr->count = rfdr->count > ATL1_MAX_RFD ? ATL1_MAX_RFD :
3542                         rfdr->count;
3543         rfdr->count = (rfdr->count + 3) & ~3;
3544         rrdr->count = rfdr->count;
3545
3546         tpdr->count = (u16) max(ring->tx_pending, (u32) ATL1_MIN_TPD);
3547         tpdr->count = tpdr->count > ATL1_MAX_TPD ? ATL1_MAX_TPD :
3548                         tpdr->count;
3549         tpdr->count = (tpdr->count + 3) & ~3;
3550
3551         if (netif_running(adapter->netdev)) {
3552                 /* try to get new resources before deleting old */
3553                 err = atl1_setup_ring_resources(adapter);
3554                 if (err)
3555                         goto err_setup_ring;
3556
3557                 /*
3558                  * save the new, restore the old in order to free it,
3559                  * then restore the new back again
3560                  */
3561
3562                 rfd_new = adapter->rfd_ring;
3563                 rrd_new = adapter->rrd_ring;
3564                 tpd_new = adapter->tpd_ring;
3565                 rhdr_new = adapter->ring_header;
3566                 adapter->rfd_ring = rfd_old;
3567                 adapter->rrd_ring = rrd_old;
3568                 adapter->tpd_ring = tpd_old;
3569                 adapter->ring_header = rhdr_old;
3570                 atl1_free_ring_resources(adapter);
3571                 adapter->rfd_ring = rfd_new;
3572                 adapter->rrd_ring = rrd_new;
3573                 adapter->tpd_ring = tpd_new;
3574                 adapter->ring_header = rhdr_new;
3575
3576                 err = atl1_up(adapter);
3577                 if (err)
3578                         return err;
3579         }
3580         return 0;
3581
3582 err_setup_ring:
3583         adapter->rfd_ring = rfd_old;
3584         adapter->rrd_ring = rrd_old;
3585         adapter->tpd_ring = tpd_old;
3586         adapter->ring_header = rhdr_old;
3587         atl1_up(adapter);
3588         return err;
3589 }
3590
3591 static void atl1_get_pauseparam(struct net_device *netdev,
3592         struct ethtool_pauseparam *epause)
3593 {
3594         struct atl1_adapter *adapter = netdev_priv(netdev);
3595         struct atl1_hw *hw = &adapter->hw;
3596
3597         if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3598             hw->media_type == MEDIA_TYPE_1000M_FULL) {
3599                 epause->autoneg = AUTONEG_ENABLE;
3600         } else {
3601                 epause->autoneg = AUTONEG_DISABLE;
3602         }
3603         epause->rx_pause = 1;
3604         epause->tx_pause = 1;
3605 }
3606
3607 static int atl1_set_pauseparam(struct net_device *netdev,
3608         struct ethtool_pauseparam *epause)
3609 {
3610         struct atl1_adapter *adapter = netdev_priv(netdev);
3611         struct atl1_hw *hw = &adapter->hw;
3612
3613         if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3614             hw->media_type == MEDIA_TYPE_1000M_FULL) {
3615                 epause->autoneg = AUTONEG_ENABLE;
3616         } else {
3617                 epause->autoneg = AUTONEG_DISABLE;
3618         }
3619
3620         epause->rx_pause = 1;
3621         epause->tx_pause = 1;
3622
3623         return 0;
3624 }
3625
3626 /* FIXME: is this right? -- CHS */
3627 static u32 atl1_get_rx_csum(struct net_device *netdev)
3628 {
3629         return 1;
3630 }
3631
3632 static void atl1_get_strings(struct net_device *netdev, u32 stringset,
3633         u8 *data)
3634 {
3635         u8 *p = data;
3636         int i;
3637
3638         switch (stringset) {
3639         case ETH_SS_STATS:
3640                 for (i = 0; i < ARRAY_SIZE(atl1_gstrings_stats); i++) {
3641                         memcpy(p, atl1_gstrings_stats[i].stat_string,
3642                                 ETH_GSTRING_LEN);
3643                         p += ETH_GSTRING_LEN;
3644                 }
3645                 break;
3646         }
3647 }
3648
3649 static int atl1_nway_reset(struct net_device *netdev)
3650 {
3651         struct atl1_adapter *adapter = netdev_priv(netdev);
3652         struct atl1_hw *hw = &adapter->hw;
3653
3654         if (netif_running(netdev)) {
3655                 u16 phy_data;
3656                 atl1_down(adapter);
3657
3658                 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3659                         hw->media_type == MEDIA_TYPE_1000M_FULL) {
3660                         phy_data = MII_CR_RESET | MII_CR_AUTO_NEG_EN;
3661                 } else {
3662                         switch (hw->media_type) {
3663                         case MEDIA_TYPE_100M_FULL:
3664                                 phy_data = MII_CR_FULL_DUPLEX |
3665                                         MII_CR_SPEED_100 | MII_CR_RESET;
3666                                 break;
3667                         case MEDIA_TYPE_100M_HALF:
3668                                 phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
3669                                 break;
3670                         case MEDIA_TYPE_10M_FULL:
3671                                 phy_data = MII_CR_FULL_DUPLEX |
3672                                         MII_CR_SPEED_10 | MII_CR_RESET;
3673                                 break;
3674                         default:
3675                                 /* MEDIA_TYPE_10M_HALF */
3676                                 phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
3677                         }
3678                 }
3679                 atl1_write_phy_reg(hw, MII_BMCR, phy_data);
3680                 atl1_up(adapter);
3681         }
3682         return 0;
3683 }
3684
3685 const struct ethtool_ops atl1_ethtool_ops = {
3686         .get_settings           = atl1_get_settings,
3687         .set_settings           = atl1_set_settings,
3688         .get_drvinfo            = atl1_get_drvinfo,
3689         .get_wol                = atl1_get_wol,
3690         .set_wol                = atl1_set_wol,
3691         .get_msglevel           = atl1_get_msglevel,
3692         .set_msglevel           = atl1_set_msglevel,
3693         .get_regs_len           = atl1_get_regs_len,
3694         .get_regs               = atl1_get_regs,
3695         .get_ringparam          = atl1_get_ringparam,
3696         .set_ringparam          = atl1_set_ringparam,
3697         .get_pauseparam         = atl1_get_pauseparam,
3698         .set_pauseparam         = atl1_set_pauseparam,
3699         .get_rx_csum            = atl1_get_rx_csum,
3700         .set_tx_csum            = ethtool_op_set_tx_hw_csum,
3701         .get_link               = ethtool_op_get_link,
3702         .set_sg                 = ethtool_op_set_sg,
3703         .get_strings            = atl1_get_strings,
3704         .nway_reset             = atl1_nway_reset,
3705         .get_ethtool_stats      = atl1_get_ethtool_stats,
3706         .get_sset_count         = atl1_get_sset_count,
3707         .set_tso                = ethtool_op_set_tso,
3708 };