553230eb365c547fce10d845051d5dbed8558753
[linux-2.6.git] / drivers / net / atl1c / atl1c_main.c
1 /*
2  * Copyright(c) 2008 - 2009 Atheros Corporation. All rights reserved.
3  *
4  * Derived from Intel e1000 driver
5  * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc., 59
19  * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  */
21
22 #include "atl1c.h"
23
24 #define ATL1C_DRV_VERSION "1.0.1.0-NAPI"
25 char atl1c_driver_name[] = "atl1c";
26 char atl1c_driver_version[] = ATL1C_DRV_VERSION;
27 #define PCI_DEVICE_ID_ATTANSIC_L2C      0x1062
28 #define PCI_DEVICE_ID_ATTANSIC_L1C      0x1063
29 #define PCI_DEVICE_ID_ATHEROS_L2C_B     0x2060 /* AR8152 v1.1 Fast 10/100 */
30 #define PCI_DEVICE_ID_ATHEROS_L2C_B2    0x2062 /* AR8152 v2.0 Fast 10/100 */
31 #define PCI_DEVICE_ID_ATHEROS_L1D       0x1073 /* AR8151 v1.0 Gigabit 1000 */
32 #define PCI_DEVICE_ID_ATHEROS_L1D_2_0   0x1083 /* AR8151 v2.0 Gigabit 1000 */
33 #define L2CB_V10                        0xc0
34 #define L2CB_V11                        0xc1
35
36 /*
37  * atl1c_pci_tbl - PCI Device ID Table
38  *
39  * Wildcard entries (PCI_ANY_ID) should come last
40  * Last entry must be all 0s
41  *
42  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
43  *   Class, Class Mask, private data (not used) }
44  */
45 static DEFINE_PCI_DEVICE_TABLE(atl1c_pci_tbl) = {
46         {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L1C)},
47         {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L2C)},
48         {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L2C_B)},
49         {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L2C_B2)},
50         {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L1D)},
51         /* required last entry */
52         { 0 }
53 };
54 MODULE_DEVICE_TABLE(pci, atl1c_pci_tbl);
55
56 MODULE_AUTHOR("Jie Yang <jie.yang@atheros.com>");
57 MODULE_DESCRIPTION("Atheros 1000M Ethernet Network Driver");
58 MODULE_LICENSE("GPL");
59 MODULE_VERSION(ATL1C_DRV_VERSION);
60
61 static int atl1c_stop_mac(struct atl1c_hw *hw);
62 static void atl1c_enable_rx_ctrl(struct atl1c_hw *hw);
63 static void atl1c_enable_tx_ctrl(struct atl1c_hw *hw);
64 static void atl1c_disable_l0s_l1(struct atl1c_hw *hw);
65 static void atl1c_set_aspm(struct atl1c_hw *hw, bool linkup);
66 static void atl1c_setup_mac_ctrl(struct atl1c_adapter *adapter);
67 static void atl1c_clean_rx_irq(struct atl1c_adapter *adapter, u8 que,
68                    int *work_done, int work_to_do);
69
70 static const u16 atl1c_pay_load_size[] = {
71         128, 256, 512, 1024, 2048, 4096,
72 };
73
74 static const u16 atl1c_rfd_prod_idx_regs[AT_MAX_RECEIVE_QUEUE] =
75 {
76         REG_MB_RFD0_PROD_IDX,
77         REG_MB_RFD1_PROD_IDX,
78         REG_MB_RFD2_PROD_IDX,
79         REG_MB_RFD3_PROD_IDX
80 };
81
82 static const u16 atl1c_rfd_addr_lo_regs[AT_MAX_RECEIVE_QUEUE] =
83 {
84         REG_RFD0_HEAD_ADDR_LO,
85         REG_RFD1_HEAD_ADDR_LO,
86         REG_RFD2_HEAD_ADDR_LO,
87         REG_RFD3_HEAD_ADDR_LO
88 };
89
90 static const u16 atl1c_rrd_addr_lo_regs[AT_MAX_RECEIVE_QUEUE] =
91 {
92         REG_RRD0_HEAD_ADDR_LO,
93         REG_RRD1_HEAD_ADDR_LO,
94         REG_RRD2_HEAD_ADDR_LO,
95         REG_RRD3_HEAD_ADDR_LO
96 };
97
98 static const u32 atl1c_default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
99         NETIF_MSG_LINK | NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP;
100 static void atl1c_pcie_patch(struct atl1c_hw *hw)
101 {
102         u32 data;
103
104         AT_READ_REG(hw, REG_PCIE_PHYMISC, &data);
105         data |= PCIE_PHYMISC_FORCE_RCV_DET;
106         AT_WRITE_REG(hw, REG_PCIE_PHYMISC, data);
107
108         if (hw->nic_type == athr_l2c_b && hw->revision_id == L2CB_V10) {
109                 AT_READ_REG(hw, REG_PCIE_PHYMISC2, &data);
110
111                 data &= ~(PCIE_PHYMISC2_SERDES_CDR_MASK <<
112                         PCIE_PHYMISC2_SERDES_CDR_SHIFT);
113                 data |= 3 << PCIE_PHYMISC2_SERDES_CDR_SHIFT;
114                 data &= ~(PCIE_PHYMISC2_SERDES_TH_MASK <<
115                         PCIE_PHYMISC2_SERDES_TH_SHIFT);
116                 data |= 3 << PCIE_PHYMISC2_SERDES_TH_SHIFT;
117                 AT_WRITE_REG(hw, REG_PCIE_PHYMISC2, data);
118         }
119 }
120
121 /* FIXME: no need any more ? */
122 /*
123  * atl1c_init_pcie - init PCIE module
124  */
125 static void atl1c_reset_pcie(struct atl1c_hw *hw, u32 flag)
126 {
127         u32 data;
128         u32 pci_cmd;
129         struct pci_dev *pdev = hw->adapter->pdev;
130
131         AT_READ_REG(hw, PCI_COMMAND, &pci_cmd);
132         pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
133         pci_cmd |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
134                 PCI_COMMAND_IO);
135         AT_WRITE_REG(hw, PCI_COMMAND, pci_cmd);
136
137         /*
138          * Clear any PowerSaveing Settings
139          */
140         pci_enable_wake(pdev, PCI_D3hot, 0);
141         pci_enable_wake(pdev, PCI_D3cold, 0);
142
143         /*
144          * Mask some pcie error bits
145          */
146         AT_READ_REG(hw, REG_PCIE_UC_SEVERITY, &data);
147         data &= ~PCIE_UC_SERVRITY_DLP;
148         data &= ~PCIE_UC_SERVRITY_FCP;
149         AT_WRITE_REG(hw, REG_PCIE_UC_SEVERITY, data);
150
151         AT_READ_REG(hw, REG_LTSSM_ID_CTRL, &data);
152         data &= ~LTSSM_ID_EN_WRO;
153         AT_WRITE_REG(hw, REG_LTSSM_ID_CTRL, data);
154
155         atl1c_pcie_patch(hw);
156         if (flag & ATL1C_PCIE_L0S_L1_DISABLE)
157                 atl1c_disable_l0s_l1(hw);
158         if (flag & ATL1C_PCIE_PHY_RESET)
159                 AT_WRITE_REG(hw, REG_GPHY_CTRL, GPHY_CTRL_DEFAULT);
160         else
161                 AT_WRITE_REG(hw, REG_GPHY_CTRL,
162                         GPHY_CTRL_DEFAULT | GPHY_CTRL_EXT_RESET);
163
164         msleep(5);
165 }
166
167 /*
168  * atl1c_irq_enable - Enable default interrupt generation settings
169  * @adapter: board private structure
170  */
171 static inline void atl1c_irq_enable(struct atl1c_adapter *adapter)
172 {
173         if (likely(atomic_dec_and_test(&adapter->irq_sem))) {
174                 AT_WRITE_REG(&adapter->hw, REG_ISR, 0x7FFFFFFF);
175                 AT_WRITE_REG(&adapter->hw, REG_IMR, adapter->hw.intr_mask);
176                 AT_WRITE_FLUSH(&adapter->hw);
177         }
178 }
179
180 /*
181  * atl1c_irq_disable - Mask off interrupt generation on the NIC
182  * @adapter: board private structure
183  */
184 static inline void atl1c_irq_disable(struct atl1c_adapter *adapter)
185 {
186         atomic_inc(&adapter->irq_sem);
187         AT_WRITE_REG(&adapter->hw, REG_IMR, 0);
188         AT_WRITE_REG(&adapter->hw, REG_ISR, ISR_DIS_INT);
189         AT_WRITE_FLUSH(&adapter->hw);
190         synchronize_irq(adapter->pdev->irq);
191 }
192
193 /*
194  * atl1c_irq_reset - reset interrupt confiure on the NIC
195  * @adapter: board private structure
196  */
197 static inline void atl1c_irq_reset(struct atl1c_adapter *adapter)
198 {
199         atomic_set(&adapter->irq_sem, 1);
200         atl1c_irq_enable(adapter);
201 }
202
203 /*
204  * atl1c_wait_until_idle - wait up to AT_HW_MAX_IDLE_DELAY reads
205  * of the idle status register until the device is actually idle
206  */
207 static u32 atl1c_wait_until_idle(struct atl1c_hw *hw)
208 {
209         int timeout;
210         u32 data;
211
212         for (timeout = 0; timeout < AT_HW_MAX_IDLE_DELAY; timeout++) {
213                 AT_READ_REG(hw, REG_IDLE_STATUS, &data);
214                 if ((data & IDLE_STATUS_MASK) == 0)
215                         return 0;
216                 msleep(1);
217         }
218         return data;
219 }
220
221 /*
222  * atl1c_phy_config - Timer Call-back
223  * @data: pointer to netdev cast into an unsigned long
224  */
225 static void atl1c_phy_config(unsigned long data)
226 {
227         struct atl1c_adapter *adapter = (struct atl1c_adapter *) data;
228         struct atl1c_hw *hw = &adapter->hw;
229         unsigned long flags;
230
231         spin_lock_irqsave(&adapter->mdio_lock, flags);
232         atl1c_restart_autoneg(hw);
233         spin_unlock_irqrestore(&adapter->mdio_lock, flags);
234 }
235
236 void atl1c_reinit_locked(struct atl1c_adapter *adapter)
237 {
238         WARN_ON(in_interrupt());
239         atl1c_down(adapter);
240         atl1c_up(adapter);
241         clear_bit(__AT_RESETTING, &adapter->flags);
242 }
243
244 static void atl1c_check_link_status(struct atl1c_adapter *adapter)
245 {
246         struct atl1c_hw *hw = &adapter->hw;
247         struct net_device *netdev = adapter->netdev;
248         struct pci_dev    *pdev   = adapter->pdev;
249         int err;
250         unsigned long flags;
251         u16 speed, duplex, phy_data;
252
253         spin_lock_irqsave(&adapter->mdio_lock, flags);
254         /* MII_BMSR must read twise */
255         atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
256         atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
257         spin_unlock_irqrestore(&adapter->mdio_lock, flags);
258
259         if ((phy_data & BMSR_LSTATUS) == 0) {
260                 /* link down */
261                 hw->hibernate = true;
262                 if (atl1c_stop_mac(hw) != 0)
263                         if (netif_msg_hw(adapter))
264                                 dev_warn(&pdev->dev, "stop mac failed\n");
265                 atl1c_set_aspm(hw, false);
266                 netif_carrier_off(netdev);
267                 netif_stop_queue(netdev);
268                 atl1c_phy_reset(hw);
269                 atl1c_phy_init(&adapter->hw);
270         } else {
271                 /* Link Up */
272                 hw->hibernate = false;
273                 spin_lock_irqsave(&adapter->mdio_lock, flags);
274                 err = atl1c_get_speed_and_duplex(hw, &speed, &duplex);
275                 spin_unlock_irqrestore(&adapter->mdio_lock, flags);
276                 if (unlikely(err))
277                         return;
278                 /* link result is our setting */
279                 if (adapter->link_speed != speed ||
280                     adapter->link_duplex != duplex) {
281                         adapter->link_speed  = speed;
282                         adapter->link_duplex = duplex;
283                         atl1c_set_aspm(hw, true);
284                         atl1c_enable_tx_ctrl(hw);
285                         atl1c_enable_rx_ctrl(hw);
286                         atl1c_setup_mac_ctrl(adapter);
287                         if (netif_msg_link(adapter))
288                                 dev_info(&pdev->dev,
289                                         "%s: %s NIC Link is Up<%d Mbps %s>\n",
290                                         atl1c_driver_name, netdev->name,
291                                         adapter->link_speed,
292                                         adapter->link_duplex == FULL_DUPLEX ?
293                                         "Full Duplex" : "Half Duplex");
294                 }
295                 if (!netif_carrier_ok(netdev))
296                         netif_carrier_on(netdev);
297         }
298 }
299
300 static void atl1c_link_chg_event(struct atl1c_adapter *adapter)
301 {
302         struct net_device *netdev = adapter->netdev;
303         struct pci_dev    *pdev   = adapter->pdev;
304         u16 phy_data;
305         u16 link_up;
306
307         spin_lock(&adapter->mdio_lock);
308         atl1c_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
309         atl1c_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
310         spin_unlock(&adapter->mdio_lock);
311         link_up = phy_data & BMSR_LSTATUS;
312         /* notify upper layer link down ASAP */
313         if (!link_up) {
314                 if (netif_carrier_ok(netdev)) {
315                         /* old link state: Up */
316                         netif_carrier_off(netdev);
317                         if (netif_msg_link(adapter))
318                                 dev_info(&pdev->dev,
319                                         "%s: %s NIC Link is Down\n",
320                                         atl1c_driver_name, netdev->name);
321                         adapter->link_speed = SPEED_0;
322                 }
323         }
324
325         adapter->work_event |= ATL1C_WORK_EVENT_LINK_CHANGE;
326         schedule_work(&adapter->common_task);
327 }
328
329 static void atl1c_common_task(struct work_struct *work)
330 {
331         struct atl1c_adapter *adapter;
332         struct net_device *netdev;
333
334         adapter = container_of(work, struct atl1c_adapter, common_task);
335         netdev = adapter->netdev;
336
337         if (adapter->work_event & ATL1C_WORK_EVENT_RESET) {
338                 adapter->work_event &= ~ATL1C_WORK_EVENT_RESET;
339                 netif_device_detach(netdev);
340                 atl1c_down(adapter);
341                 atl1c_up(adapter);
342                 netif_device_attach(netdev);
343                 return;
344         }
345
346         if (adapter->work_event & ATL1C_WORK_EVENT_LINK_CHANGE) {
347                 adapter->work_event &= ~ATL1C_WORK_EVENT_LINK_CHANGE;
348                 atl1c_check_link_status(adapter);
349         }
350         return;
351 }
352
353
354 static void atl1c_del_timer(struct atl1c_adapter *adapter)
355 {
356         del_timer_sync(&adapter->phy_config_timer);
357 }
358
359
360 /*
361  * atl1c_tx_timeout - Respond to a Tx Hang
362  * @netdev: network interface device structure
363  */
364 static void atl1c_tx_timeout(struct net_device *netdev)
365 {
366         struct atl1c_adapter *adapter = netdev_priv(netdev);
367
368         /* Do the reset outside of interrupt context */
369         adapter->work_event |= ATL1C_WORK_EVENT_RESET;
370         schedule_work(&adapter->common_task);
371 }
372
373 /*
374  * atl1c_set_multi - Multicast and Promiscuous mode set
375  * @netdev: network interface device structure
376  *
377  * The set_multi entry point is called whenever the multicast address
378  * list or the network interface flags are updated.  This routine is
379  * responsible for configuring the hardware for proper multicast,
380  * promiscuous mode, and all-multi behavior.
381  */
382 static void atl1c_set_multi(struct net_device *netdev)
383 {
384         struct atl1c_adapter *adapter = netdev_priv(netdev);
385         struct atl1c_hw *hw = &adapter->hw;
386         struct netdev_hw_addr *ha;
387         u32 mac_ctrl_data;
388         u32 hash_value;
389
390         /* Check for Promiscuous and All Multicast modes */
391         AT_READ_REG(hw, REG_MAC_CTRL, &mac_ctrl_data);
392
393         if (netdev->flags & IFF_PROMISC) {
394                 mac_ctrl_data |= MAC_CTRL_PROMIS_EN;
395         } else if (netdev->flags & IFF_ALLMULTI) {
396                 mac_ctrl_data |= MAC_CTRL_MC_ALL_EN;
397                 mac_ctrl_data &= ~MAC_CTRL_PROMIS_EN;
398         } else {
399                 mac_ctrl_data &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
400         }
401
402         AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
403
404         /* clear the old settings from the multicast hash table */
405         AT_WRITE_REG(hw, REG_RX_HASH_TABLE, 0);
406         AT_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, 1, 0);
407
408         /* comoute mc addresses' hash value ,and put it into hash table */
409         netdev_for_each_mc_addr(ha, netdev) {
410                 hash_value = atl1c_hash_mc_addr(hw, ha->addr);
411                 atl1c_hash_set(hw, hash_value);
412         }
413 }
414
415 static void atl1c_vlan_rx_register(struct net_device *netdev,
416                                    struct vlan_group *grp)
417 {
418         struct atl1c_adapter *adapter = netdev_priv(netdev);
419         struct pci_dev *pdev = adapter->pdev;
420         u32 mac_ctrl_data = 0;
421
422         if (netif_msg_pktdata(adapter))
423                 dev_dbg(&pdev->dev, "atl1c_vlan_rx_register\n");
424
425         atl1c_irq_disable(adapter);
426
427         adapter->vlgrp = grp;
428         AT_READ_REG(&adapter->hw, REG_MAC_CTRL, &mac_ctrl_data);
429
430         if (grp) {
431                 /* enable VLAN tag insert/strip */
432                 mac_ctrl_data |= MAC_CTRL_RMV_VLAN;
433         } else {
434                 /* disable VLAN tag insert/strip */
435                 mac_ctrl_data &= ~MAC_CTRL_RMV_VLAN;
436         }
437
438         AT_WRITE_REG(&adapter->hw, REG_MAC_CTRL, mac_ctrl_data);
439         atl1c_irq_enable(adapter);
440 }
441
442 static void atl1c_restore_vlan(struct atl1c_adapter *adapter)
443 {
444         struct pci_dev *pdev = adapter->pdev;
445
446         if (netif_msg_pktdata(adapter))
447                 dev_dbg(&pdev->dev, "atl1c_restore_vlan !");
448         atl1c_vlan_rx_register(adapter->netdev, adapter->vlgrp);
449 }
450 /*
451  * atl1c_set_mac - Change the Ethernet Address of the NIC
452  * @netdev: network interface device structure
453  * @p: pointer to an address structure
454  *
455  * Returns 0 on success, negative on failure
456  */
457 static int atl1c_set_mac_addr(struct net_device *netdev, void *p)
458 {
459         struct atl1c_adapter *adapter = netdev_priv(netdev);
460         struct sockaddr *addr = p;
461
462         if (!is_valid_ether_addr(addr->sa_data))
463                 return -EADDRNOTAVAIL;
464
465         if (netif_running(netdev))
466                 return -EBUSY;
467
468         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
469         memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
470
471         atl1c_hw_set_mac_addr(&adapter->hw);
472
473         return 0;
474 }
475
476 static void atl1c_set_rxbufsize(struct atl1c_adapter *adapter,
477                                 struct net_device *dev)
478 {
479         int mtu = dev->mtu;
480
481         adapter->rx_buffer_len = mtu > AT_RX_BUF_SIZE ?
482                 roundup(mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN, 8) : AT_RX_BUF_SIZE;
483 }
484 /*
485  * atl1c_change_mtu - Change the Maximum Transfer Unit
486  * @netdev: network interface device structure
487  * @new_mtu: new value for maximum frame size
488  *
489  * Returns 0 on success, negative on failure
490  */
491 static int atl1c_change_mtu(struct net_device *netdev, int new_mtu)
492 {
493         struct atl1c_adapter *adapter = netdev_priv(netdev);
494         int old_mtu   = netdev->mtu;
495         int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
496
497         if ((max_frame < ETH_ZLEN + ETH_FCS_LEN) ||
498                         (max_frame > MAX_JUMBO_FRAME_SIZE)) {
499                 if (netif_msg_link(adapter))
500                         dev_warn(&adapter->pdev->dev, "invalid MTU setting\n");
501                 return -EINVAL;
502         }
503         /* set MTU */
504         if (old_mtu != new_mtu && netif_running(netdev)) {
505                 while (test_and_set_bit(__AT_RESETTING, &adapter->flags))
506                         msleep(1);
507                 netdev->mtu = new_mtu;
508                 adapter->hw.max_frame_size = new_mtu;
509                 atl1c_set_rxbufsize(adapter, netdev);
510                 if (new_mtu > MAX_TSO_FRAME_SIZE) {
511                         adapter->netdev->features &= ~NETIF_F_TSO;
512                         adapter->netdev->features &= ~NETIF_F_TSO6;
513                 } else {
514                         adapter->netdev->features |= NETIF_F_TSO;
515                         adapter->netdev->features |= NETIF_F_TSO6;
516                 }
517                 atl1c_down(adapter);
518                 atl1c_up(adapter);
519                 clear_bit(__AT_RESETTING, &adapter->flags);
520                 if (adapter->hw.ctrl_flags & ATL1C_FPGA_VERSION) {
521                         u32 phy_data;
522
523                         AT_READ_REG(&adapter->hw, 0x1414, &phy_data);
524                         phy_data |= 0x10000000;
525                         AT_WRITE_REG(&adapter->hw, 0x1414, phy_data);
526                 }
527
528         }
529         return 0;
530 }
531
532 /*
533  *  caller should hold mdio_lock
534  */
535 static int atl1c_mdio_read(struct net_device *netdev, int phy_id, int reg_num)
536 {
537         struct atl1c_adapter *adapter = netdev_priv(netdev);
538         u16 result;
539
540         atl1c_read_phy_reg(&adapter->hw, reg_num & MDIO_REG_ADDR_MASK, &result);
541         return result;
542 }
543
544 static void atl1c_mdio_write(struct net_device *netdev, int phy_id,
545                              int reg_num, int val)
546 {
547         struct atl1c_adapter *adapter = netdev_priv(netdev);
548
549         atl1c_write_phy_reg(&adapter->hw, reg_num & MDIO_REG_ADDR_MASK, val);
550 }
551
552 /*
553  * atl1c_mii_ioctl -
554  * @netdev:
555  * @ifreq:
556  * @cmd:
557  */
558 static int atl1c_mii_ioctl(struct net_device *netdev,
559                            struct ifreq *ifr, int cmd)
560 {
561         struct atl1c_adapter *adapter = netdev_priv(netdev);
562         struct pci_dev *pdev = adapter->pdev;
563         struct mii_ioctl_data *data = if_mii(ifr);
564         unsigned long flags;
565         int retval = 0;
566
567         if (!netif_running(netdev))
568                 return -EINVAL;
569
570         spin_lock_irqsave(&adapter->mdio_lock, flags);
571         switch (cmd) {
572         case SIOCGMIIPHY:
573                 data->phy_id = 0;
574                 break;
575
576         case SIOCGMIIREG:
577                 if (atl1c_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
578                                     &data->val_out)) {
579                         retval = -EIO;
580                         goto out;
581                 }
582                 break;
583
584         case SIOCSMIIREG:
585                 if (data->reg_num & ~(0x1F)) {
586                         retval = -EFAULT;
587                         goto out;
588                 }
589
590                 dev_dbg(&pdev->dev, "<atl1c_mii_ioctl> write %x %x",
591                                 data->reg_num, data->val_in);
592                 if (atl1c_write_phy_reg(&adapter->hw,
593                                      data->reg_num, data->val_in)) {
594                         retval = -EIO;
595                         goto out;
596                 }
597                 break;
598
599         default:
600                 retval = -EOPNOTSUPP;
601                 break;
602         }
603 out:
604         spin_unlock_irqrestore(&adapter->mdio_lock, flags);
605         return retval;
606 }
607
608 /*
609  * atl1c_ioctl -
610  * @netdev:
611  * @ifreq:
612  * @cmd:
613  */
614 static int atl1c_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
615 {
616         switch (cmd) {
617         case SIOCGMIIPHY:
618         case SIOCGMIIREG:
619         case SIOCSMIIREG:
620                 return atl1c_mii_ioctl(netdev, ifr, cmd);
621         default:
622                 return -EOPNOTSUPP;
623         }
624 }
625
626 /*
627  * atl1c_alloc_queues - Allocate memory for all rings
628  * @adapter: board private structure to initialize
629  *
630  */
631 static int __devinit atl1c_alloc_queues(struct atl1c_adapter *adapter)
632 {
633         return 0;
634 }
635
636 static void atl1c_set_mac_type(struct atl1c_hw *hw)
637 {
638         switch (hw->device_id) {
639         case PCI_DEVICE_ID_ATTANSIC_L2C:
640                 hw->nic_type = athr_l2c;
641                 break;
642         case PCI_DEVICE_ID_ATTANSIC_L1C:
643                 hw->nic_type = athr_l1c;
644                 break;
645         case PCI_DEVICE_ID_ATHEROS_L2C_B:
646                 hw->nic_type = athr_l2c_b;
647                 break;
648         case PCI_DEVICE_ID_ATHEROS_L2C_B2:
649                 hw->nic_type = athr_l2c_b2;
650                 break;
651         case PCI_DEVICE_ID_ATHEROS_L1D:
652                 hw->nic_type = athr_l1d;
653                 break;
654         case PCI_DEVICE_ID_ATHEROS_L1D_2_0:
655                 hw->nic_type = athr_l1d_2;
656                 break;
657         default:
658                 break;
659         }
660 }
661
662 static int atl1c_setup_mac_funcs(struct atl1c_hw *hw)
663 {
664         u32 phy_status_data;
665         u32 link_ctrl_data;
666
667         atl1c_set_mac_type(hw);
668         AT_READ_REG(hw, REG_PHY_STATUS, &phy_status_data);
669         AT_READ_REG(hw, REG_LINK_CTRL, &link_ctrl_data);
670
671         hw->ctrl_flags = ATL1C_INTR_MODRT_ENABLE  |
672                          ATL1C_TXQ_MODE_ENHANCE;
673         if (link_ctrl_data & LINK_CTRL_L0S_EN)
674                 hw->ctrl_flags |= ATL1C_ASPM_L0S_SUPPORT;
675         if (link_ctrl_data & LINK_CTRL_L1_EN)
676                 hw->ctrl_flags |= ATL1C_ASPM_L1_SUPPORT;
677         if (link_ctrl_data & LINK_CTRL_EXT_SYNC)
678                 hw->ctrl_flags |= ATL1C_LINK_EXT_SYNC;
679         hw->ctrl_flags |= ATL1C_ASPM_CTRL_MON;
680
681         if (hw->nic_type == athr_l1c ||
682             hw->nic_type == athr_l1d ||
683             hw->nic_type == athr_l1d_2)
684                 hw->link_cap_flags |= ATL1C_LINK_CAP_1000M;
685         return 0;
686 }
687 /*
688  * atl1c_sw_init - Initialize general software structures (struct atl1c_adapter)
689  * @adapter: board private structure to initialize
690  *
691  * atl1c_sw_init initializes the Adapter private data structure.
692  * Fields are initialized based on PCI device information and
693  * OS network device settings (MTU size).
694  */
695 static int __devinit atl1c_sw_init(struct atl1c_adapter *adapter)
696 {
697         struct atl1c_hw *hw   = &adapter->hw;
698         struct pci_dev  *pdev = adapter->pdev;
699         u32 revision;
700
701
702         adapter->wol = 0;
703         adapter->link_speed = SPEED_0;
704         adapter->link_duplex = FULL_DUPLEX;
705         adapter->num_rx_queues = AT_DEF_RECEIVE_QUEUE;
706         adapter->tpd_ring[0].count = 1024;
707         adapter->rfd_ring[0].count = 512;
708
709         hw->vendor_id = pdev->vendor;
710         hw->device_id = pdev->device;
711         hw->subsystem_vendor_id = pdev->subsystem_vendor;
712         hw->subsystem_id = pdev->subsystem_device;
713         AT_READ_REG(hw, PCI_CLASS_REVISION, &revision);
714         hw->revision_id = revision & 0xFF;
715         /* before link up, we assume hibernate is true */
716         hw->hibernate = true;
717         hw->media_type = MEDIA_TYPE_AUTO_SENSOR;
718         if (atl1c_setup_mac_funcs(hw) != 0) {
719                 dev_err(&pdev->dev, "set mac function pointers failed\n");
720                 return -1;
721         }
722         hw->intr_mask = IMR_NORMAL_MASK;
723         hw->phy_configured = false;
724         hw->preamble_len = 7;
725         hw->max_frame_size = adapter->netdev->mtu;
726         if (adapter->num_rx_queues < 2) {
727                 hw->rss_type = atl1c_rss_disable;
728                 hw->rss_mode = atl1c_rss_mode_disable;
729         } else {
730                 hw->rss_type = atl1c_rss_ipv4;
731                 hw->rss_mode = atl1c_rss_mul_que_mul_int;
732                 hw->rss_hash_bits = 16;
733         }
734         hw->autoneg_advertised = ADVERTISED_Autoneg;
735         hw->indirect_tab = 0xE4E4E4E4;
736         hw->base_cpu = 0;
737
738         hw->ict = 50000;                /* 100ms */
739         hw->smb_timer = 200000;         /* 400ms */
740         hw->cmb_tpd = 4;
741         hw->cmb_tx_timer = 1;           /* 2 us  */
742         hw->rx_imt = 200;
743         hw->tx_imt = 1000;
744
745         hw->tpd_burst = 5;
746         hw->rfd_burst = 8;
747         hw->dma_order = atl1c_dma_ord_out;
748         hw->dmar_block = atl1c_dma_req_1024;
749         hw->dmaw_block = atl1c_dma_req_1024;
750         hw->dmar_dly_cnt = 15;
751         hw->dmaw_dly_cnt = 4;
752
753         if (atl1c_alloc_queues(adapter)) {
754                 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
755                 return -ENOMEM;
756         }
757         /* TODO */
758         atl1c_set_rxbufsize(adapter, adapter->netdev);
759         atomic_set(&adapter->irq_sem, 1);
760         spin_lock_init(&adapter->mdio_lock);
761         spin_lock_init(&adapter->tx_lock);
762         set_bit(__AT_DOWN, &adapter->flags);
763
764         return 0;
765 }
766
767 static inline void atl1c_clean_buffer(struct pci_dev *pdev,
768                                 struct atl1c_buffer *buffer_info, int in_irq)
769 {
770         u16 pci_driection;
771         if (buffer_info->flags & ATL1C_BUFFER_FREE)
772                 return;
773         if (buffer_info->dma) {
774                 if (buffer_info->flags & ATL1C_PCIMAP_FROMDEVICE)
775                         pci_driection = PCI_DMA_FROMDEVICE;
776                 else
777                         pci_driection = PCI_DMA_TODEVICE;
778
779                 if (buffer_info->flags & ATL1C_PCIMAP_SINGLE)
780                         pci_unmap_single(pdev, buffer_info->dma,
781                                         buffer_info->length, pci_driection);
782                 else if (buffer_info->flags & ATL1C_PCIMAP_PAGE)
783                         pci_unmap_page(pdev, buffer_info->dma,
784                                         buffer_info->length, pci_driection);
785         }
786         if (buffer_info->skb) {
787                 if (in_irq)
788                         dev_kfree_skb_irq(buffer_info->skb);
789                 else
790                         dev_kfree_skb(buffer_info->skb);
791         }
792         buffer_info->dma = 0;
793         buffer_info->skb = NULL;
794         ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_FREE);
795 }
796 /*
797  * atl1c_clean_tx_ring - Free Tx-skb
798  * @adapter: board private structure
799  */
800 static void atl1c_clean_tx_ring(struct atl1c_adapter *adapter,
801                                 enum atl1c_trans_queue type)
802 {
803         struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
804         struct atl1c_buffer *buffer_info;
805         struct pci_dev *pdev = adapter->pdev;
806         u16 index, ring_count;
807
808         ring_count = tpd_ring->count;
809         for (index = 0; index < ring_count; index++) {
810                 buffer_info = &tpd_ring->buffer_info[index];
811                 atl1c_clean_buffer(pdev, buffer_info, 0);
812         }
813
814         /* Zero out Tx-buffers */
815         memset(tpd_ring->desc, 0, sizeof(struct atl1c_tpd_desc) *
816                 ring_count);
817         atomic_set(&tpd_ring->next_to_clean, 0);
818         tpd_ring->next_to_use = 0;
819 }
820
821 /*
822  * atl1c_clean_rx_ring - Free rx-reservation skbs
823  * @adapter: board private structure
824  */
825 static void atl1c_clean_rx_ring(struct atl1c_adapter *adapter)
826 {
827         struct atl1c_rfd_ring *rfd_ring = adapter->rfd_ring;
828         struct atl1c_rrd_ring *rrd_ring = adapter->rrd_ring;
829         struct atl1c_buffer *buffer_info;
830         struct pci_dev *pdev = adapter->pdev;
831         int i, j;
832
833         for (i = 0; i < adapter->num_rx_queues; i++) {
834                 for (j = 0; j < rfd_ring[i].count; j++) {
835                         buffer_info = &rfd_ring[i].buffer_info[j];
836                         atl1c_clean_buffer(pdev, buffer_info, 0);
837                 }
838                 /* zero out the descriptor ring */
839                 memset(rfd_ring[i].desc, 0, rfd_ring[i].size);
840                 rfd_ring[i].next_to_clean = 0;
841                 rfd_ring[i].next_to_use = 0;
842                 rrd_ring[i].next_to_use = 0;
843                 rrd_ring[i].next_to_clean = 0;
844         }
845 }
846
847 /*
848  * Read / Write Ptr Initialize:
849  */
850 static void atl1c_init_ring_ptrs(struct atl1c_adapter *adapter)
851 {
852         struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
853         struct atl1c_rfd_ring *rfd_ring = adapter->rfd_ring;
854         struct atl1c_rrd_ring *rrd_ring = adapter->rrd_ring;
855         struct atl1c_buffer *buffer_info;
856         int i, j;
857
858         for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
859                 tpd_ring[i].next_to_use = 0;
860                 atomic_set(&tpd_ring[i].next_to_clean, 0);
861                 buffer_info = tpd_ring[i].buffer_info;
862                 for (j = 0; j < tpd_ring->count; j++)
863                         ATL1C_SET_BUFFER_STATE(&buffer_info[i],
864                                         ATL1C_BUFFER_FREE);
865         }
866         for (i = 0; i < adapter->num_rx_queues; i++) {
867                 rfd_ring[i].next_to_use = 0;
868                 rfd_ring[i].next_to_clean = 0;
869                 rrd_ring[i].next_to_use = 0;
870                 rrd_ring[i].next_to_clean = 0;
871                 for (j = 0; j < rfd_ring[i].count; j++) {
872                         buffer_info = &rfd_ring[i].buffer_info[j];
873                         ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_FREE);
874                 }
875         }
876 }
877
878 /*
879  * atl1c_free_ring_resources - Free Tx / RX descriptor Resources
880  * @adapter: board private structure
881  *
882  * Free all transmit software resources
883  */
884 static void atl1c_free_ring_resources(struct atl1c_adapter *adapter)
885 {
886         struct pci_dev *pdev = adapter->pdev;
887
888         pci_free_consistent(pdev, adapter->ring_header.size,
889                                         adapter->ring_header.desc,
890                                         adapter->ring_header.dma);
891         adapter->ring_header.desc = NULL;
892
893         /* Note: just free tdp_ring.buffer_info,
894         *  it contain rfd_ring.buffer_info, do not double free */
895         if (adapter->tpd_ring[0].buffer_info) {
896                 kfree(adapter->tpd_ring[0].buffer_info);
897                 adapter->tpd_ring[0].buffer_info = NULL;
898         }
899 }
900
901 /*
902  * atl1c_setup_mem_resources - allocate Tx / RX descriptor resources
903  * @adapter: board private structure
904  *
905  * Return 0 on success, negative on failure
906  */
907 static int atl1c_setup_ring_resources(struct atl1c_adapter *adapter)
908 {
909         struct pci_dev *pdev = adapter->pdev;
910         struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
911         struct atl1c_rfd_ring *rfd_ring = adapter->rfd_ring;
912         struct atl1c_rrd_ring *rrd_ring = adapter->rrd_ring;
913         struct atl1c_ring_header *ring_header = &adapter->ring_header;
914         int num_rx_queues = adapter->num_rx_queues;
915         int size;
916         int i;
917         int count = 0;
918         int rx_desc_count = 0;
919         u32 offset = 0;
920
921         rrd_ring[0].count = rfd_ring[0].count;
922         for (i = 1; i < AT_MAX_TRANSMIT_QUEUE; i++)
923                 tpd_ring[i].count = tpd_ring[0].count;
924
925         for (i = 1; i < adapter->num_rx_queues; i++)
926                 rfd_ring[i].count = rrd_ring[i].count = rfd_ring[0].count;
927
928         /* 2 tpd queue, one high priority queue,
929          * another normal priority queue */
930         size = sizeof(struct atl1c_buffer) * (tpd_ring->count * 2 +
931                 rfd_ring->count * num_rx_queues);
932         tpd_ring->buffer_info = kzalloc(size, GFP_KERNEL);
933         if (unlikely(!tpd_ring->buffer_info)) {
934                 dev_err(&pdev->dev, "kzalloc failed, size = %d\n",
935                         size);
936                 goto err_nomem;
937         }
938         for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
939                 tpd_ring[i].buffer_info =
940                         (struct atl1c_buffer *) (tpd_ring->buffer_info + count);
941                 count += tpd_ring[i].count;
942         }
943
944         for (i = 0; i < num_rx_queues; i++) {
945                 rfd_ring[i].buffer_info =
946                         (struct atl1c_buffer *) (tpd_ring->buffer_info + count);
947                 count += rfd_ring[i].count;
948                 rx_desc_count += rfd_ring[i].count;
949         }
950         /*
951          * real ring DMA buffer
952          * each ring/block may need up to 8 bytes for alignment, hence the
953          * additional bytes tacked onto the end.
954          */
955         ring_header->size = size =
956                 sizeof(struct atl1c_tpd_desc) * tpd_ring->count * 2 +
957                 sizeof(struct atl1c_rx_free_desc) * rx_desc_count +
958                 sizeof(struct atl1c_recv_ret_status) * rx_desc_count +
959                 sizeof(struct atl1c_hw_stats) +
960                 8 * 4 + 8 * 2 * num_rx_queues;
961
962         ring_header->desc = pci_alloc_consistent(pdev, ring_header->size,
963                                 &ring_header->dma);
964         if (unlikely(!ring_header->desc)) {
965                 dev_err(&pdev->dev, "pci_alloc_consistend failed\n");
966                 goto err_nomem;
967         }
968         memset(ring_header->desc, 0, ring_header->size);
969         /* init TPD ring */
970
971         tpd_ring[0].dma = roundup(ring_header->dma, 8);
972         offset = tpd_ring[0].dma - ring_header->dma;
973         for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
974                 tpd_ring[i].dma = ring_header->dma + offset;
975                 tpd_ring[i].desc = (u8 *) ring_header->desc + offset;
976                 tpd_ring[i].size =
977                         sizeof(struct atl1c_tpd_desc) * tpd_ring[i].count;
978                 offset += roundup(tpd_ring[i].size, 8);
979         }
980         /* init RFD ring */
981         for (i = 0; i < num_rx_queues; i++) {
982                 rfd_ring[i].dma = ring_header->dma + offset;
983                 rfd_ring[i].desc = (u8 *) ring_header->desc + offset;
984                 rfd_ring[i].size = sizeof(struct atl1c_rx_free_desc) *
985                                 rfd_ring[i].count;
986                 offset += roundup(rfd_ring[i].size, 8);
987         }
988
989         /* init RRD ring */
990         for (i = 0; i < num_rx_queues; i++) {
991                 rrd_ring[i].dma = ring_header->dma + offset;
992                 rrd_ring[i].desc = (u8 *) ring_header->desc + offset;
993                 rrd_ring[i].size = sizeof(struct atl1c_recv_ret_status) *
994                                 rrd_ring[i].count;
995                 offset += roundup(rrd_ring[i].size, 8);
996         }
997
998         adapter->smb.dma = ring_header->dma + offset;
999         adapter->smb.smb = (u8 *)ring_header->desc + offset;
1000         return 0;
1001
1002 err_nomem:
1003         kfree(tpd_ring->buffer_info);
1004         return -ENOMEM;
1005 }
1006
1007 static void atl1c_configure_des_ring(struct atl1c_adapter *adapter)
1008 {
1009         struct atl1c_hw *hw = &adapter->hw;
1010         struct atl1c_rfd_ring *rfd_ring = (struct atl1c_rfd_ring *)
1011                                 adapter->rfd_ring;
1012         struct atl1c_rrd_ring *rrd_ring = (struct atl1c_rrd_ring *)
1013                                 adapter->rrd_ring;
1014         struct atl1c_tpd_ring *tpd_ring = (struct atl1c_tpd_ring *)
1015                                 adapter->tpd_ring;
1016         struct atl1c_cmb *cmb = (struct atl1c_cmb *) &adapter->cmb;
1017         struct atl1c_smb *smb = (struct atl1c_smb *) &adapter->smb;
1018         int i;
1019         u32 data;
1020
1021         /* TPD */
1022         AT_WRITE_REG(hw, REG_TX_BASE_ADDR_HI,
1023                         (u32)((tpd_ring[atl1c_trans_normal].dma &
1024                                 AT_DMA_HI_ADDR_MASK) >> 32));
1025         /* just enable normal priority TX queue */
1026         AT_WRITE_REG(hw, REG_NTPD_HEAD_ADDR_LO,
1027                         (u32)(tpd_ring[atl1c_trans_normal].dma &
1028                                 AT_DMA_LO_ADDR_MASK));
1029         AT_WRITE_REG(hw, REG_HTPD_HEAD_ADDR_LO,
1030                         (u32)(tpd_ring[atl1c_trans_high].dma &
1031                                 AT_DMA_LO_ADDR_MASK));
1032         AT_WRITE_REG(hw, REG_TPD_RING_SIZE,
1033                         (u32)(tpd_ring[0].count & TPD_RING_SIZE_MASK));
1034
1035
1036         /* RFD */
1037         AT_WRITE_REG(hw, REG_RX_BASE_ADDR_HI,
1038                         (u32)((rfd_ring[0].dma & AT_DMA_HI_ADDR_MASK) >> 32));
1039         for (i = 0; i < adapter->num_rx_queues; i++)
1040                 AT_WRITE_REG(hw, atl1c_rfd_addr_lo_regs[i],
1041                         (u32)(rfd_ring[i].dma & AT_DMA_LO_ADDR_MASK));
1042
1043         AT_WRITE_REG(hw, REG_RFD_RING_SIZE,
1044                         rfd_ring[0].count & RFD_RING_SIZE_MASK);
1045         AT_WRITE_REG(hw, REG_RX_BUF_SIZE,
1046                         adapter->rx_buffer_len & RX_BUF_SIZE_MASK);
1047
1048         /* RRD */
1049         for (i = 0; i < adapter->num_rx_queues; i++)
1050                 AT_WRITE_REG(hw, atl1c_rrd_addr_lo_regs[i],
1051                         (u32)(rrd_ring[i].dma & AT_DMA_LO_ADDR_MASK));
1052         AT_WRITE_REG(hw, REG_RRD_RING_SIZE,
1053                         (rrd_ring[0].count & RRD_RING_SIZE_MASK));
1054
1055         /* CMB */
1056         AT_WRITE_REG(hw, REG_CMB_BASE_ADDR_LO, cmb->dma & AT_DMA_LO_ADDR_MASK);
1057
1058         /* SMB */
1059         AT_WRITE_REG(hw, REG_SMB_BASE_ADDR_HI,
1060                         (u32)((smb->dma & AT_DMA_HI_ADDR_MASK) >> 32));
1061         AT_WRITE_REG(hw, REG_SMB_BASE_ADDR_LO,
1062                         (u32)(smb->dma & AT_DMA_LO_ADDR_MASK));
1063         if (hw->nic_type == athr_l2c_b) {
1064                 AT_WRITE_REG(hw, REG_SRAM_RXF_LEN, 0x02a0L);
1065                 AT_WRITE_REG(hw, REG_SRAM_TXF_LEN, 0x0100L);
1066                 AT_WRITE_REG(hw, REG_SRAM_RXF_ADDR, 0x029f0000L);
1067                 AT_WRITE_REG(hw, REG_SRAM_RFD0_INFO, 0x02bf02a0L);
1068                 AT_WRITE_REG(hw, REG_SRAM_TXF_ADDR, 0x03bf02c0L);
1069                 AT_WRITE_REG(hw, REG_SRAM_TRD_ADDR, 0x03df03c0L);
1070                 AT_WRITE_REG(hw, REG_TXF_WATER_MARK, 0);        /* TX watermark, to enter l1 state.*/
1071                 AT_WRITE_REG(hw, REG_RXD_DMA_CTRL, 0);          /* RXD threshold.*/
1072         }
1073         if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l1d_2) {
1074                         /* Power Saving for L2c_B */
1075                 AT_READ_REG(hw, REG_SERDES_LOCK, &data);
1076                 data |= SERDES_MAC_CLK_SLOWDOWN;
1077                 data |= SERDES_PYH_CLK_SLOWDOWN;
1078                 AT_WRITE_REG(hw, REG_SERDES_LOCK, data);
1079         }
1080         /* Load all of base address above */
1081         AT_WRITE_REG(hw, REG_LOAD_PTR, 1);
1082 }
1083
1084 static void atl1c_configure_tx(struct atl1c_adapter *adapter)
1085 {
1086         struct atl1c_hw *hw = &adapter->hw;
1087         u32 dev_ctrl_data;
1088         u32 max_pay_load;
1089         u16 tx_offload_thresh;
1090         u32 txq_ctrl_data;
1091         u32 extra_size = 0;     /* Jumbo frame threshold in QWORD unit */
1092         u32 max_pay_load_data;
1093
1094         extra_size = ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN;
1095         tx_offload_thresh = MAX_TX_OFFLOAD_THRESH;
1096         AT_WRITE_REG(hw, REG_TX_TSO_OFFLOAD_THRESH,
1097                 (tx_offload_thresh >> 3) & TX_TSO_OFFLOAD_THRESH_MASK);
1098         AT_READ_REG(hw, REG_DEVICE_CTRL, &dev_ctrl_data);
1099         max_pay_load  = (dev_ctrl_data >> DEVICE_CTRL_MAX_PAYLOAD_SHIFT) &
1100                         DEVICE_CTRL_MAX_PAYLOAD_MASK;
1101         hw->dmaw_block = min(max_pay_load, hw->dmaw_block);
1102         max_pay_load  = (dev_ctrl_data >> DEVICE_CTRL_MAX_RREQ_SZ_SHIFT) &
1103                         DEVICE_CTRL_MAX_RREQ_SZ_MASK;
1104         hw->dmar_block = min(max_pay_load, hw->dmar_block);
1105
1106         txq_ctrl_data = (hw->tpd_burst & TXQ_NUM_TPD_BURST_MASK) <<
1107                         TXQ_NUM_TPD_BURST_SHIFT;
1108         if (hw->ctrl_flags & ATL1C_TXQ_MODE_ENHANCE)
1109                 txq_ctrl_data |= TXQ_CTRL_ENH_MODE;
1110         max_pay_load_data = (atl1c_pay_load_size[hw->dmar_block] &
1111                         TXQ_TXF_BURST_NUM_MASK) << TXQ_TXF_BURST_NUM_SHIFT;
1112         if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l2c_b2)
1113                 max_pay_load_data >>= 1;
1114         txq_ctrl_data |= max_pay_load_data;
1115
1116         AT_WRITE_REG(hw, REG_TXQ_CTRL, txq_ctrl_data);
1117 }
1118
1119 static void atl1c_configure_rx(struct atl1c_adapter *adapter)
1120 {
1121         struct atl1c_hw *hw = &adapter->hw;
1122         u32 rxq_ctrl_data;
1123
1124         rxq_ctrl_data = (hw->rfd_burst & RXQ_RFD_BURST_NUM_MASK) <<
1125                         RXQ_RFD_BURST_NUM_SHIFT;
1126
1127         if (hw->ctrl_flags & ATL1C_RX_IPV6_CHKSUM)
1128                 rxq_ctrl_data |= IPV6_CHKSUM_CTRL_EN;
1129         if (hw->rss_type == atl1c_rss_ipv4)
1130                 rxq_ctrl_data |= RSS_HASH_IPV4;
1131         if (hw->rss_type == atl1c_rss_ipv4_tcp)
1132                 rxq_ctrl_data |= RSS_HASH_IPV4_TCP;
1133         if (hw->rss_type == atl1c_rss_ipv6)
1134                 rxq_ctrl_data |= RSS_HASH_IPV6;
1135         if (hw->rss_type == atl1c_rss_ipv6_tcp)
1136                 rxq_ctrl_data |= RSS_HASH_IPV6_TCP;
1137         if (hw->rss_type != atl1c_rss_disable)
1138                 rxq_ctrl_data |= RRS_HASH_CTRL_EN;
1139
1140         rxq_ctrl_data |= (hw->rss_mode & RSS_MODE_MASK) <<
1141                         RSS_MODE_SHIFT;
1142         rxq_ctrl_data |= (hw->rss_hash_bits & RSS_HASH_BITS_MASK) <<
1143                         RSS_HASH_BITS_SHIFT;
1144         if (hw->ctrl_flags & ATL1C_ASPM_CTRL_MON)
1145                 rxq_ctrl_data |= (ASPM_THRUPUT_LIMIT_1M &
1146                         ASPM_THRUPUT_LIMIT_MASK) << ASPM_THRUPUT_LIMIT_SHIFT;
1147
1148         AT_WRITE_REG(hw, REG_RXQ_CTRL, rxq_ctrl_data);
1149 }
1150
1151 static void atl1c_configure_rss(struct atl1c_adapter *adapter)
1152 {
1153         struct atl1c_hw *hw = &adapter->hw;
1154
1155         AT_WRITE_REG(hw, REG_IDT_TABLE, hw->indirect_tab);
1156         AT_WRITE_REG(hw, REG_BASE_CPU_NUMBER, hw->base_cpu);
1157 }
1158
1159 static void atl1c_configure_dma(struct atl1c_adapter *adapter)
1160 {
1161         struct atl1c_hw *hw = &adapter->hw;
1162         u32 dma_ctrl_data;
1163
1164         dma_ctrl_data = DMA_CTRL_DMAR_REQ_PRI;
1165         if (hw->ctrl_flags & ATL1C_CMB_ENABLE)
1166                 dma_ctrl_data |= DMA_CTRL_CMB_EN;
1167         if (hw->ctrl_flags & ATL1C_SMB_ENABLE)
1168                 dma_ctrl_data |= DMA_CTRL_SMB_EN;
1169         else
1170                 dma_ctrl_data |= MAC_CTRL_SMB_DIS;
1171
1172         switch (hw->dma_order) {
1173         case atl1c_dma_ord_in:
1174                 dma_ctrl_data |= DMA_CTRL_DMAR_IN_ORDER;
1175                 break;
1176         case atl1c_dma_ord_enh:
1177                 dma_ctrl_data |= DMA_CTRL_DMAR_ENH_ORDER;
1178                 break;
1179         case atl1c_dma_ord_out:
1180                 dma_ctrl_data |= DMA_CTRL_DMAR_OUT_ORDER;
1181                 break;
1182         default:
1183                 break;
1184         }
1185
1186         dma_ctrl_data |= (((u32)hw->dmar_block) & DMA_CTRL_DMAR_BURST_LEN_MASK)
1187                 << DMA_CTRL_DMAR_BURST_LEN_SHIFT;
1188         dma_ctrl_data |= (((u32)hw->dmaw_block) & DMA_CTRL_DMAW_BURST_LEN_MASK)
1189                 << DMA_CTRL_DMAW_BURST_LEN_SHIFT;
1190         dma_ctrl_data |= (((u32)hw->dmar_dly_cnt) & DMA_CTRL_DMAR_DLY_CNT_MASK)
1191                 << DMA_CTRL_DMAR_DLY_CNT_SHIFT;
1192         dma_ctrl_data |= (((u32)hw->dmaw_dly_cnt) & DMA_CTRL_DMAW_DLY_CNT_MASK)
1193                 << DMA_CTRL_DMAW_DLY_CNT_SHIFT;
1194
1195         AT_WRITE_REG(hw, REG_DMA_CTRL, dma_ctrl_data);
1196 }
1197
1198 /*
1199  * Stop the mac, transmit and receive units
1200  * hw - Struct containing variables accessed by shared code
1201  * return : 0  or  idle status (if error)
1202  */
1203 static int atl1c_stop_mac(struct atl1c_hw *hw)
1204 {
1205         u32 data;
1206
1207         AT_READ_REG(hw, REG_RXQ_CTRL, &data);
1208         data &= ~(RXQ1_CTRL_EN | RXQ2_CTRL_EN |
1209                   RXQ3_CTRL_EN | RXQ_CTRL_EN);
1210         AT_WRITE_REG(hw, REG_RXQ_CTRL, data);
1211
1212         AT_READ_REG(hw, REG_TXQ_CTRL, &data);
1213         data &= ~TXQ_CTRL_EN;
1214         AT_WRITE_REG(hw, REG_TWSI_CTRL, data);
1215
1216         atl1c_wait_until_idle(hw);
1217
1218         AT_READ_REG(hw, REG_MAC_CTRL, &data);
1219         data &= ~(MAC_CTRL_TX_EN | MAC_CTRL_RX_EN);
1220         AT_WRITE_REG(hw, REG_MAC_CTRL, data);
1221
1222         return (int)atl1c_wait_until_idle(hw);
1223 }
1224
1225 static void atl1c_enable_rx_ctrl(struct atl1c_hw *hw)
1226 {
1227         u32 data;
1228
1229         AT_READ_REG(hw, REG_RXQ_CTRL, &data);
1230         switch (hw->adapter->num_rx_queues) {
1231         case 4:
1232                 data |= (RXQ3_CTRL_EN | RXQ2_CTRL_EN | RXQ1_CTRL_EN);
1233                 break;
1234         case 3:
1235                 data |= (RXQ2_CTRL_EN | RXQ1_CTRL_EN);
1236                 break;
1237         case 2:
1238                 data |= RXQ1_CTRL_EN;
1239                 break;
1240         default:
1241                 break;
1242         }
1243         data |= RXQ_CTRL_EN;
1244         AT_WRITE_REG(hw, REG_RXQ_CTRL, data);
1245 }
1246
1247 static void atl1c_enable_tx_ctrl(struct atl1c_hw *hw)
1248 {
1249         u32 data;
1250
1251         AT_READ_REG(hw, REG_TXQ_CTRL, &data);
1252         data |= TXQ_CTRL_EN;
1253         AT_WRITE_REG(hw, REG_TXQ_CTRL, data);
1254 }
1255
1256 /*
1257  * Reset the transmit and receive units; mask and clear all interrupts.
1258  * hw - Struct containing variables accessed by shared code
1259  * return : 0  or  idle status (if error)
1260  */
1261 static int atl1c_reset_mac(struct atl1c_hw *hw)
1262 {
1263         struct atl1c_adapter *adapter = (struct atl1c_adapter *)hw->adapter;
1264         struct pci_dev *pdev = adapter->pdev;
1265         u32 master_ctrl_data = 0;
1266
1267         AT_WRITE_REG(hw, REG_IMR, 0);
1268         AT_WRITE_REG(hw, REG_ISR, ISR_DIS_INT);
1269
1270         atl1c_stop_mac(hw);
1271         /*
1272          * Issue Soft Reset to the MAC.  This will reset the chip's
1273          * transmit, receive, DMA.  It will not effect
1274          * the current PCI configuration.  The global reset bit is self-
1275          * clearing, and should clear within a microsecond.
1276          */
1277         AT_READ_REG(hw, REG_MASTER_CTRL, &master_ctrl_data);
1278         master_ctrl_data |= MASTER_CTRL_OOB_DIS_OFF;
1279         AT_WRITE_REGW(hw, REG_MASTER_CTRL, ((master_ctrl_data | MASTER_CTRL_SOFT_RST)
1280                         & 0xFFFF));
1281
1282         AT_WRITE_FLUSH(hw);
1283         msleep(10);
1284         /* Wait at least 10ms for All module to be Idle */
1285
1286         if (atl1c_wait_until_idle(hw)) {
1287                 dev_err(&pdev->dev,
1288                         "MAC state machine can't be idle since"
1289                         " disabled for 10ms second\n");
1290                 return -1;
1291         }
1292         return 0;
1293 }
1294
1295 static void atl1c_disable_l0s_l1(struct atl1c_hw *hw)
1296 {
1297         u32 pm_ctrl_data;
1298
1299         AT_READ_REG(hw, REG_PM_CTRL, &pm_ctrl_data);
1300         pm_ctrl_data &= ~(PM_CTRL_L1_ENTRY_TIMER_MASK <<
1301                         PM_CTRL_L1_ENTRY_TIMER_SHIFT);
1302         pm_ctrl_data &= ~PM_CTRL_CLK_SWH_L1;
1303         pm_ctrl_data &= ~PM_CTRL_ASPM_L0S_EN;
1304         pm_ctrl_data &= ~PM_CTRL_ASPM_L1_EN;
1305         pm_ctrl_data &= ~PM_CTRL_MAC_ASPM_CHK;
1306         pm_ctrl_data &= ~PM_CTRL_SERDES_PD_EX_L1;
1307
1308         pm_ctrl_data |= PM_CTRL_SERDES_BUDS_RX_L1_EN;
1309         pm_ctrl_data |= PM_CTRL_SERDES_PLL_L1_EN;
1310         pm_ctrl_data |= PM_CTRL_SERDES_L1_EN;
1311         AT_WRITE_REG(hw, REG_PM_CTRL, pm_ctrl_data);
1312 }
1313
1314 /*
1315  * Set ASPM state.
1316  * Enable/disable L0s/L1 depend on link state.
1317  */
1318 static void atl1c_set_aspm(struct atl1c_hw *hw, bool linkup)
1319 {
1320         u32 pm_ctrl_data;
1321         u32 link_ctrl_data;
1322         u32 link_l1_timer = 0xF;
1323
1324         AT_READ_REG(hw, REG_PM_CTRL, &pm_ctrl_data);
1325         AT_READ_REG(hw, REG_LINK_CTRL, &link_ctrl_data);
1326
1327         pm_ctrl_data &= ~PM_CTRL_SERDES_PD_EX_L1;
1328         pm_ctrl_data &=  ~(PM_CTRL_L1_ENTRY_TIMER_MASK <<
1329                         PM_CTRL_L1_ENTRY_TIMER_SHIFT);
1330         pm_ctrl_data &= ~(PM_CTRL_LCKDET_TIMER_MASK <<
1331                         PM_CTRL_LCKDET_TIMER_SHIFT);
1332         pm_ctrl_data |= AT_LCKDET_TIMER << PM_CTRL_LCKDET_TIMER_SHIFT;
1333
1334         if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l1d ||
1335                 hw->nic_type == athr_l2c_b2 || hw->nic_type == athr_l1d_2) {
1336                 link_ctrl_data &= ~LINK_CTRL_EXT_SYNC;
1337                 if (!(hw->ctrl_flags & ATL1C_APS_MODE_ENABLE)) {
1338                         if (hw->nic_type == athr_l2c_b && hw->revision_id == L2CB_V10)
1339                                 link_ctrl_data |= LINK_CTRL_EXT_SYNC;
1340                 }
1341
1342                 AT_WRITE_REG(hw, REG_LINK_CTRL, link_ctrl_data);
1343
1344                 pm_ctrl_data |= PM_CTRL_RCVR_WT_TIMER;
1345                 pm_ctrl_data &= ~(PM_CTRL_PM_REQ_TIMER_MASK <<
1346                         PM_CTRL_PM_REQ_TIMER_SHIFT);
1347                 pm_ctrl_data |= AT_ASPM_L1_TIMER <<
1348                         PM_CTRL_PM_REQ_TIMER_SHIFT;
1349                 pm_ctrl_data &= ~PM_CTRL_SA_DLY_EN;
1350                 pm_ctrl_data &= ~PM_CTRL_HOTRST;
1351                 pm_ctrl_data |= 1 << PM_CTRL_L1_ENTRY_TIMER_SHIFT;
1352                 pm_ctrl_data |= PM_CTRL_SERDES_PD_EX_L1;
1353         }
1354         pm_ctrl_data |= PM_CTRL_MAC_ASPM_CHK;
1355         if (linkup) {
1356                 pm_ctrl_data &= ~PM_CTRL_ASPM_L1_EN;
1357                 pm_ctrl_data &= ~PM_CTRL_ASPM_L0S_EN;
1358                 if (hw->ctrl_flags & ATL1C_ASPM_L1_SUPPORT)
1359                         pm_ctrl_data |= PM_CTRL_ASPM_L1_EN;
1360                 if (hw->ctrl_flags & ATL1C_ASPM_L0S_SUPPORT)
1361                         pm_ctrl_data |= PM_CTRL_ASPM_L0S_EN;
1362
1363                 if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l1d ||
1364                         hw->nic_type == athr_l2c_b2 || hw->nic_type == athr_l1d_2) {
1365                         if (hw->nic_type == athr_l2c_b)
1366                                 if (!(hw->ctrl_flags & ATL1C_APS_MODE_ENABLE))
1367                                         pm_ctrl_data &= ~PM_CTRL_ASPM_L0S_EN;
1368                         pm_ctrl_data &= ~PM_CTRL_SERDES_L1_EN;
1369                         pm_ctrl_data &= ~PM_CTRL_SERDES_PLL_L1_EN;
1370                         pm_ctrl_data &= ~PM_CTRL_SERDES_BUDS_RX_L1_EN;
1371                         pm_ctrl_data |= PM_CTRL_CLK_SWH_L1;
1372                 if (hw->adapter->link_speed == SPEED_100 ||
1373                                 hw->adapter->link_speed == SPEED_1000) {
1374                                 pm_ctrl_data &=  ~(PM_CTRL_L1_ENTRY_TIMER_MASK <<
1375                                         PM_CTRL_L1_ENTRY_TIMER_SHIFT);
1376                                 if (hw->nic_type == athr_l2c_b)
1377                                         link_l1_timer = 7;
1378                                 else if (hw->nic_type == athr_l2c_b2 ||
1379                                         hw->nic_type == athr_l1d_2)
1380                                         link_l1_timer = 4;
1381                                 pm_ctrl_data |= link_l1_timer <<
1382                                         PM_CTRL_L1_ENTRY_TIMER_SHIFT;
1383                         }
1384                 } else {
1385                         pm_ctrl_data |= PM_CTRL_SERDES_L1_EN;
1386                         pm_ctrl_data |= PM_CTRL_SERDES_PLL_L1_EN;
1387                         pm_ctrl_data |= PM_CTRL_SERDES_BUDS_RX_L1_EN;
1388                         pm_ctrl_data &= ~PM_CTRL_CLK_SWH_L1;
1389                         pm_ctrl_data &= ~PM_CTRL_ASPM_L0S_EN;
1390                         pm_ctrl_data &= ~PM_CTRL_ASPM_L1_EN;
1391
1392                 }
1393         } else {
1394                 pm_ctrl_data &= ~PM_CTRL_SERDES_L1_EN;
1395                 pm_ctrl_data &= ~PM_CTRL_ASPM_L0S_EN;
1396                 pm_ctrl_data &= ~PM_CTRL_SERDES_PLL_L1_EN;
1397                 pm_ctrl_data |= PM_CTRL_CLK_SWH_L1;
1398
1399                 if (hw->ctrl_flags & ATL1C_ASPM_L1_SUPPORT)
1400                         pm_ctrl_data |= PM_CTRL_ASPM_L1_EN;
1401                 else
1402                         pm_ctrl_data &= ~PM_CTRL_ASPM_L1_EN;
1403         }
1404         AT_WRITE_REG(hw, REG_PM_CTRL, pm_ctrl_data);
1405
1406         return;
1407 }
1408
1409 static void atl1c_setup_mac_ctrl(struct atl1c_adapter *adapter)
1410 {
1411         struct atl1c_hw *hw = &adapter->hw;
1412         struct net_device *netdev = adapter->netdev;
1413         u32 mac_ctrl_data;
1414
1415         mac_ctrl_data = MAC_CTRL_TX_EN | MAC_CTRL_RX_EN;
1416         mac_ctrl_data |= (MAC_CTRL_TX_FLOW | MAC_CTRL_RX_FLOW);
1417
1418         if (adapter->link_duplex == FULL_DUPLEX) {
1419                 hw->mac_duplex = true;
1420                 mac_ctrl_data |= MAC_CTRL_DUPLX;
1421         }
1422
1423         if (adapter->link_speed == SPEED_1000)
1424                 hw->mac_speed = atl1c_mac_speed_1000;
1425         else
1426                 hw->mac_speed = atl1c_mac_speed_10_100;
1427
1428         mac_ctrl_data |= (hw->mac_speed & MAC_CTRL_SPEED_MASK) <<
1429                         MAC_CTRL_SPEED_SHIFT;
1430
1431         mac_ctrl_data |= (MAC_CTRL_ADD_CRC | MAC_CTRL_PAD);
1432         mac_ctrl_data |= ((hw->preamble_len & MAC_CTRL_PRMLEN_MASK) <<
1433                         MAC_CTRL_PRMLEN_SHIFT);
1434
1435         if (adapter->vlgrp)
1436                 mac_ctrl_data |= MAC_CTRL_RMV_VLAN;
1437
1438         mac_ctrl_data |= MAC_CTRL_BC_EN;
1439         if (netdev->flags & IFF_PROMISC)
1440                 mac_ctrl_data |= MAC_CTRL_PROMIS_EN;
1441         if (netdev->flags & IFF_ALLMULTI)
1442                 mac_ctrl_data |= MAC_CTRL_MC_ALL_EN;
1443
1444         mac_ctrl_data |= MAC_CTRL_SINGLE_PAUSE_EN;
1445         if (hw->nic_type == athr_l1d || hw->nic_type == athr_l2c_b2 ||
1446             hw->nic_type == athr_l1d_2) {
1447                 mac_ctrl_data |= MAC_CTRL_SPEED_MODE_SW;
1448                 mac_ctrl_data |= MAC_CTRL_HASH_ALG_CRC32;
1449         }
1450         AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
1451 }
1452
1453 /*
1454  * atl1c_configure - Configure Transmit&Receive Unit after Reset
1455  * @adapter: board private structure
1456  *
1457  * Configure the Tx /Rx unit of the MAC after a reset.
1458  */
1459 static int atl1c_configure(struct atl1c_adapter *adapter)
1460 {
1461         struct atl1c_hw *hw = &adapter->hw;
1462         u32 master_ctrl_data = 0;
1463         u32 intr_modrt_data;
1464         u32 data;
1465
1466         /* clear interrupt status */
1467         AT_WRITE_REG(hw, REG_ISR, 0xFFFFFFFF);
1468         /*  Clear any WOL status */
1469         AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
1470         /* set Interrupt Clear Timer
1471          * HW will enable self to assert interrupt event to system after
1472          * waiting x-time for software to notify it accept interrupt.
1473          */
1474
1475         data = CLK_GATING_EN_ALL;
1476         if (hw->ctrl_flags & ATL1C_CLK_GATING_EN) {
1477                 if (hw->nic_type == athr_l2c_b)
1478                         data &= ~CLK_GATING_RXMAC_EN;
1479         } else
1480                 data = 0;
1481         AT_WRITE_REG(hw, REG_CLK_GATING_CTRL, data);
1482
1483         AT_WRITE_REG(hw, REG_INT_RETRIG_TIMER,
1484                 hw->ict & INT_RETRIG_TIMER_MASK);
1485
1486         atl1c_configure_des_ring(adapter);
1487
1488         if (hw->ctrl_flags & ATL1C_INTR_MODRT_ENABLE) {
1489                 intr_modrt_data = (hw->tx_imt & IRQ_MODRT_TIMER_MASK) <<
1490                                         IRQ_MODRT_TX_TIMER_SHIFT;
1491                 intr_modrt_data |= (hw->rx_imt & IRQ_MODRT_TIMER_MASK) <<
1492                                         IRQ_MODRT_RX_TIMER_SHIFT;
1493                 AT_WRITE_REG(hw, REG_IRQ_MODRT_TIMER_INIT, intr_modrt_data);
1494                 master_ctrl_data |=
1495                         MASTER_CTRL_TX_ITIMER_EN | MASTER_CTRL_RX_ITIMER_EN;
1496         }
1497
1498         if (hw->ctrl_flags & ATL1C_INTR_CLEAR_ON_READ)
1499                 master_ctrl_data |= MASTER_CTRL_INT_RDCLR;
1500
1501         master_ctrl_data |= MASTER_CTRL_SA_TIMER_EN;
1502         AT_WRITE_REG(hw, REG_MASTER_CTRL, master_ctrl_data);
1503
1504         if (hw->ctrl_flags & ATL1C_CMB_ENABLE) {
1505                 AT_WRITE_REG(hw, REG_CMB_TPD_THRESH,
1506                         hw->cmb_tpd & CMB_TPD_THRESH_MASK);
1507                 AT_WRITE_REG(hw, REG_CMB_TX_TIMER,
1508                         hw->cmb_tx_timer & CMB_TX_TIMER_MASK);
1509         }
1510
1511         if (hw->ctrl_flags & ATL1C_SMB_ENABLE)
1512                 AT_WRITE_REG(hw, REG_SMB_STAT_TIMER,
1513                         hw->smb_timer & SMB_STAT_TIMER_MASK);
1514         /* set MTU */
1515         AT_WRITE_REG(hw, REG_MTU, hw->max_frame_size + ETH_HLEN +
1516                         VLAN_HLEN + ETH_FCS_LEN);
1517         /* HDS, disable */
1518         AT_WRITE_REG(hw, REG_HDS_CTRL, 0);
1519
1520         atl1c_configure_tx(adapter);
1521         atl1c_configure_rx(adapter);
1522         atl1c_configure_rss(adapter);
1523         atl1c_configure_dma(adapter);
1524
1525         return 0;
1526 }
1527
1528 static void atl1c_update_hw_stats(struct atl1c_adapter *adapter)
1529 {
1530         u16 hw_reg_addr = 0;
1531         unsigned long *stats_item = NULL;
1532         u32 data;
1533
1534         /* update rx status */
1535         hw_reg_addr = REG_MAC_RX_STATUS_BIN;
1536         stats_item  = &adapter->hw_stats.rx_ok;
1537         while (hw_reg_addr <= REG_MAC_RX_STATUS_END) {
1538                 AT_READ_REG(&adapter->hw, hw_reg_addr, &data);
1539                 *stats_item += data;
1540                 stats_item++;
1541                 hw_reg_addr += 4;
1542         }
1543 /* update tx status */
1544         hw_reg_addr = REG_MAC_TX_STATUS_BIN;
1545         stats_item  = &adapter->hw_stats.tx_ok;
1546         while (hw_reg_addr <= REG_MAC_TX_STATUS_END) {
1547                 AT_READ_REG(&adapter->hw, hw_reg_addr, &data);
1548                 *stats_item += data;
1549                 stats_item++;
1550                 hw_reg_addr += 4;
1551         }
1552 }
1553
1554 /*
1555  * atl1c_get_stats - Get System Network Statistics
1556  * @netdev: network interface device structure
1557  *
1558  * Returns the address of the device statistics structure.
1559  * The statistics are actually updated from the timer callback.
1560  */
1561 static struct net_device_stats *atl1c_get_stats(struct net_device *netdev)
1562 {
1563         struct atl1c_adapter *adapter = netdev_priv(netdev);
1564         struct atl1c_hw_stats  *hw_stats = &adapter->hw_stats;
1565         struct net_device_stats *net_stats = &netdev->stats;
1566
1567         atl1c_update_hw_stats(adapter);
1568         net_stats->rx_packets = hw_stats->rx_ok;
1569         net_stats->tx_packets = hw_stats->tx_ok;
1570         net_stats->rx_bytes   = hw_stats->rx_byte_cnt;
1571         net_stats->tx_bytes   = hw_stats->tx_byte_cnt;
1572         net_stats->multicast  = hw_stats->rx_mcast;
1573         net_stats->collisions = hw_stats->tx_1_col +
1574                                 hw_stats->tx_2_col * 2 +
1575                                 hw_stats->tx_late_col + hw_stats->tx_abort_col;
1576         net_stats->rx_errors  = hw_stats->rx_frag + hw_stats->rx_fcs_err +
1577                                 hw_stats->rx_len_err + hw_stats->rx_sz_ov +
1578                                 hw_stats->rx_rrd_ov + hw_stats->rx_align_err;
1579         net_stats->rx_fifo_errors   = hw_stats->rx_rxf_ov;
1580         net_stats->rx_length_errors = hw_stats->rx_len_err;
1581         net_stats->rx_crc_errors    = hw_stats->rx_fcs_err;
1582         net_stats->rx_frame_errors  = hw_stats->rx_align_err;
1583         net_stats->rx_over_errors   = hw_stats->rx_rrd_ov + hw_stats->rx_rxf_ov;
1584
1585         net_stats->rx_missed_errors = hw_stats->rx_rrd_ov + hw_stats->rx_rxf_ov;
1586
1587         net_stats->tx_errors = hw_stats->tx_late_col + hw_stats->tx_abort_col +
1588                                 hw_stats->tx_underrun + hw_stats->tx_trunc;
1589         net_stats->tx_fifo_errors    = hw_stats->tx_underrun;
1590         net_stats->tx_aborted_errors = hw_stats->tx_abort_col;
1591         net_stats->tx_window_errors  = hw_stats->tx_late_col;
1592
1593         return net_stats;
1594 }
1595
1596 static inline void atl1c_clear_phy_int(struct atl1c_adapter *adapter)
1597 {
1598         u16 phy_data;
1599
1600         spin_lock(&adapter->mdio_lock);
1601         atl1c_read_phy_reg(&adapter->hw, MII_ISR, &phy_data);
1602         spin_unlock(&adapter->mdio_lock);
1603 }
1604
1605 static bool atl1c_clean_tx_irq(struct atl1c_adapter *adapter,
1606                                 enum atl1c_trans_queue type)
1607 {
1608         struct atl1c_tpd_ring *tpd_ring = (struct atl1c_tpd_ring *)
1609                                 &adapter->tpd_ring[type];
1610         struct atl1c_buffer *buffer_info;
1611         struct pci_dev *pdev = adapter->pdev;
1612         u16 next_to_clean = atomic_read(&tpd_ring->next_to_clean);
1613         u16 hw_next_to_clean;
1614         u16 shift;
1615         u32 data;
1616
1617         if (type == atl1c_trans_high)
1618                 shift = MB_HTPD_CONS_IDX_SHIFT;
1619         else
1620                 shift = MB_NTPD_CONS_IDX_SHIFT;
1621
1622         AT_READ_REG(&adapter->hw, REG_MB_PRIO_CONS_IDX, &data);
1623         hw_next_to_clean = (data >> shift) & MB_PRIO_PROD_IDX_MASK;
1624
1625         while (next_to_clean != hw_next_to_clean) {
1626                 buffer_info = &tpd_ring->buffer_info[next_to_clean];
1627                 atl1c_clean_buffer(pdev, buffer_info, 1);
1628                 if (++next_to_clean == tpd_ring->count)
1629                         next_to_clean = 0;
1630                 atomic_set(&tpd_ring->next_to_clean, next_to_clean);
1631         }
1632
1633         if (netif_queue_stopped(adapter->netdev) &&
1634                         netif_carrier_ok(adapter->netdev)) {
1635                 netif_wake_queue(adapter->netdev);
1636         }
1637
1638         return true;
1639 }
1640
1641 /*
1642  * atl1c_intr - Interrupt Handler
1643  * @irq: interrupt number
1644  * @data: pointer to a network interface device structure
1645  * @pt_regs: CPU registers structure
1646  */
1647 static irqreturn_t atl1c_intr(int irq, void *data)
1648 {
1649         struct net_device *netdev  = data;
1650         struct atl1c_adapter *adapter = netdev_priv(netdev);
1651         struct pci_dev *pdev = adapter->pdev;
1652         struct atl1c_hw *hw = &adapter->hw;
1653         int max_ints = AT_MAX_INT_WORK;
1654         int handled = IRQ_NONE;
1655         u32 status;
1656         u32 reg_data;
1657
1658         do {
1659                 AT_READ_REG(hw, REG_ISR, &reg_data);
1660                 status = reg_data & hw->intr_mask;
1661
1662                 if (status == 0 || (status & ISR_DIS_INT) != 0) {
1663                         if (max_ints != AT_MAX_INT_WORK)
1664                                 handled = IRQ_HANDLED;
1665                         break;
1666                 }
1667                 /* link event */
1668                 if (status & ISR_GPHY)
1669                         atl1c_clear_phy_int(adapter);
1670                 /* Ack ISR */
1671                 AT_WRITE_REG(hw, REG_ISR, status | ISR_DIS_INT);
1672                 if (status & ISR_RX_PKT) {
1673                         if (likely(napi_schedule_prep(&adapter->napi))) {
1674                                 hw->intr_mask &= ~ISR_RX_PKT;
1675                                 AT_WRITE_REG(hw, REG_IMR, hw->intr_mask);
1676                                 __napi_schedule(&adapter->napi);
1677                         }
1678                 }
1679                 if (status & ISR_TX_PKT)
1680                         atl1c_clean_tx_irq(adapter, atl1c_trans_normal);
1681
1682                 handled = IRQ_HANDLED;
1683                 /* check if PCIE PHY Link down */
1684                 if (status & ISR_ERROR) {
1685                         if (netif_msg_hw(adapter))
1686                                 dev_err(&pdev->dev,
1687                                         "atl1c hardware error (status = 0x%x)\n",
1688                                         status & ISR_ERROR);
1689                         /* reset MAC */
1690                         adapter->work_event |= ATL1C_WORK_EVENT_RESET;
1691                         schedule_work(&adapter->common_task);
1692                         return IRQ_HANDLED;
1693                 }
1694
1695                 if (status & ISR_OVER)
1696                         if (netif_msg_intr(adapter))
1697                                 dev_warn(&pdev->dev,
1698                                         "TX/RX overflow (status = 0x%x)\n",
1699                                         status & ISR_OVER);
1700
1701                 /* link event */
1702                 if (status & (ISR_GPHY | ISR_MANUAL)) {
1703                         netdev->stats.tx_carrier_errors++;
1704                         atl1c_link_chg_event(adapter);
1705                         break;
1706                 }
1707
1708         } while (--max_ints > 0);
1709         /* re-enable Interrupt*/
1710         AT_WRITE_REG(&adapter->hw, REG_ISR, 0);
1711         return handled;
1712 }
1713
1714 static inline void atl1c_rx_checksum(struct atl1c_adapter *adapter,
1715                   struct sk_buff *skb, struct atl1c_recv_ret_status *prrs)
1716 {
1717         /*
1718          * The pid field in RRS in not correct sometimes, so we
1719          * cannot figure out if the packet is fragmented or not,
1720          * so we tell the KERNEL CHECKSUM_NONE
1721          */
1722         skb_checksum_none_assert(skb);
1723 }
1724
1725 static int atl1c_alloc_rx_buffer(struct atl1c_adapter *adapter, const int ringid)
1726 {
1727         struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring[ringid];
1728         struct pci_dev *pdev = adapter->pdev;
1729         struct atl1c_buffer *buffer_info, *next_info;
1730         struct sk_buff *skb;
1731         void *vir_addr = NULL;
1732         u16 num_alloc = 0;
1733         u16 rfd_next_to_use, next_next;
1734         struct atl1c_rx_free_desc *rfd_desc;
1735
1736         next_next = rfd_next_to_use = rfd_ring->next_to_use;
1737         if (++next_next == rfd_ring->count)
1738                 next_next = 0;
1739         buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
1740         next_info = &rfd_ring->buffer_info[next_next];
1741
1742         while (next_info->flags & ATL1C_BUFFER_FREE) {
1743                 rfd_desc = ATL1C_RFD_DESC(rfd_ring, rfd_next_to_use);
1744
1745                 skb = dev_alloc_skb(adapter->rx_buffer_len);
1746                 if (unlikely(!skb)) {
1747                         if (netif_msg_rx_err(adapter))
1748                                 dev_warn(&pdev->dev, "alloc rx buffer failed\n");
1749                         break;
1750                 }
1751
1752                 /*
1753                  * Make buffer alignment 2 beyond a 16 byte boundary
1754                  * this will result in a 16 byte aligned IP header after
1755                  * the 14 byte MAC header is removed
1756                  */
1757                 vir_addr = skb->data;
1758                 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
1759                 buffer_info->skb = skb;
1760                 buffer_info->length = adapter->rx_buffer_len;
1761                 buffer_info->dma = pci_map_single(pdev, vir_addr,
1762                                                 buffer_info->length,
1763                                                 PCI_DMA_FROMDEVICE);
1764                 ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_SINGLE,
1765                         ATL1C_PCIMAP_FROMDEVICE);
1766                 rfd_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
1767                 rfd_next_to_use = next_next;
1768                 if (++next_next == rfd_ring->count)
1769                         next_next = 0;
1770                 buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
1771                 next_info = &rfd_ring->buffer_info[next_next];
1772                 num_alloc++;
1773         }
1774
1775         if (num_alloc) {
1776                 /* TODO: update mailbox here */
1777                 wmb();
1778                 rfd_ring->next_to_use = rfd_next_to_use;
1779                 AT_WRITE_REG(&adapter->hw, atl1c_rfd_prod_idx_regs[ringid],
1780                         rfd_ring->next_to_use & MB_RFDX_PROD_IDX_MASK);
1781         }
1782
1783         return num_alloc;
1784 }
1785
1786 static void atl1c_clean_rrd(struct atl1c_rrd_ring *rrd_ring,
1787                         struct  atl1c_recv_ret_status *rrs, u16 num)
1788 {
1789         u16 i;
1790         /* the relationship between rrd and rfd is one map one */
1791         for (i = 0; i < num; i++, rrs = ATL1C_RRD_DESC(rrd_ring,
1792                                         rrd_ring->next_to_clean)) {
1793                 rrs->word3 &= ~RRS_RXD_UPDATED;
1794                 if (++rrd_ring->next_to_clean == rrd_ring->count)
1795                         rrd_ring->next_to_clean = 0;
1796         }
1797 }
1798
1799 static void atl1c_clean_rfd(struct atl1c_rfd_ring *rfd_ring,
1800         struct atl1c_recv_ret_status *rrs, u16 num)
1801 {
1802         u16 i;
1803         u16 rfd_index;
1804         struct atl1c_buffer *buffer_info = rfd_ring->buffer_info;
1805
1806         rfd_index = (rrs->word0 >> RRS_RX_RFD_INDEX_SHIFT) &
1807                         RRS_RX_RFD_INDEX_MASK;
1808         for (i = 0; i < num; i++) {
1809                 buffer_info[rfd_index].skb = NULL;
1810                 ATL1C_SET_BUFFER_STATE(&buffer_info[rfd_index],
1811                                         ATL1C_BUFFER_FREE);
1812                 if (++rfd_index == rfd_ring->count)
1813                         rfd_index = 0;
1814         }
1815         rfd_ring->next_to_clean = rfd_index;
1816 }
1817
1818 static void atl1c_clean_rx_irq(struct atl1c_adapter *adapter, u8 que,
1819                    int *work_done, int work_to_do)
1820 {
1821         u16 rfd_num, rfd_index;
1822         u16 count = 0;
1823         u16 length;
1824         struct pci_dev *pdev = adapter->pdev;
1825         struct net_device *netdev  = adapter->netdev;
1826         struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring[que];
1827         struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring[que];
1828         struct sk_buff *skb;
1829         struct atl1c_recv_ret_status *rrs;
1830         struct atl1c_buffer *buffer_info;
1831
1832         while (1) {
1833                 if (*work_done >= work_to_do)
1834                         break;
1835                 rrs = ATL1C_RRD_DESC(rrd_ring, rrd_ring->next_to_clean);
1836                 if (likely(RRS_RXD_IS_VALID(rrs->word3))) {
1837                         rfd_num = (rrs->word0 >> RRS_RX_RFD_CNT_SHIFT) &
1838                                 RRS_RX_RFD_CNT_MASK;
1839                         if (unlikely(rfd_num != 1))
1840                                 /* TODO support mul rfd*/
1841                                 if (netif_msg_rx_err(adapter))
1842                                         dev_warn(&pdev->dev,
1843                                                 "Multi rfd not support yet!\n");
1844                         goto rrs_checked;
1845                 } else {
1846                         break;
1847                 }
1848 rrs_checked:
1849                 atl1c_clean_rrd(rrd_ring, rrs, rfd_num);
1850                 if (rrs->word3 & (RRS_RX_ERR_SUM | RRS_802_3_LEN_ERR)) {
1851                         atl1c_clean_rfd(rfd_ring, rrs, rfd_num);
1852                                 if (netif_msg_rx_err(adapter))
1853                                         dev_warn(&pdev->dev,
1854                                                 "wrong packet! rrs word3 is %x\n",
1855                                                 rrs->word3);
1856                         continue;
1857                 }
1858
1859                 length = le16_to_cpu((rrs->word3 >> RRS_PKT_SIZE_SHIFT) &
1860                                 RRS_PKT_SIZE_MASK);
1861                 /* Good Receive */
1862                 if (likely(rfd_num == 1)) {
1863                         rfd_index = (rrs->word0 >> RRS_RX_RFD_INDEX_SHIFT) &
1864                                         RRS_RX_RFD_INDEX_MASK;
1865                         buffer_info = &rfd_ring->buffer_info[rfd_index];
1866                         pci_unmap_single(pdev, buffer_info->dma,
1867                                 buffer_info->length, PCI_DMA_FROMDEVICE);
1868                         skb = buffer_info->skb;
1869                 } else {
1870                         /* TODO */
1871                         if (netif_msg_rx_err(adapter))
1872                                 dev_warn(&pdev->dev,
1873                                         "Multi rfd not support yet!\n");
1874                         break;
1875                 }
1876                 atl1c_clean_rfd(rfd_ring, rrs, rfd_num);
1877                 skb_put(skb, length - ETH_FCS_LEN);
1878                 skb->protocol = eth_type_trans(skb, netdev);
1879                 atl1c_rx_checksum(adapter, skb, rrs);
1880                 if (unlikely(adapter->vlgrp) && rrs->word3 & RRS_VLAN_INS) {
1881                         u16 vlan;
1882
1883                         AT_TAG_TO_VLAN(rrs->vlan_tag, vlan);
1884                         vlan = le16_to_cpu(vlan);
1885                         vlan_hwaccel_receive_skb(skb, adapter->vlgrp, vlan);
1886                 } else
1887                         netif_receive_skb(skb);
1888
1889                 (*work_done)++;
1890                 count++;
1891         }
1892         if (count)
1893                 atl1c_alloc_rx_buffer(adapter, que);
1894 }
1895
1896 /*
1897  * atl1c_clean - NAPI Rx polling callback
1898  * @adapter: board private structure
1899  */
1900 static int atl1c_clean(struct napi_struct *napi, int budget)
1901 {
1902         struct atl1c_adapter *adapter =
1903                         container_of(napi, struct atl1c_adapter, napi);
1904         int work_done = 0;
1905
1906         /* Keep link state information with original netdev */
1907         if (!netif_carrier_ok(adapter->netdev))
1908                 goto quit_polling;
1909         /* just enable one RXQ */
1910         atl1c_clean_rx_irq(adapter, 0, &work_done, budget);
1911
1912         if (work_done < budget) {
1913 quit_polling:
1914                 napi_complete(napi);
1915                 adapter->hw.intr_mask |= ISR_RX_PKT;
1916                 AT_WRITE_REG(&adapter->hw, REG_IMR, adapter->hw.intr_mask);
1917         }
1918         return work_done;
1919 }
1920
1921 #ifdef CONFIG_NET_POLL_CONTROLLER
1922
1923 /*
1924  * Polling 'interrupt' - used by things like netconsole to send skbs
1925  * without having to re-enable interrupts. It's not called while
1926  * the interrupt routine is executing.
1927  */
1928 static void atl1c_netpoll(struct net_device *netdev)
1929 {
1930         struct atl1c_adapter *adapter = netdev_priv(netdev);
1931
1932         disable_irq(adapter->pdev->irq);
1933         atl1c_intr(adapter->pdev->irq, netdev);
1934         enable_irq(adapter->pdev->irq);
1935 }
1936 #endif
1937
1938 static inline u16 atl1c_tpd_avail(struct atl1c_adapter *adapter, enum atl1c_trans_queue type)
1939 {
1940         struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
1941         u16 next_to_use = 0;
1942         u16 next_to_clean = 0;
1943
1944         next_to_clean = atomic_read(&tpd_ring->next_to_clean);
1945         next_to_use   = tpd_ring->next_to_use;
1946
1947         return (u16)(next_to_clean > next_to_use) ?
1948                 (next_to_clean - next_to_use - 1) :
1949                 (tpd_ring->count + next_to_clean - next_to_use - 1);
1950 }
1951
1952 /*
1953  * get next usable tpd
1954  * Note: should call atl1c_tdp_avail to make sure
1955  * there is enough tpd to use
1956  */
1957 static struct atl1c_tpd_desc *atl1c_get_tpd(struct atl1c_adapter *adapter,
1958         enum atl1c_trans_queue type)
1959 {
1960         struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
1961         struct atl1c_tpd_desc *tpd_desc;
1962         u16 next_to_use = 0;
1963
1964         next_to_use = tpd_ring->next_to_use;
1965         if (++tpd_ring->next_to_use == tpd_ring->count)
1966                 tpd_ring->next_to_use = 0;
1967         tpd_desc = ATL1C_TPD_DESC(tpd_ring, next_to_use);
1968         memset(tpd_desc, 0, sizeof(struct atl1c_tpd_desc));
1969         return  tpd_desc;
1970 }
1971
1972 static struct atl1c_buffer *
1973 atl1c_get_tx_buffer(struct atl1c_adapter *adapter, struct atl1c_tpd_desc *tpd)
1974 {
1975         struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
1976
1977         return &tpd_ring->buffer_info[tpd -
1978                         (struct atl1c_tpd_desc *)tpd_ring->desc];
1979 }
1980
1981 /* Calculate the transmit packet descript needed*/
1982 static u16 atl1c_cal_tpd_req(const struct sk_buff *skb)
1983 {
1984         u16 tpd_req;
1985         u16 proto_hdr_len = 0;
1986
1987         tpd_req = skb_shinfo(skb)->nr_frags + 1;
1988
1989         if (skb_is_gso(skb)) {
1990                 proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1991                 if (proto_hdr_len < skb_headlen(skb))
1992                         tpd_req++;
1993                 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
1994                         tpd_req++;
1995         }
1996         return tpd_req;
1997 }
1998
1999 static int atl1c_tso_csum(struct atl1c_adapter *adapter,
2000                           struct sk_buff *skb,
2001                           struct atl1c_tpd_desc **tpd,
2002                           enum atl1c_trans_queue type)
2003 {
2004         struct pci_dev *pdev = adapter->pdev;
2005         u8 hdr_len;
2006         u32 real_len;
2007         unsigned short offload_type;
2008         int err;
2009
2010         if (skb_is_gso(skb)) {
2011                 if (skb_header_cloned(skb)) {
2012                         err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2013                         if (unlikely(err))
2014                                 return -1;
2015                 }
2016                 offload_type = skb_shinfo(skb)->gso_type;
2017
2018                 if (offload_type & SKB_GSO_TCPV4) {
2019                         real_len = (((unsigned char *)ip_hdr(skb) - skb->data)
2020                                         + ntohs(ip_hdr(skb)->tot_len));
2021
2022                         if (real_len < skb->len)
2023                                 pskb_trim(skb, real_len);
2024
2025                         hdr_len = (skb_transport_offset(skb) + tcp_hdrlen(skb));
2026                         if (unlikely(skb->len == hdr_len)) {
2027                                 /* only xsum need */
2028                                 if (netif_msg_tx_queued(adapter))
2029                                         dev_warn(&pdev->dev,
2030                                                 "IPV4 tso with zero data??\n");
2031                                 goto check_sum;
2032                         } else {
2033                                 ip_hdr(skb)->check = 0;
2034                                 tcp_hdr(skb)->check = ~csum_tcpudp_magic(
2035                                                         ip_hdr(skb)->saddr,
2036                                                         ip_hdr(skb)->daddr,
2037                                                         0, IPPROTO_TCP, 0);
2038                                 (*tpd)->word1 |= 1 << TPD_IPV4_PACKET_SHIFT;
2039                         }
2040                 }
2041
2042                 if (offload_type & SKB_GSO_TCPV6) {
2043                         struct atl1c_tpd_ext_desc *etpd =
2044                                 *(struct atl1c_tpd_ext_desc **)(tpd);
2045
2046                         memset(etpd, 0, sizeof(struct atl1c_tpd_ext_desc));
2047                         *tpd = atl1c_get_tpd(adapter, type);
2048                         ipv6_hdr(skb)->payload_len = 0;
2049                         /* check payload == 0 byte ? */
2050                         hdr_len = (skb_transport_offset(skb) + tcp_hdrlen(skb));
2051                         if (unlikely(skb->len == hdr_len)) {
2052                                 /* only xsum need */
2053                                 if (netif_msg_tx_queued(adapter))
2054                                         dev_warn(&pdev->dev,
2055                                                 "IPV6 tso with zero data??\n");
2056                                 goto check_sum;
2057                         } else
2058                                 tcp_hdr(skb)->check = ~csum_ipv6_magic(
2059                                                 &ipv6_hdr(skb)->saddr,
2060                                                 &ipv6_hdr(skb)->daddr,
2061                                                 0, IPPROTO_TCP, 0);
2062                         etpd->word1 |= 1 << TPD_LSO_EN_SHIFT;
2063                         etpd->word1 |= 1 << TPD_LSO_VER_SHIFT;
2064                         etpd->pkt_len = cpu_to_le32(skb->len);
2065                         (*tpd)->word1 |= 1 << TPD_LSO_VER_SHIFT;
2066                 }
2067
2068                 (*tpd)->word1 |= 1 << TPD_LSO_EN_SHIFT;
2069                 (*tpd)->word1 |= (skb_transport_offset(skb) & TPD_TCPHDR_OFFSET_MASK) <<
2070                                 TPD_TCPHDR_OFFSET_SHIFT;
2071                 (*tpd)->word1 |= (skb_shinfo(skb)->gso_size & TPD_MSS_MASK) <<
2072                                 TPD_MSS_SHIFT;
2073                 return 0;
2074         }
2075
2076 check_sum:
2077         if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
2078                 u8 css, cso;
2079                 cso = skb_transport_offset(skb);
2080
2081                 if (unlikely(cso & 0x1)) {
2082                         if (netif_msg_tx_err(adapter))
2083                                 dev_err(&adapter->pdev->dev,
2084                                         "payload offset should not an event number\n");
2085                         return -1;
2086                 } else {
2087                         css = cso + skb->csum_offset;
2088
2089                         (*tpd)->word1 |= ((cso >> 1) & TPD_PLOADOFFSET_MASK) <<
2090                                         TPD_PLOADOFFSET_SHIFT;
2091                         (*tpd)->word1 |= ((css >> 1) & TPD_CCSUM_OFFSET_MASK) <<
2092                                         TPD_CCSUM_OFFSET_SHIFT;
2093                         (*tpd)->word1 |= 1 << TPD_CCSUM_EN_SHIFT;
2094                 }
2095         }
2096         return 0;
2097 }
2098
2099 static void atl1c_tx_map(struct atl1c_adapter *adapter,
2100                       struct sk_buff *skb, struct atl1c_tpd_desc *tpd,
2101                         enum atl1c_trans_queue type)
2102 {
2103         struct atl1c_tpd_desc *use_tpd = NULL;
2104         struct atl1c_buffer *buffer_info = NULL;
2105         u16 buf_len = skb_headlen(skb);
2106         u16 map_len = 0;
2107         u16 mapped_len = 0;
2108         u16 hdr_len = 0;
2109         u16 nr_frags;
2110         u16 f;
2111         int tso;
2112
2113         nr_frags = skb_shinfo(skb)->nr_frags;
2114         tso = (tpd->word1 >> TPD_LSO_EN_SHIFT) & TPD_LSO_EN_MASK;
2115         if (tso) {
2116                 /* TSO */
2117                 map_len = hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2118                 use_tpd = tpd;
2119
2120                 buffer_info = atl1c_get_tx_buffer(adapter, use_tpd);
2121                 buffer_info->length = map_len;
2122                 buffer_info->dma = pci_map_single(adapter->pdev,
2123                                         skb->data, hdr_len, PCI_DMA_TODEVICE);
2124                 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
2125                 ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_SINGLE,
2126                         ATL1C_PCIMAP_TODEVICE);
2127                 mapped_len += map_len;
2128                 use_tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
2129                 use_tpd->buffer_len = cpu_to_le16(buffer_info->length);
2130         }
2131
2132         if (mapped_len < buf_len) {
2133                 /* mapped_len == 0, means we should use the first tpd,
2134                    which is given by caller  */
2135                 if (mapped_len == 0)
2136                         use_tpd = tpd;
2137                 else {
2138                         use_tpd = atl1c_get_tpd(adapter, type);
2139                         memcpy(use_tpd, tpd, sizeof(struct atl1c_tpd_desc));
2140                 }
2141                 buffer_info = atl1c_get_tx_buffer(adapter, use_tpd);
2142                 buffer_info->length = buf_len - mapped_len;
2143                 buffer_info->dma =
2144                         pci_map_single(adapter->pdev, skb->data + mapped_len,
2145                                         buffer_info->length, PCI_DMA_TODEVICE);
2146                 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
2147                 ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_SINGLE,
2148                         ATL1C_PCIMAP_TODEVICE);
2149                 use_tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
2150                 use_tpd->buffer_len  = cpu_to_le16(buffer_info->length);
2151         }
2152
2153         for (f = 0; f < nr_frags; f++) {
2154                 struct skb_frag_struct *frag;
2155
2156                 frag = &skb_shinfo(skb)->frags[f];
2157
2158                 use_tpd = atl1c_get_tpd(adapter, type);
2159                 memcpy(use_tpd, tpd, sizeof(struct atl1c_tpd_desc));
2160
2161                 buffer_info = atl1c_get_tx_buffer(adapter, use_tpd);
2162                 buffer_info->length = frag->size;
2163                 buffer_info->dma =
2164                         pci_map_page(adapter->pdev, frag->page,
2165                                         frag->page_offset,
2166                                         buffer_info->length,
2167                                         PCI_DMA_TODEVICE);
2168                 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
2169                 ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_PAGE,
2170                         ATL1C_PCIMAP_TODEVICE);
2171                 use_tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
2172                 use_tpd->buffer_len  = cpu_to_le16(buffer_info->length);
2173         }
2174
2175         /* The last tpd */
2176         use_tpd->word1 |= 1 << TPD_EOP_SHIFT;
2177         /* The last buffer info contain the skb address,
2178            so it will be free after unmap */
2179         buffer_info->skb = skb;
2180 }
2181
2182 static void atl1c_tx_queue(struct atl1c_adapter *adapter, struct sk_buff *skb,
2183                            struct atl1c_tpd_desc *tpd, enum atl1c_trans_queue type)
2184 {
2185         struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
2186         u32 prod_data;
2187
2188         AT_READ_REG(&adapter->hw, REG_MB_PRIO_PROD_IDX, &prod_data);
2189         switch (type) {
2190         case atl1c_trans_high:
2191                 prod_data &= 0xFFFF0000;
2192                 prod_data |= tpd_ring->next_to_use & 0xFFFF;
2193                 break;
2194         case atl1c_trans_normal:
2195                 prod_data &= 0x0000FFFF;
2196                 prod_data |= (tpd_ring->next_to_use & 0xFFFF) << 16;
2197                 break;
2198         default:
2199                 break;
2200         }
2201         wmb();
2202         AT_WRITE_REG(&adapter->hw, REG_MB_PRIO_PROD_IDX, prod_data);
2203 }
2204
2205 static netdev_tx_t atl1c_xmit_frame(struct sk_buff *skb,
2206                                           struct net_device *netdev)
2207 {
2208         struct atl1c_adapter *adapter = netdev_priv(netdev);
2209         unsigned long flags;
2210         u16 tpd_req = 1;
2211         struct atl1c_tpd_desc *tpd;
2212         enum atl1c_trans_queue type = atl1c_trans_normal;
2213
2214         if (test_bit(__AT_DOWN, &adapter->flags)) {
2215                 dev_kfree_skb_any(skb);
2216                 return NETDEV_TX_OK;
2217         }
2218
2219         tpd_req = atl1c_cal_tpd_req(skb);
2220         if (!spin_trylock_irqsave(&adapter->tx_lock, flags)) {
2221                 if (netif_msg_pktdata(adapter))
2222                         dev_info(&adapter->pdev->dev, "tx locked\n");
2223                 return NETDEV_TX_LOCKED;
2224         }
2225         if (skb->mark == 0x01)
2226                 type = atl1c_trans_high;
2227         else
2228                 type = atl1c_trans_normal;
2229
2230         if (atl1c_tpd_avail(adapter, type) < tpd_req) {
2231                 /* no enough descriptor, just stop queue */
2232                 netif_stop_queue(netdev);
2233                 spin_unlock_irqrestore(&adapter->tx_lock, flags);
2234                 return NETDEV_TX_BUSY;
2235         }
2236
2237         tpd = atl1c_get_tpd(adapter, type);
2238
2239         /* do TSO and check sum */
2240         if (atl1c_tso_csum(adapter, skb, &tpd, type) != 0) {
2241                 spin_unlock_irqrestore(&adapter->tx_lock, flags);
2242                 dev_kfree_skb_any(skb);
2243                 return NETDEV_TX_OK;
2244         }
2245
2246         if (unlikely(adapter->vlgrp && vlan_tx_tag_present(skb))) {
2247                 u16 vlan = vlan_tx_tag_get(skb);
2248                 __le16 tag;
2249
2250                 vlan = cpu_to_le16(vlan);
2251                 AT_VLAN_TO_TAG(vlan, tag);
2252                 tpd->word1 |= 1 << TPD_INS_VTAG_SHIFT;
2253                 tpd->vlan_tag = tag;
2254         }
2255
2256         if (skb_network_offset(skb) != ETH_HLEN)
2257                 tpd->word1 |= 1 << TPD_ETH_TYPE_SHIFT; /* Ethernet frame */
2258
2259         atl1c_tx_map(adapter, skb, tpd, type);
2260         atl1c_tx_queue(adapter, skb, tpd, type);
2261
2262         spin_unlock_irqrestore(&adapter->tx_lock, flags);
2263         return NETDEV_TX_OK;
2264 }
2265
2266 static void atl1c_free_irq(struct atl1c_adapter *adapter)
2267 {
2268         struct net_device *netdev = adapter->netdev;
2269
2270         free_irq(adapter->pdev->irq, netdev);
2271
2272         if (adapter->have_msi)
2273                 pci_disable_msi(adapter->pdev);
2274 }
2275
2276 static int atl1c_request_irq(struct atl1c_adapter *adapter)
2277 {
2278         struct pci_dev    *pdev   = adapter->pdev;
2279         struct net_device *netdev = adapter->netdev;
2280         int flags = 0;
2281         int err = 0;
2282
2283         adapter->have_msi = true;
2284         err = pci_enable_msi(adapter->pdev);
2285         if (err) {
2286                 if (netif_msg_ifup(adapter))
2287                         dev_err(&pdev->dev,
2288                                 "Unable to allocate MSI interrupt Error: %d\n",
2289                                 err);
2290                 adapter->have_msi = false;
2291         } else
2292                 netdev->irq = pdev->irq;
2293
2294         if (!adapter->have_msi)
2295                 flags |= IRQF_SHARED;
2296         err = request_irq(adapter->pdev->irq, atl1c_intr, flags,
2297                         netdev->name, netdev);
2298         if (err) {
2299                 if (netif_msg_ifup(adapter))
2300                         dev_err(&pdev->dev,
2301                                 "Unable to allocate interrupt Error: %d\n",
2302                                 err);
2303                 if (adapter->have_msi)
2304                         pci_disable_msi(adapter->pdev);
2305                 return err;
2306         }
2307         if (netif_msg_ifup(adapter))
2308                 dev_dbg(&pdev->dev, "atl1c_request_irq OK\n");
2309         return err;
2310 }
2311
2312 int atl1c_up(struct atl1c_adapter *adapter)
2313 {
2314         struct net_device *netdev = adapter->netdev;
2315         int num;
2316         int err;
2317         int i;
2318
2319         netif_carrier_off(netdev);
2320         atl1c_init_ring_ptrs(adapter);
2321         atl1c_set_multi(netdev);
2322         atl1c_restore_vlan(adapter);
2323
2324         for (i = 0; i < adapter->num_rx_queues; i++) {
2325                 num = atl1c_alloc_rx_buffer(adapter, i);
2326                 if (unlikely(num == 0)) {
2327                         err = -ENOMEM;
2328                         goto err_alloc_rx;
2329                 }
2330         }
2331
2332         if (atl1c_configure(adapter)) {
2333                 err = -EIO;
2334                 goto err_up;
2335         }
2336
2337         err = atl1c_request_irq(adapter);
2338         if (unlikely(err))
2339                 goto err_up;
2340
2341         clear_bit(__AT_DOWN, &adapter->flags);
2342         napi_enable(&adapter->napi);
2343         atl1c_irq_enable(adapter);
2344         atl1c_check_link_status(adapter);
2345         netif_start_queue(netdev);
2346         return err;
2347
2348 err_up:
2349 err_alloc_rx:
2350         atl1c_clean_rx_ring(adapter);
2351         return err;
2352 }
2353
2354 void atl1c_down(struct atl1c_adapter *adapter)
2355 {
2356         struct net_device *netdev = adapter->netdev;
2357
2358         atl1c_del_timer(adapter);
2359         adapter->work_event = 0; /* clear all event */
2360         /* signal that we're down so the interrupt handler does not
2361          * reschedule our watchdog timer */
2362         set_bit(__AT_DOWN, &adapter->flags);
2363         netif_carrier_off(netdev);
2364         napi_disable(&adapter->napi);
2365         atl1c_irq_disable(adapter);
2366         atl1c_free_irq(adapter);
2367         /* reset MAC to disable all RX/TX */
2368         atl1c_reset_mac(&adapter->hw);
2369         msleep(1);
2370
2371         adapter->link_speed = SPEED_0;
2372         adapter->link_duplex = -1;
2373         atl1c_clean_tx_ring(adapter, atl1c_trans_normal);
2374         atl1c_clean_tx_ring(adapter, atl1c_trans_high);
2375         atl1c_clean_rx_ring(adapter);
2376 }
2377
2378 /*
2379  * atl1c_open - Called when a network interface is made active
2380  * @netdev: network interface device structure
2381  *
2382  * Returns 0 on success, negative value on failure
2383  *
2384  * The open entry point is called when a network interface is made
2385  * active by the system (IFF_UP).  At this point all resources needed
2386  * for transmit and receive operations are allocated, the interrupt
2387  * handler is registered with the OS, the watchdog timer is started,
2388  * and the stack is notified that the interface is ready.
2389  */
2390 static int atl1c_open(struct net_device *netdev)
2391 {
2392         struct atl1c_adapter *adapter = netdev_priv(netdev);
2393         int err;
2394
2395         /* disallow open during test */
2396         if (test_bit(__AT_TESTING, &adapter->flags))
2397                 return -EBUSY;
2398
2399         /* allocate rx/tx dma buffer & descriptors */
2400         err = atl1c_setup_ring_resources(adapter);
2401         if (unlikely(err))
2402                 return err;
2403
2404         err = atl1c_up(adapter);
2405         if (unlikely(err))
2406                 goto err_up;
2407
2408         if (adapter->hw.ctrl_flags & ATL1C_FPGA_VERSION) {
2409                 u32 phy_data;
2410
2411                 AT_READ_REG(&adapter->hw, REG_MDIO_CTRL, &phy_data);
2412                 phy_data |= MDIO_AP_EN;
2413                 AT_WRITE_REG(&adapter->hw, REG_MDIO_CTRL, phy_data);
2414         }
2415         return 0;
2416
2417 err_up:
2418         atl1c_free_irq(adapter);
2419         atl1c_free_ring_resources(adapter);
2420         atl1c_reset_mac(&adapter->hw);
2421         return err;
2422 }
2423
2424 /*
2425  * atl1c_close - Disables a network interface
2426  * @netdev: network interface device structure
2427  *
2428  * Returns 0, this is not allowed to fail
2429  *
2430  * The close entry point is called when an interface is de-activated
2431  * by the OS.  The hardware is still under the drivers control, but
2432  * needs to be disabled.  A global MAC reset is issued to stop the
2433  * hardware, and all transmit and receive resources are freed.
2434  */
2435 static int atl1c_close(struct net_device *netdev)
2436 {
2437         struct atl1c_adapter *adapter = netdev_priv(netdev);
2438
2439         WARN_ON(test_bit(__AT_RESETTING, &adapter->flags));
2440         atl1c_down(adapter);
2441         atl1c_free_ring_resources(adapter);
2442         return 0;
2443 }
2444
2445 static int atl1c_suspend(struct pci_dev *pdev, pm_message_t state)
2446 {
2447         struct net_device *netdev = pci_get_drvdata(pdev);
2448         struct atl1c_adapter *adapter = netdev_priv(netdev);
2449         struct atl1c_hw *hw = &adapter->hw;
2450         u32 mac_ctrl_data = 0;
2451         u32 master_ctrl_data = 0;
2452         u32 wol_ctrl_data = 0;
2453         u16 mii_intr_status_data = 0;
2454         u32 wufc = adapter->wol;
2455         int retval = 0;
2456
2457         atl1c_disable_l0s_l1(hw);
2458         if (netif_running(netdev)) {
2459                 WARN_ON(test_bit(__AT_RESETTING, &adapter->flags));
2460                 atl1c_down(adapter);
2461         }
2462         netif_device_detach(netdev);
2463         retval = pci_save_state(pdev);
2464         if (retval)
2465                 return retval;
2466
2467         if (wufc)
2468                 if (atl1c_phy_power_saving(hw) != 0)
2469                         dev_dbg(&pdev->dev, "phy power saving failed");
2470
2471         AT_READ_REG(hw, REG_MASTER_CTRL, &master_ctrl_data);
2472         AT_READ_REG(hw, REG_MAC_CTRL, &mac_ctrl_data);
2473
2474         master_ctrl_data &= ~MASTER_CTRL_CLK_SEL_DIS;
2475         mac_ctrl_data &= ~(MAC_CTRL_PRMLEN_MASK << MAC_CTRL_PRMLEN_SHIFT);
2476         mac_ctrl_data |= (((u32)adapter->hw.preamble_len &
2477                         MAC_CTRL_PRMLEN_MASK) <<
2478                         MAC_CTRL_PRMLEN_SHIFT);
2479         mac_ctrl_data &= ~(MAC_CTRL_SPEED_MASK << MAC_CTRL_SPEED_SHIFT);
2480         mac_ctrl_data &= ~MAC_CTRL_DUPLX;
2481
2482         if (wufc) {
2483                 mac_ctrl_data |= MAC_CTRL_RX_EN;
2484                 if (adapter->link_speed == SPEED_1000 ||
2485                         adapter->link_speed == SPEED_0) {
2486                         mac_ctrl_data |= atl1c_mac_speed_1000 <<
2487                                         MAC_CTRL_SPEED_SHIFT;
2488                         mac_ctrl_data |= MAC_CTRL_DUPLX;
2489                 } else
2490                         mac_ctrl_data |= atl1c_mac_speed_10_100 <<
2491                                         MAC_CTRL_SPEED_SHIFT;
2492
2493                 if (adapter->link_duplex == DUPLEX_FULL)
2494                         mac_ctrl_data |= MAC_CTRL_DUPLX;
2495
2496                 /* turn on magic packet wol */
2497                 if (wufc & AT_WUFC_MAG)
2498                         wol_ctrl_data |= WOL_MAGIC_EN | WOL_MAGIC_PME_EN;
2499
2500                 if (wufc & AT_WUFC_LNKC) {
2501                         wol_ctrl_data |=  WOL_LINK_CHG_EN | WOL_LINK_CHG_PME_EN;
2502                         /* only link up can wake up */
2503                         if (atl1c_write_phy_reg(hw, MII_IER, IER_LINK_UP) != 0) {
2504                                 dev_dbg(&pdev->dev, "%s: read write phy "
2505                                                   "register failed.\n",
2506                                                   atl1c_driver_name);
2507                         }
2508                 }
2509                 /* clear phy interrupt */
2510                 atl1c_read_phy_reg(hw, MII_ISR, &mii_intr_status_data);
2511                 /* Config MAC Ctrl register */
2512                 if (adapter->vlgrp)
2513                         mac_ctrl_data |= MAC_CTRL_RMV_VLAN;
2514
2515                 /* magic packet maybe Broadcast&multicast&Unicast frame */
2516                 if (wufc & AT_WUFC_MAG)
2517                         mac_ctrl_data |= MAC_CTRL_BC_EN;
2518
2519                 dev_dbg(&pdev->dev,
2520                         "%s: suspend MAC=0x%x\n",
2521                         atl1c_driver_name, mac_ctrl_data);
2522                 AT_WRITE_REG(hw, REG_MASTER_CTRL, master_ctrl_data);
2523                 AT_WRITE_REG(hw, REG_WOL_CTRL, wol_ctrl_data);
2524                 AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
2525
2526                 /* pcie patch */
2527                 device_set_wakeup_enable(&pdev->dev, 1);
2528
2529                 AT_WRITE_REG(hw, REG_GPHY_CTRL, GPHY_CTRL_DEFAULT |
2530                         GPHY_CTRL_EXT_RESET);
2531                 pci_prepare_to_sleep(pdev);
2532         } else {
2533                 AT_WRITE_REG(hw, REG_GPHY_CTRL, GPHY_CTRL_POWER_SAVING);
2534                 master_ctrl_data |= MASTER_CTRL_CLK_SEL_DIS;
2535                 mac_ctrl_data |= atl1c_mac_speed_10_100 << MAC_CTRL_SPEED_SHIFT;
2536                 mac_ctrl_data |= MAC_CTRL_DUPLX;
2537                 AT_WRITE_REG(hw, REG_MASTER_CTRL, master_ctrl_data);
2538                 AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
2539                 AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
2540                 hw->phy_configured = false; /* re-init PHY when resume */
2541                 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
2542         }
2543
2544         pci_disable_device(pdev);
2545         pci_set_power_state(pdev, pci_choose_state(pdev, state));
2546
2547         return 0;
2548 }
2549
2550 static int atl1c_resume(struct pci_dev *pdev)
2551 {
2552         struct net_device *netdev = pci_get_drvdata(pdev);
2553         struct atl1c_adapter *adapter = netdev_priv(netdev);
2554
2555         pci_set_power_state(pdev, PCI_D0);
2556         pci_restore_state(pdev);
2557         pci_enable_wake(pdev, PCI_D3hot, 0);
2558         pci_enable_wake(pdev, PCI_D3cold, 0);
2559
2560         AT_WRITE_REG(&adapter->hw, REG_WOL_CTRL, 0);
2561         atl1c_reset_pcie(&adapter->hw, ATL1C_PCIE_L0S_L1_DISABLE |
2562                         ATL1C_PCIE_PHY_RESET);
2563
2564         atl1c_phy_reset(&adapter->hw);
2565         atl1c_reset_mac(&adapter->hw);
2566         atl1c_phy_init(&adapter->hw);
2567
2568 #if 0
2569         AT_READ_REG(&adapter->hw, REG_PM_CTRLSTAT, &pm_data);
2570         pm_data &= ~PM_CTRLSTAT_PME_EN;
2571         AT_WRITE_REG(&adapter->hw, REG_PM_CTRLSTAT, pm_data);
2572 #endif
2573
2574         netif_device_attach(netdev);
2575         if (netif_running(netdev))
2576                 atl1c_up(adapter);
2577
2578         return 0;
2579 }
2580
2581 static void atl1c_shutdown(struct pci_dev *pdev)
2582 {
2583         atl1c_suspend(pdev, PMSG_SUSPEND);
2584 }
2585
2586 static const struct net_device_ops atl1c_netdev_ops = {
2587         .ndo_open               = atl1c_open,
2588         .ndo_stop               = atl1c_close,
2589         .ndo_validate_addr      = eth_validate_addr,
2590         .ndo_start_xmit         = atl1c_xmit_frame,
2591         .ndo_set_mac_address    = atl1c_set_mac_addr,
2592         .ndo_set_multicast_list = atl1c_set_multi,
2593         .ndo_change_mtu         = atl1c_change_mtu,
2594         .ndo_do_ioctl           = atl1c_ioctl,
2595         .ndo_tx_timeout         = atl1c_tx_timeout,
2596         .ndo_get_stats          = atl1c_get_stats,
2597         .ndo_vlan_rx_register   = atl1c_vlan_rx_register,
2598 #ifdef CONFIG_NET_POLL_CONTROLLER
2599         .ndo_poll_controller    = atl1c_netpoll,
2600 #endif
2601 };
2602
2603 static int atl1c_init_netdev(struct net_device *netdev, struct pci_dev *pdev)
2604 {
2605         SET_NETDEV_DEV(netdev, &pdev->dev);
2606         pci_set_drvdata(pdev, netdev);
2607
2608         netdev->irq  = pdev->irq;
2609         netdev->netdev_ops = &atl1c_netdev_ops;
2610         netdev->watchdog_timeo = AT_TX_WATCHDOG;
2611         atl1c_set_ethtool_ops(netdev);
2612
2613         /* TODO: add when ready */
2614         netdev->features =      NETIF_F_SG         |
2615                                 NETIF_F_HW_CSUM    |
2616                                 NETIF_F_HW_VLAN_TX |
2617                                 NETIF_F_HW_VLAN_RX |
2618                                 NETIF_F_TSO        |
2619                                 NETIF_F_TSO6;
2620         return 0;
2621 }
2622
2623 /*
2624  * atl1c_probe - Device Initialization Routine
2625  * @pdev: PCI device information struct
2626  * @ent: entry in atl1c_pci_tbl
2627  *
2628  * Returns 0 on success, negative on failure
2629  *
2630  * atl1c_probe initializes an adapter identified by a pci_dev structure.
2631  * The OS initialization, configuring of the adapter private structure,
2632  * and a hardware reset occur.
2633  */
2634 static int __devinit atl1c_probe(struct pci_dev *pdev,
2635                                  const struct pci_device_id *ent)
2636 {
2637         struct net_device *netdev;
2638         struct atl1c_adapter *adapter;
2639         static int cards_found;
2640
2641         int err = 0;
2642
2643         /* enable device (incl. PCI PM wakeup and hotplug setup) */
2644         err = pci_enable_device_mem(pdev);
2645         if (err) {
2646                 dev_err(&pdev->dev, "cannot enable PCI device\n");
2647                 return err;
2648         }
2649
2650         /*
2651          * The atl1c chip can DMA to 64-bit addresses, but it uses a single
2652          * shared register for the high 32 bits, so only a single, aligned,
2653          * 4 GB physical address range can be used at a time.
2654          *
2655          * Supporting 64-bit DMA on this hardware is more trouble than it's
2656          * worth.  It is far easier to limit to 32-bit DMA than update
2657          * various kernel subsystems to support the mechanics required by a
2658          * fixed-high-32-bit system.
2659          */
2660         if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
2661             (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)) {
2662                 dev_err(&pdev->dev, "No usable DMA configuration,aborting\n");
2663                 goto err_dma;
2664         }
2665
2666         err = pci_request_regions(pdev, atl1c_driver_name);
2667         if (err) {
2668                 dev_err(&pdev->dev, "cannot obtain PCI resources\n");
2669                 goto err_pci_reg;
2670         }
2671
2672         pci_set_master(pdev);
2673
2674         netdev = alloc_etherdev(sizeof(struct atl1c_adapter));
2675         if (netdev == NULL) {
2676                 err = -ENOMEM;
2677                 dev_err(&pdev->dev, "etherdev alloc failed\n");
2678                 goto err_alloc_etherdev;
2679         }
2680
2681         err = atl1c_init_netdev(netdev, pdev);
2682         if (err) {
2683                 dev_err(&pdev->dev, "init netdevice failed\n");
2684                 goto err_init_netdev;
2685         }
2686         adapter = netdev_priv(netdev);
2687         adapter->bd_number = cards_found;
2688         adapter->netdev = netdev;
2689         adapter->pdev = pdev;
2690         adapter->hw.adapter = adapter;
2691         adapter->msg_enable = netif_msg_init(-1, atl1c_default_msg);
2692         adapter->hw.hw_addr = ioremap(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
2693         if (!adapter->hw.hw_addr) {
2694                 err = -EIO;
2695                 dev_err(&pdev->dev, "cannot map device registers\n");
2696                 goto err_ioremap;
2697         }
2698         netdev->base_addr = (unsigned long)adapter->hw.hw_addr;
2699
2700         /* init mii data */
2701         adapter->mii.dev = netdev;
2702         adapter->mii.mdio_read  = atl1c_mdio_read;
2703         adapter->mii.mdio_write = atl1c_mdio_write;
2704         adapter->mii.phy_id_mask = 0x1f;
2705         adapter->mii.reg_num_mask = MDIO_REG_ADDR_MASK;
2706         netif_napi_add(netdev, &adapter->napi, atl1c_clean, 64);
2707         setup_timer(&adapter->phy_config_timer, atl1c_phy_config,
2708                         (unsigned long)adapter);
2709         /* setup the private structure */
2710         err = atl1c_sw_init(adapter);
2711         if (err) {
2712                 dev_err(&pdev->dev, "net device private data init failed\n");
2713                 goto err_sw_init;
2714         }
2715         atl1c_reset_pcie(&adapter->hw, ATL1C_PCIE_L0S_L1_DISABLE |
2716                         ATL1C_PCIE_PHY_RESET);
2717
2718         /* Init GPHY as early as possible due to power saving issue  */
2719         atl1c_phy_reset(&adapter->hw);
2720
2721         err = atl1c_reset_mac(&adapter->hw);
2722         if (err) {
2723                 err = -EIO;
2724                 goto err_reset;
2725         }
2726
2727         device_init_wakeup(&pdev->dev, 1);
2728         /* reset the controller to
2729          * put the device in a known good starting state */
2730         err = atl1c_phy_init(&adapter->hw);
2731         if (err) {
2732                 err = -EIO;
2733                 goto err_reset;
2734         }
2735         if (atl1c_read_mac_addr(&adapter->hw) != 0) {
2736                 err = -EIO;
2737                 dev_err(&pdev->dev, "get mac address failed\n");
2738                 goto err_eeprom;
2739         }
2740         memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
2741         memcpy(netdev->perm_addr, adapter->hw.mac_addr, netdev->addr_len);
2742         if (netif_msg_probe(adapter))
2743                 dev_dbg(&pdev->dev, "mac address : %pM\n",
2744                         adapter->hw.mac_addr);
2745
2746         atl1c_hw_set_mac_addr(&adapter->hw);
2747         INIT_WORK(&adapter->common_task, atl1c_common_task);
2748         adapter->work_event = 0;
2749         err = register_netdev(netdev);
2750         if (err) {
2751                 dev_err(&pdev->dev, "register netdevice failed\n");
2752                 goto err_register;
2753         }
2754
2755         if (netif_msg_probe(adapter))
2756                 dev_info(&pdev->dev, "version %s\n", ATL1C_DRV_VERSION);
2757         cards_found++;
2758         return 0;
2759
2760 err_reset:
2761 err_register:
2762 err_sw_init:
2763 err_eeprom:
2764         iounmap(adapter->hw.hw_addr);
2765 err_init_netdev:
2766 err_ioremap:
2767         free_netdev(netdev);
2768 err_alloc_etherdev:
2769         pci_release_regions(pdev);
2770 err_pci_reg:
2771 err_dma:
2772         pci_disable_device(pdev);
2773         return err;
2774 }
2775
2776 /*
2777  * atl1c_remove - Device Removal Routine
2778  * @pdev: PCI device information struct
2779  *
2780  * atl1c_remove is called by the PCI subsystem to alert the driver
2781  * that it should release a PCI device.  The could be caused by a
2782  * Hot-Plug event, or because the driver is going to be removed from
2783  * memory.
2784  */
2785 static void __devexit atl1c_remove(struct pci_dev *pdev)
2786 {
2787         struct net_device *netdev = pci_get_drvdata(pdev);
2788         struct atl1c_adapter *adapter = netdev_priv(netdev);
2789
2790         unregister_netdev(netdev);
2791         atl1c_phy_disable(&adapter->hw);
2792
2793         iounmap(adapter->hw.hw_addr);
2794
2795         pci_release_regions(pdev);
2796         pci_disable_device(pdev);
2797         free_netdev(netdev);
2798 }
2799
2800 /*
2801  * atl1c_io_error_detected - called when PCI error is detected
2802  * @pdev: Pointer to PCI device
2803  * @state: The current pci connection state
2804  *
2805  * This function is called after a PCI bus error affecting
2806  * this device has been detected.
2807  */
2808 static pci_ers_result_t atl1c_io_error_detected(struct pci_dev *pdev,
2809                                                 pci_channel_state_t state)
2810 {
2811         struct net_device *netdev = pci_get_drvdata(pdev);
2812         struct atl1c_adapter *adapter = netdev_priv(netdev);
2813
2814         netif_device_detach(netdev);
2815
2816         if (state == pci_channel_io_perm_failure)
2817                 return PCI_ERS_RESULT_DISCONNECT;
2818
2819         if (netif_running(netdev))
2820                 atl1c_down(adapter);
2821
2822         pci_disable_device(pdev);
2823
2824         /* Request a slot slot reset. */
2825         return PCI_ERS_RESULT_NEED_RESET;
2826 }
2827
2828 /*
2829  * atl1c_io_slot_reset - called after the pci bus has been reset.
2830  * @pdev: Pointer to PCI device
2831  *
2832  * Restart the card from scratch, as if from a cold-boot. Implementation
2833  * resembles the first-half of the e1000_resume routine.
2834  */
2835 static pci_ers_result_t atl1c_io_slot_reset(struct pci_dev *pdev)
2836 {
2837         struct net_device *netdev = pci_get_drvdata(pdev);
2838         struct atl1c_adapter *adapter = netdev_priv(netdev);
2839
2840         if (pci_enable_device(pdev)) {
2841                 if (netif_msg_hw(adapter))
2842                         dev_err(&pdev->dev,
2843                                 "Cannot re-enable PCI device after reset\n");
2844                 return PCI_ERS_RESULT_DISCONNECT;
2845         }
2846         pci_set_master(pdev);
2847
2848         pci_enable_wake(pdev, PCI_D3hot, 0);
2849         pci_enable_wake(pdev, PCI_D3cold, 0);
2850
2851         atl1c_reset_mac(&adapter->hw);
2852
2853         return PCI_ERS_RESULT_RECOVERED;
2854 }
2855
2856 /*
2857  * atl1c_io_resume - called when traffic can start flowing again.
2858  * @pdev: Pointer to PCI device
2859  *
2860  * This callback is called when the error recovery driver tells us that
2861  * its OK to resume normal operation. Implementation resembles the
2862  * second-half of the atl1c_resume routine.
2863  */
2864 static void atl1c_io_resume(struct pci_dev *pdev)
2865 {
2866         struct net_device *netdev = pci_get_drvdata(pdev);
2867         struct atl1c_adapter *adapter = netdev_priv(netdev);
2868
2869         if (netif_running(netdev)) {
2870                 if (atl1c_up(adapter)) {
2871                         if (netif_msg_hw(adapter))
2872                                 dev_err(&pdev->dev,
2873                                         "Cannot bring device back up after reset\n");
2874                         return;
2875                 }
2876         }
2877
2878         netif_device_attach(netdev);
2879 }
2880
2881 static struct pci_error_handlers atl1c_err_handler = {
2882         .error_detected = atl1c_io_error_detected,
2883         .slot_reset = atl1c_io_slot_reset,
2884         .resume = atl1c_io_resume,
2885 };
2886
2887 static struct pci_driver atl1c_driver = {
2888         .name     = atl1c_driver_name,
2889         .id_table = atl1c_pci_tbl,
2890         .probe    = atl1c_probe,
2891         .remove   = __devexit_p(atl1c_remove),
2892         /* Power Managment Hooks */
2893         .suspend  = atl1c_suspend,
2894         .resume   = atl1c_resume,
2895         .shutdown = atl1c_shutdown,
2896         .err_handler = &atl1c_err_handler
2897 };
2898
2899 /*
2900  * atl1c_init_module - Driver Registration Routine
2901  *
2902  * atl1c_init_module is the first routine called when the driver is
2903  * loaded. All it does is register with the PCI subsystem.
2904  */
2905 static int __init atl1c_init_module(void)
2906 {
2907         return pci_register_driver(&atl1c_driver);
2908 }
2909
2910 /*
2911  * atl1c_exit_module - Driver Exit Cleanup Routine
2912  *
2913  * atl1c_exit_module is called just before the driver is removed
2914  * from memory.
2915  */
2916 static void __exit atl1c_exit_module(void)
2917 {
2918         pci_unregister_driver(&atl1c_driver);
2919 }
2920
2921 module_init(atl1c_init_module);
2922 module_exit(atl1c_exit_module);