dd893b38e70d4b9ccba5eb5672e600115c5d8797
[pandora-kernel.git] / drivers / net / ethernet / atheros / atl1e / atl1e_main.c
1 /*
2  * Copyright(c) 2007 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 "atl1e.h"
23
24 #define DRV_VERSION "1.0.0.7-NAPI"
25
26 char atl1e_driver_name[] = "ATL1E";
27 char atl1e_driver_version[] = DRV_VERSION;
28 #define PCI_DEVICE_ID_ATTANSIC_L1E      0x1026
29 /*
30  * atl1e_pci_tbl - PCI Device ID Table
31  *
32  * Wildcard entries (PCI_ANY_ID) should come last
33  * Last entry must be all 0s
34  *
35  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
36  *   Class, Class Mask, private data (not used) }
37  */
38 static DEFINE_PCI_DEVICE_TABLE(atl1e_pci_tbl) = {
39         {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L1E)},
40         {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, 0x1066)},
41         /* required last entry */
42         { 0 }
43 };
44 MODULE_DEVICE_TABLE(pci, atl1e_pci_tbl);
45
46 MODULE_AUTHOR("Atheros Corporation, <xiong.huang@atheros.com>, Jie Yang <jie.yang@atheros.com>");
47 MODULE_DESCRIPTION("Atheros 1000M Ethernet Network Driver");
48 MODULE_LICENSE("GPL");
49 MODULE_VERSION(DRV_VERSION);
50
51 static void atl1e_setup_mac_ctrl(struct atl1e_adapter *adapter);
52
53 static const u16
54 atl1e_rx_page_vld_regs[AT_MAX_RECEIVE_QUEUE][AT_PAGE_NUM_PER_QUEUE] =
55 {
56         {REG_HOST_RXF0_PAGE0_VLD, REG_HOST_RXF0_PAGE1_VLD},
57         {REG_HOST_RXF1_PAGE0_VLD, REG_HOST_RXF1_PAGE1_VLD},
58         {REG_HOST_RXF2_PAGE0_VLD, REG_HOST_RXF2_PAGE1_VLD},
59         {REG_HOST_RXF3_PAGE0_VLD, REG_HOST_RXF3_PAGE1_VLD}
60 };
61
62 static const u16 atl1e_rx_page_hi_addr_regs[AT_MAX_RECEIVE_QUEUE] =
63 {
64         REG_RXF0_BASE_ADDR_HI,
65         REG_RXF1_BASE_ADDR_HI,
66         REG_RXF2_BASE_ADDR_HI,
67         REG_RXF3_BASE_ADDR_HI
68 };
69
70 static const u16
71 atl1e_rx_page_lo_addr_regs[AT_MAX_RECEIVE_QUEUE][AT_PAGE_NUM_PER_QUEUE] =
72 {
73         {REG_HOST_RXF0_PAGE0_LO, REG_HOST_RXF0_PAGE1_LO},
74         {REG_HOST_RXF1_PAGE0_LO, REG_HOST_RXF1_PAGE1_LO},
75         {REG_HOST_RXF2_PAGE0_LO, REG_HOST_RXF2_PAGE1_LO},
76         {REG_HOST_RXF3_PAGE0_LO, REG_HOST_RXF3_PAGE1_LO}
77 };
78
79 static const u16
80 atl1e_rx_page_write_offset_regs[AT_MAX_RECEIVE_QUEUE][AT_PAGE_NUM_PER_QUEUE] =
81 {
82         {REG_HOST_RXF0_MB0_LO,  REG_HOST_RXF0_MB1_LO},
83         {REG_HOST_RXF1_MB0_LO,  REG_HOST_RXF1_MB1_LO},
84         {REG_HOST_RXF2_MB0_LO,  REG_HOST_RXF2_MB1_LO},
85         {REG_HOST_RXF3_MB0_LO,  REG_HOST_RXF3_MB1_LO}
86 };
87
88 static const u16 atl1e_pay_load_size[] = {
89         128, 256, 512, 1024, 2048, 4096,
90 };
91
92 /*
93  * atl1e_irq_enable - Enable default interrupt generation settings
94  * @adapter: board private structure
95  */
96 static inline void atl1e_irq_enable(struct atl1e_adapter *adapter)
97 {
98         if (likely(atomic_dec_and_test(&adapter->irq_sem))) {
99                 AT_WRITE_REG(&adapter->hw, REG_ISR, 0);
100                 AT_WRITE_REG(&adapter->hw, REG_IMR, IMR_NORMAL_MASK);
101                 AT_WRITE_FLUSH(&adapter->hw);
102         }
103 }
104
105 /*
106  * atl1e_irq_disable - Mask off interrupt generation on the NIC
107  * @adapter: board private structure
108  */
109 static inline void atl1e_irq_disable(struct atl1e_adapter *adapter)
110 {
111         atomic_inc(&adapter->irq_sem);
112         AT_WRITE_REG(&adapter->hw, REG_IMR, 0);
113         AT_WRITE_FLUSH(&adapter->hw);
114         synchronize_irq(adapter->pdev->irq);
115 }
116
117 /*
118  * atl1e_irq_reset - reset interrupt confiure on the NIC
119  * @adapter: board private structure
120  */
121 static inline void atl1e_irq_reset(struct atl1e_adapter *adapter)
122 {
123         atomic_set(&adapter->irq_sem, 0);
124         AT_WRITE_REG(&adapter->hw, REG_ISR, 0);
125         AT_WRITE_REG(&adapter->hw, REG_IMR, 0);
126         AT_WRITE_FLUSH(&adapter->hw);
127 }
128
129 /*
130  * atl1e_phy_config - Timer Call-back
131  * @data: pointer to netdev cast into an unsigned long
132  */
133 static void atl1e_phy_config(unsigned long data)
134 {
135         struct atl1e_adapter *adapter = (struct atl1e_adapter *) data;
136         struct atl1e_hw *hw = &adapter->hw;
137         unsigned long flags;
138
139         spin_lock_irqsave(&adapter->mdio_lock, flags);
140         atl1e_restart_autoneg(hw);
141         spin_unlock_irqrestore(&adapter->mdio_lock, flags);
142 }
143
144 void atl1e_reinit_locked(struct atl1e_adapter *adapter)
145 {
146
147         WARN_ON(in_interrupt());
148         while (test_and_set_bit(__AT_RESETTING, &adapter->flags))
149                 msleep(1);
150         atl1e_down(adapter);
151         atl1e_up(adapter);
152         clear_bit(__AT_RESETTING, &adapter->flags);
153 }
154
155 static void atl1e_reset_task(struct work_struct *work)
156 {
157         struct atl1e_adapter *adapter;
158         adapter = container_of(work, struct atl1e_adapter, reset_task);
159
160         atl1e_reinit_locked(adapter);
161 }
162
163 static int atl1e_check_link(struct atl1e_adapter *adapter)
164 {
165         struct atl1e_hw *hw = &adapter->hw;
166         struct net_device *netdev = adapter->netdev;
167         int err = 0;
168         u16 speed, duplex, phy_data;
169
170         /* MII_BMSR must read twice */
171         atl1e_read_phy_reg(hw, MII_BMSR, &phy_data);
172         atl1e_read_phy_reg(hw, MII_BMSR, &phy_data);
173         if ((phy_data & BMSR_LSTATUS) == 0) {
174                 /* link down */
175                 if (netif_carrier_ok(netdev)) { /* old link state: Up */
176                         u32 value;
177                         /* disable rx */
178                         value = AT_READ_REG(hw, REG_MAC_CTRL);
179                         value &= ~MAC_CTRL_RX_EN;
180                         AT_WRITE_REG(hw, REG_MAC_CTRL, value);
181                         adapter->link_speed = SPEED_0;
182                         netif_carrier_off(netdev);
183                         netif_stop_queue(netdev);
184                 }
185         } else {
186                 /* Link Up */
187                 err = atl1e_get_speed_and_duplex(hw, &speed, &duplex);
188                 if (unlikely(err))
189                         return err;
190
191                 /* link result is our setting */
192                 if (adapter->link_speed != speed ||
193                     adapter->link_duplex != duplex) {
194                         adapter->link_speed  = speed;
195                         adapter->link_duplex = duplex;
196                         atl1e_setup_mac_ctrl(adapter);
197                         netdev_info(netdev,
198                                     "NIC Link is Up <%d Mbps %s Duplex>\n",
199                                     adapter->link_speed,
200                                     adapter->link_duplex == FULL_DUPLEX ?
201                                     "Full" : "Half");
202                 }
203
204                 if (!netif_carrier_ok(netdev)) {
205                         /* Link down -> Up */
206                         netif_carrier_on(netdev);
207                         netif_wake_queue(netdev);
208                 }
209         }
210         return 0;
211 }
212
213 /*
214  * atl1e_link_chg_task - deal with link change event Out of interrupt context
215  * @netdev: network interface device structure
216  */
217 static void atl1e_link_chg_task(struct work_struct *work)
218 {
219         struct atl1e_adapter *adapter;
220         unsigned long flags;
221
222         adapter = container_of(work, struct atl1e_adapter, link_chg_task);
223         spin_lock_irqsave(&adapter->mdio_lock, flags);
224         atl1e_check_link(adapter);
225         spin_unlock_irqrestore(&adapter->mdio_lock, flags);
226 }
227
228 static void atl1e_link_chg_event(struct atl1e_adapter *adapter)
229 {
230         struct net_device *netdev = adapter->netdev;
231         u16 phy_data = 0;
232         u16 link_up = 0;
233
234         spin_lock(&adapter->mdio_lock);
235         atl1e_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
236         atl1e_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
237         spin_unlock(&adapter->mdio_lock);
238         link_up = phy_data & BMSR_LSTATUS;
239         /* notify upper layer link down ASAP */
240         if (!link_up) {
241                 if (netif_carrier_ok(netdev)) {
242                         /* old link state: Up */
243                         netdev_info(netdev, "NIC Link is Down\n");
244                         adapter->link_speed = SPEED_0;
245                         netif_stop_queue(netdev);
246                 }
247         }
248         schedule_work(&adapter->link_chg_task);
249 }
250
251 static void atl1e_del_timer(struct atl1e_adapter *adapter)
252 {
253         del_timer_sync(&adapter->phy_config_timer);
254 }
255
256 static void atl1e_cancel_work(struct atl1e_adapter *adapter)
257 {
258         cancel_work_sync(&adapter->reset_task);
259         cancel_work_sync(&adapter->link_chg_task);
260 }
261
262 /*
263  * atl1e_tx_timeout - Respond to a Tx Hang
264  * @netdev: network interface device structure
265  */
266 static void atl1e_tx_timeout(struct net_device *netdev)
267 {
268         struct atl1e_adapter *adapter = netdev_priv(netdev);
269
270         /* Do the reset outside of interrupt context */
271         schedule_work(&adapter->reset_task);
272 }
273
274 /*
275  * atl1e_set_multi - Multicast and Promiscuous mode set
276  * @netdev: network interface device structure
277  *
278  * The set_multi entry point is called whenever the multicast address
279  * list or the network interface flags are updated.  This routine is
280  * responsible for configuring the hardware for proper multicast,
281  * promiscuous mode, and all-multi behavior.
282  */
283 static void atl1e_set_multi(struct net_device *netdev)
284 {
285         struct atl1e_adapter *adapter = netdev_priv(netdev);
286         struct atl1e_hw *hw = &adapter->hw;
287         struct netdev_hw_addr *ha;
288         u32 mac_ctrl_data = 0;
289         u32 hash_value;
290
291         /* Check for Promiscuous and All Multicast modes */
292         mac_ctrl_data = AT_READ_REG(hw, REG_MAC_CTRL);
293
294         if (netdev->flags & IFF_PROMISC) {
295                 mac_ctrl_data |= MAC_CTRL_PROMIS_EN;
296         } else if (netdev->flags & IFF_ALLMULTI) {
297                 mac_ctrl_data |= MAC_CTRL_MC_ALL_EN;
298                 mac_ctrl_data &= ~MAC_CTRL_PROMIS_EN;
299         } else {
300                 mac_ctrl_data &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
301         }
302
303         AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
304
305         /* clear the old settings from the multicast hash table */
306         AT_WRITE_REG(hw, REG_RX_HASH_TABLE, 0);
307         AT_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, 1, 0);
308
309         /* comoute mc addresses' hash value ,and put it into hash table */
310         netdev_for_each_mc_addr(ha, netdev) {
311                 hash_value = atl1e_hash_mc_addr(hw, ha->addr);
312                 atl1e_hash_set(hw, hash_value);
313         }
314 }
315
316 static void __atl1e_vlan_mode(u32 features, u32 *mac_ctrl_data)
317 {
318         if (features & NETIF_F_HW_VLAN_RX) {
319                 /* enable VLAN tag insert/strip */
320                 *mac_ctrl_data |= MAC_CTRL_RMV_VLAN;
321         } else {
322                 /* disable VLAN tag insert/strip */
323                 *mac_ctrl_data &= ~MAC_CTRL_RMV_VLAN;
324         }
325 }
326
327 static void atl1e_vlan_mode(struct net_device *netdev, u32 features)
328 {
329         struct atl1e_adapter *adapter = netdev_priv(netdev);
330         u32 mac_ctrl_data = 0;
331
332         netdev_dbg(adapter->netdev, "%s\n", __func__);
333
334         atl1e_irq_disable(adapter);
335         mac_ctrl_data = AT_READ_REG(&adapter->hw, REG_MAC_CTRL);
336         __atl1e_vlan_mode(features, &mac_ctrl_data);
337         AT_WRITE_REG(&adapter->hw, REG_MAC_CTRL, mac_ctrl_data);
338         atl1e_irq_enable(adapter);
339 }
340
341 static void atl1e_restore_vlan(struct atl1e_adapter *adapter)
342 {
343         netdev_dbg(adapter->netdev, "%s\n", __func__);
344         atl1e_vlan_mode(adapter->netdev, adapter->netdev->features);
345 }
346
347 /*
348  * atl1e_set_mac - Change the Ethernet Address of the NIC
349  * @netdev: network interface device structure
350  * @p: pointer to an address structure
351  *
352  * Returns 0 on success, negative on failure
353  */
354 static int atl1e_set_mac_addr(struct net_device *netdev, void *p)
355 {
356         struct atl1e_adapter *adapter = netdev_priv(netdev);
357         struct sockaddr *addr = p;
358
359         if (!is_valid_ether_addr(addr->sa_data))
360                 return -EADDRNOTAVAIL;
361
362         if (netif_running(netdev))
363                 return -EBUSY;
364
365         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
366         memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
367
368         atl1e_hw_set_mac_addr(&adapter->hw);
369
370         return 0;
371 }
372
373 static u32 atl1e_fix_features(struct net_device *netdev, u32 features)
374 {
375         /*
376          * Since there is no support for separate rx/tx vlan accel
377          * enable/disable make sure tx flag is always in same state as rx.
378          */
379         if (features & NETIF_F_HW_VLAN_RX)
380                 features |= NETIF_F_HW_VLAN_TX;
381         else
382                 features &= ~NETIF_F_HW_VLAN_TX;
383
384         return features;
385 }
386
387 static int atl1e_set_features(struct net_device *netdev, u32 features)
388 {
389         u32 changed = netdev->features ^ features;
390
391         if (changed & NETIF_F_HW_VLAN_RX)
392                 atl1e_vlan_mode(netdev, features);
393
394         return 0;
395 }
396
397 /*
398  * atl1e_change_mtu - Change the Maximum Transfer Unit
399  * @netdev: network interface device structure
400  * @new_mtu: new value for maximum frame size
401  *
402  * Returns 0 on success, negative on failure
403  */
404 static int atl1e_change_mtu(struct net_device *netdev, int new_mtu)
405 {
406         struct atl1e_adapter *adapter = netdev_priv(netdev);
407         int old_mtu   = netdev->mtu;
408         int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
409
410         if ((max_frame < ETH_ZLEN + ETH_FCS_LEN) ||
411                         (max_frame > MAX_JUMBO_FRAME_SIZE)) {
412                 netdev_warn(adapter->netdev, "invalid MTU setting\n");
413                 return -EINVAL;
414         }
415         /* set MTU */
416         if (old_mtu != new_mtu && netif_running(netdev)) {
417                 while (test_and_set_bit(__AT_RESETTING, &adapter->flags))
418                         msleep(1);
419                 netdev->mtu = new_mtu;
420                 adapter->hw.max_frame_size = new_mtu;
421                 adapter->hw.rx_jumbo_th = (max_frame + 7) >> 3;
422                 atl1e_down(adapter);
423                 atl1e_up(adapter);
424                 clear_bit(__AT_RESETTING, &adapter->flags);
425         }
426         return 0;
427 }
428
429 /*
430  *  caller should hold mdio_lock
431  */
432 static int atl1e_mdio_read(struct net_device *netdev, int phy_id, int reg_num)
433 {
434         struct atl1e_adapter *adapter = netdev_priv(netdev);
435         u16 result;
436
437         atl1e_read_phy_reg(&adapter->hw, reg_num & MDIO_REG_ADDR_MASK, &result);
438         return result;
439 }
440
441 static void atl1e_mdio_write(struct net_device *netdev, int phy_id,
442                              int reg_num, int val)
443 {
444         struct atl1e_adapter *adapter = netdev_priv(netdev);
445
446         atl1e_write_phy_reg(&adapter->hw, reg_num & MDIO_REG_ADDR_MASK, val);
447 }
448
449 /*
450  * atl1e_mii_ioctl -
451  * @netdev:
452  * @ifreq:
453  * @cmd:
454  */
455 static int atl1e_mii_ioctl(struct net_device *netdev,
456                            struct ifreq *ifr, int cmd)
457 {
458         struct atl1e_adapter *adapter = netdev_priv(netdev);
459         struct mii_ioctl_data *data = if_mii(ifr);
460         unsigned long flags;
461         int retval = 0;
462
463         if (!netif_running(netdev))
464                 return -EINVAL;
465
466         spin_lock_irqsave(&adapter->mdio_lock, flags);
467         switch (cmd) {
468         case SIOCGMIIPHY:
469                 data->phy_id = 0;
470                 break;
471
472         case SIOCGMIIREG:
473                 if (atl1e_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
474                                     &data->val_out)) {
475                         retval = -EIO;
476                         goto out;
477                 }
478                 break;
479
480         case SIOCSMIIREG:
481                 if (data->reg_num & ~(0x1F)) {
482                         retval = -EFAULT;
483                         goto out;
484                 }
485
486                 netdev_dbg(adapter->netdev, "<atl1e_mii_ioctl> write %x %x\n",
487                            data->reg_num, data->val_in);
488                 if (atl1e_write_phy_reg(&adapter->hw,
489                                      data->reg_num, data->val_in)) {
490                         retval = -EIO;
491                         goto out;
492                 }
493                 break;
494
495         default:
496                 retval = -EOPNOTSUPP;
497                 break;
498         }
499 out:
500         spin_unlock_irqrestore(&adapter->mdio_lock, flags);
501         return retval;
502
503 }
504
505 /*
506  * atl1e_ioctl -
507  * @netdev:
508  * @ifreq:
509  * @cmd:
510  */
511 static int atl1e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
512 {
513         switch (cmd) {
514         case SIOCGMIIPHY:
515         case SIOCGMIIREG:
516         case SIOCSMIIREG:
517                 return atl1e_mii_ioctl(netdev, ifr, cmd);
518         default:
519                 return -EOPNOTSUPP;
520         }
521 }
522
523 static void atl1e_setup_pcicmd(struct pci_dev *pdev)
524 {
525         u16 cmd;
526
527         pci_read_config_word(pdev, PCI_COMMAND, &cmd);
528         cmd &= ~(PCI_COMMAND_INTX_DISABLE | PCI_COMMAND_IO);
529         cmd |=  (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
530         pci_write_config_word(pdev, PCI_COMMAND, cmd);
531
532         /*
533          * some motherboards BIOS(PXE/EFI) driver may set PME
534          * while they transfer control to OS (Windows/Linux)
535          * so we should clear this bit before NIC work normally
536          */
537         pci_write_config_dword(pdev, REG_PM_CTRLSTAT, 0);
538         msleep(1);
539 }
540
541 /*
542  * atl1e_alloc_queues - Allocate memory for all rings
543  * @adapter: board private structure to initialize
544  *
545  */
546 static int __devinit atl1e_alloc_queues(struct atl1e_adapter *adapter)
547 {
548         return 0;
549 }
550
551 /*
552  * atl1e_sw_init - Initialize general software structures (struct atl1e_adapter)
553  * @adapter: board private structure to initialize
554  *
555  * atl1e_sw_init initializes the Adapter private data structure.
556  * Fields are initialized based on PCI device information and
557  * OS network device settings (MTU size).
558  */
559 static int __devinit atl1e_sw_init(struct atl1e_adapter *adapter)
560 {
561         struct atl1e_hw *hw   = &adapter->hw;
562         struct pci_dev  *pdev = adapter->pdev;
563         u32 phy_status_data = 0;
564
565         adapter->wol = 0;
566         adapter->link_speed = SPEED_0;   /* hardware init */
567         adapter->link_duplex = FULL_DUPLEX;
568         adapter->num_rx_queues = 1;
569
570         /* PCI config space info */
571         hw->vendor_id = pdev->vendor;
572         hw->device_id = pdev->device;
573         hw->subsystem_vendor_id = pdev->subsystem_vendor;
574         hw->subsystem_id = pdev->subsystem_device;
575         hw->revision_id  = pdev->revision;
576
577         pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
578
579         phy_status_data = AT_READ_REG(hw, REG_PHY_STATUS);
580         /* nic type */
581         if (hw->revision_id >= 0xF0) {
582                 hw->nic_type = athr_l2e_revB;
583         } else {
584                 if (phy_status_data & PHY_STATUS_100M)
585                         hw->nic_type = athr_l1e;
586                 else
587                         hw->nic_type = athr_l2e_revA;
588         }
589
590         phy_status_data = AT_READ_REG(hw, REG_PHY_STATUS);
591
592         if (phy_status_data & PHY_STATUS_EMI_CA)
593                 hw->emi_ca = true;
594         else
595                 hw->emi_ca = false;
596
597         hw->phy_configured = false;
598         hw->preamble_len = 7;
599         hw->max_frame_size = adapter->netdev->mtu;
600         hw->rx_jumbo_th = (hw->max_frame_size + ETH_HLEN +
601                                 VLAN_HLEN + ETH_FCS_LEN + 7) >> 3;
602
603         hw->rrs_type = atl1e_rrs_disable;
604         hw->indirect_tab = 0;
605         hw->base_cpu = 0;
606
607         /* need confirm */
608
609         hw->ict = 50000;                 /* 100ms */
610         hw->smb_timer = 200000;          /* 200ms  */
611         hw->tpd_burst = 5;
612         hw->rrd_thresh = 1;
613         hw->tpd_thresh = adapter->tx_ring.count / 2;
614         hw->rx_count_down = 4;  /* 2us resolution */
615         hw->tx_count_down = hw->imt * 4 / 3;
616         hw->dmar_block = atl1e_dma_req_1024;
617         hw->dmaw_block = atl1e_dma_req_1024;
618         hw->dmar_dly_cnt = 15;
619         hw->dmaw_dly_cnt = 4;
620
621         if (atl1e_alloc_queues(adapter)) {
622                 netdev_err(adapter->netdev, "Unable to allocate memory for queues\n");
623                 return -ENOMEM;
624         }
625
626         atomic_set(&adapter->irq_sem, 1);
627         spin_lock_init(&adapter->mdio_lock);
628         spin_lock_init(&adapter->tx_lock);
629
630         set_bit(__AT_DOWN, &adapter->flags);
631
632         return 0;
633 }
634
635 /*
636  * atl1e_clean_tx_ring - Free Tx-skb
637  * @adapter: board private structure
638  */
639 static void atl1e_clean_tx_ring(struct atl1e_adapter *adapter)
640 {
641         struct atl1e_tx_ring *tx_ring = (struct atl1e_tx_ring *)
642                                 &adapter->tx_ring;
643         struct atl1e_tx_buffer *tx_buffer = NULL;
644         struct pci_dev *pdev = adapter->pdev;
645         u16 index, ring_count;
646
647         if (tx_ring->desc == NULL || tx_ring->tx_buffer == NULL)
648                 return;
649
650         ring_count = tx_ring->count;
651         /* first unmmap dma */
652         for (index = 0; index < ring_count; index++) {
653                 tx_buffer = &tx_ring->tx_buffer[index];
654                 if (tx_buffer->dma) {
655                         if (tx_buffer->flags & ATL1E_TX_PCIMAP_SINGLE)
656                                 pci_unmap_single(pdev, tx_buffer->dma,
657                                         tx_buffer->length, PCI_DMA_TODEVICE);
658                         else if (tx_buffer->flags & ATL1E_TX_PCIMAP_PAGE)
659                                 pci_unmap_page(pdev, tx_buffer->dma,
660                                         tx_buffer->length, PCI_DMA_TODEVICE);
661                         tx_buffer->dma = 0;
662                 }
663         }
664         /* second free skb */
665         for (index = 0; index < ring_count; index++) {
666                 tx_buffer = &tx_ring->tx_buffer[index];
667                 if (tx_buffer->skb) {
668                         dev_kfree_skb_any(tx_buffer->skb);
669                         tx_buffer->skb = NULL;
670                 }
671         }
672         /* Zero out Tx-buffers */
673         memset(tx_ring->desc, 0, sizeof(struct atl1e_tpd_desc) *
674                                 ring_count);
675         memset(tx_ring->tx_buffer, 0, sizeof(struct atl1e_tx_buffer) *
676                                 ring_count);
677 }
678
679 /*
680  * atl1e_clean_rx_ring - Free rx-reservation skbs
681  * @adapter: board private structure
682  */
683 static void atl1e_clean_rx_ring(struct atl1e_adapter *adapter)
684 {
685         struct atl1e_rx_ring *rx_ring =
686                 (struct atl1e_rx_ring *)&adapter->rx_ring;
687         struct atl1e_rx_page_desc *rx_page_desc = rx_ring->rx_page_desc;
688         u16 i, j;
689
690
691         if (adapter->ring_vir_addr == NULL)
692                 return;
693         /* Zero out the descriptor ring */
694         for (i = 0; i < adapter->num_rx_queues; i++) {
695                 for (j = 0; j < AT_PAGE_NUM_PER_QUEUE; j++) {
696                         if (rx_page_desc[i].rx_page[j].addr != NULL) {
697                                 memset(rx_page_desc[i].rx_page[j].addr, 0,
698                                                 rx_ring->real_page_size);
699                         }
700                 }
701         }
702 }
703
704 static void atl1e_cal_ring_size(struct atl1e_adapter *adapter, u32 *ring_size)
705 {
706         *ring_size = ((u32)(adapter->tx_ring.count *
707                      sizeof(struct atl1e_tpd_desc) + 7
708                         /* tx ring, qword align */
709                      + adapter->rx_ring.real_page_size * AT_PAGE_NUM_PER_QUEUE *
710                         adapter->num_rx_queues + 31
711                         /* rx ring,  32 bytes align */
712                      + (1 + AT_PAGE_NUM_PER_QUEUE * adapter->num_rx_queues) *
713                         sizeof(u32) + 3));
714                         /* tx, rx cmd, dword align   */
715 }
716
717 static void atl1e_init_ring_resources(struct atl1e_adapter *adapter)
718 {
719         struct atl1e_rx_ring *rx_ring = NULL;
720
721         rx_ring = &adapter->rx_ring;
722
723         rx_ring->real_page_size = adapter->rx_ring.page_size
724                                  + adapter->hw.max_frame_size
725                                  + ETH_HLEN + VLAN_HLEN
726                                  + ETH_FCS_LEN;
727         rx_ring->real_page_size = roundup(rx_ring->real_page_size, 32);
728         atl1e_cal_ring_size(adapter, &adapter->ring_size);
729
730         adapter->ring_vir_addr = NULL;
731         adapter->rx_ring.desc = NULL;
732         rwlock_init(&adapter->tx_ring.tx_lock);
733 }
734
735 /*
736  * Read / Write Ptr Initialize:
737  */
738 static void atl1e_init_ring_ptrs(struct atl1e_adapter *adapter)
739 {
740         struct atl1e_tx_ring *tx_ring = NULL;
741         struct atl1e_rx_ring *rx_ring = NULL;
742         struct atl1e_rx_page_desc *rx_page_desc = NULL;
743         int i, j;
744
745         tx_ring = &adapter->tx_ring;
746         rx_ring = &adapter->rx_ring;
747         rx_page_desc = rx_ring->rx_page_desc;
748
749         tx_ring->next_to_use = 0;
750         atomic_set(&tx_ring->next_to_clean, 0);
751
752         for (i = 0; i < adapter->num_rx_queues; i++) {
753                 rx_page_desc[i].rx_using  = 0;
754                 rx_page_desc[i].rx_nxseq = 0;
755                 for (j = 0; j < AT_PAGE_NUM_PER_QUEUE; j++) {
756                         *rx_page_desc[i].rx_page[j].write_offset_addr = 0;
757                         rx_page_desc[i].rx_page[j].read_offset = 0;
758                 }
759         }
760 }
761
762 /*
763  * atl1e_free_ring_resources - Free Tx / RX descriptor Resources
764  * @adapter: board private structure
765  *
766  * Free all transmit software resources
767  */
768 static void atl1e_free_ring_resources(struct atl1e_adapter *adapter)
769 {
770         struct pci_dev *pdev = adapter->pdev;
771
772         atl1e_clean_tx_ring(adapter);
773         atl1e_clean_rx_ring(adapter);
774
775         if (adapter->ring_vir_addr) {
776                 pci_free_consistent(pdev, adapter->ring_size,
777                                 adapter->ring_vir_addr, adapter->ring_dma);
778                 adapter->ring_vir_addr = NULL;
779         }
780
781         if (adapter->tx_ring.tx_buffer) {
782                 kfree(adapter->tx_ring.tx_buffer);
783                 adapter->tx_ring.tx_buffer = NULL;
784         }
785 }
786
787 /*
788  * atl1e_setup_mem_resources - allocate Tx / RX descriptor resources
789  * @adapter: board private structure
790  *
791  * Return 0 on success, negative on failure
792  */
793 static int atl1e_setup_ring_resources(struct atl1e_adapter *adapter)
794 {
795         struct pci_dev *pdev = adapter->pdev;
796         struct atl1e_tx_ring *tx_ring;
797         struct atl1e_rx_ring *rx_ring;
798         struct atl1e_rx_page_desc  *rx_page_desc;
799         int size, i, j;
800         u32 offset = 0;
801         int err = 0;
802
803         if (adapter->ring_vir_addr != NULL)
804                 return 0; /* alloced already */
805
806         tx_ring = &adapter->tx_ring;
807         rx_ring = &adapter->rx_ring;
808
809         /* real ring DMA buffer */
810
811         size = adapter->ring_size;
812         adapter->ring_vir_addr = pci_alloc_consistent(pdev,
813                         adapter->ring_size, &adapter->ring_dma);
814
815         if (adapter->ring_vir_addr == NULL) {
816                 netdev_err(adapter->netdev,
817                            "pci_alloc_consistent failed, size = D%d\n", size);
818                 return -ENOMEM;
819         }
820
821         memset(adapter->ring_vir_addr, 0, adapter->ring_size);
822
823         rx_page_desc = rx_ring->rx_page_desc;
824
825         /* Init TPD Ring */
826         tx_ring->dma = roundup(adapter->ring_dma, 8);
827         offset = tx_ring->dma - adapter->ring_dma;
828         tx_ring->desc = adapter->ring_vir_addr + offset;
829         size = sizeof(struct atl1e_tx_buffer) * (tx_ring->count);
830         tx_ring->tx_buffer = kzalloc(size, GFP_KERNEL);
831         if (tx_ring->tx_buffer == NULL) {
832                 netdev_err(adapter->netdev, "kzalloc failed, size = D%d\n",
833                            size);
834                 err = -ENOMEM;
835                 goto failed;
836         }
837
838         /* Init RXF-Pages */
839         offset += (sizeof(struct atl1e_tpd_desc) * tx_ring->count);
840         offset = roundup(offset, 32);
841
842         for (i = 0; i < adapter->num_rx_queues; i++) {
843                 for (j = 0; j < AT_PAGE_NUM_PER_QUEUE; j++) {
844                         rx_page_desc[i].rx_page[j].dma =
845                                 adapter->ring_dma + offset;
846                         rx_page_desc[i].rx_page[j].addr =
847                                 adapter->ring_vir_addr + offset;
848                         offset += rx_ring->real_page_size;
849                 }
850         }
851
852         /* Init CMB dma address */
853         tx_ring->cmb_dma = adapter->ring_dma + offset;
854         tx_ring->cmb = adapter->ring_vir_addr + offset;
855         offset += sizeof(u32);
856
857         for (i = 0; i < adapter->num_rx_queues; i++) {
858                 for (j = 0; j < AT_PAGE_NUM_PER_QUEUE; j++) {
859                         rx_page_desc[i].rx_page[j].write_offset_dma =
860                                 adapter->ring_dma + offset;
861                         rx_page_desc[i].rx_page[j].write_offset_addr =
862                                 adapter->ring_vir_addr + offset;
863                         offset += sizeof(u32);
864                 }
865         }
866
867         if (unlikely(offset > adapter->ring_size)) {
868                 netdev_err(adapter->netdev, "offset(%d) > ring size(%d) !!\n",
869                            offset, adapter->ring_size);
870                 err = -1;
871                 goto failed;
872         }
873
874         return 0;
875 failed:
876         if (adapter->ring_vir_addr != NULL) {
877                 pci_free_consistent(pdev, adapter->ring_size,
878                                 adapter->ring_vir_addr, adapter->ring_dma);
879                 adapter->ring_vir_addr = NULL;
880         }
881         return err;
882 }
883
884 static inline void atl1e_configure_des_ring(const struct atl1e_adapter *adapter)
885 {
886
887         struct atl1e_hw *hw = (struct atl1e_hw *)&adapter->hw;
888         struct atl1e_rx_ring *rx_ring =
889                         (struct atl1e_rx_ring *)&adapter->rx_ring;
890         struct atl1e_tx_ring *tx_ring =
891                         (struct atl1e_tx_ring *)&adapter->tx_ring;
892         struct atl1e_rx_page_desc *rx_page_desc = NULL;
893         int i, j;
894
895         AT_WRITE_REG(hw, REG_DESC_BASE_ADDR_HI,
896                         (u32)((adapter->ring_dma & AT_DMA_HI_ADDR_MASK) >> 32));
897         AT_WRITE_REG(hw, REG_TPD_BASE_ADDR_LO,
898                         (u32)((tx_ring->dma) & AT_DMA_LO_ADDR_MASK));
899         AT_WRITE_REG(hw, REG_TPD_RING_SIZE, (u16)(tx_ring->count));
900         AT_WRITE_REG(hw, REG_HOST_TX_CMB_LO,
901                         (u32)((tx_ring->cmb_dma) & AT_DMA_LO_ADDR_MASK));
902
903         rx_page_desc = rx_ring->rx_page_desc;
904         /* RXF Page Physical address / Page Length */
905         for (i = 0; i < AT_MAX_RECEIVE_QUEUE; i++) {
906                 AT_WRITE_REG(hw, atl1e_rx_page_hi_addr_regs[i],
907                                  (u32)((adapter->ring_dma &
908                                  AT_DMA_HI_ADDR_MASK) >> 32));
909                 for (j = 0; j < AT_PAGE_NUM_PER_QUEUE; j++) {
910                         u32 page_phy_addr;
911                         u32 offset_phy_addr;
912
913                         page_phy_addr = rx_page_desc[i].rx_page[j].dma;
914                         offset_phy_addr =
915                                    rx_page_desc[i].rx_page[j].write_offset_dma;
916
917                         AT_WRITE_REG(hw, atl1e_rx_page_lo_addr_regs[i][j],
918                                         page_phy_addr & AT_DMA_LO_ADDR_MASK);
919                         AT_WRITE_REG(hw, atl1e_rx_page_write_offset_regs[i][j],
920                                         offset_phy_addr & AT_DMA_LO_ADDR_MASK);
921                         AT_WRITE_REGB(hw, atl1e_rx_page_vld_regs[i][j], 1);
922                 }
923         }
924         /* Page Length */
925         AT_WRITE_REG(hw, REG_HOST_RXFPAGE_SIZE, rx_ring->page_size);
926         /* Load all of base address above */
927         AT_WRITE_REG(hw, REG_LOAD_PTR, 1);
928 }
929
930 static inline void atl1e_configure_tx(struct atl1e_adapter *adapter)
931 {
932         struct atl1e_hw *hw = (struct atl1e_hw *)&adapter->hw;
933         u32 dev_ctrl_data = 0;
934         u32 max_pay_load = 0;
935         u32 jumbo_thresh = 0;
936         u32 extra_size = 0;     /* Jumbo frame threshold in QWORD unit */
937
938         /* configure TXQ param */
939         if (hw->nic_type != athr_l2e_revB) {
940                 extra_size = ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN;
941                 if (hw->max_frame_size <= 1500) {
942                         jumbo_thresh = hw->max_frame_size + extra_size;
943                 } else if (hw->max_frame_size < 6*1024) {
944                         jumbo_thresh =
945                                 (hw->max_frame_size + extra_size) * 2 / 3;
946                 } else {
947                         jumbo_thresh = (hw->max_frame_size + extra_size) / 2;
948                 }
949                 AT_WRITE_REG(hw, REG_TX_EARLY_TH, (jumbo_thresh + 7) >> 3);
950         }
951
952         dev_ctrl_data = AT_READ_REG(hw, REG_DEVICE_CTRL);
953
954         max_pay_load  = ((dev_ctrl_data >> DEVICE_CTRL_MAX_PAYLOAD_SHIFT)) &
955                         DEVICE_CTRL_MAX_PAYLOAD_MASK;
956
957         hw->dmaw_block = min_t(u32, max_pay_load, hw->dmaw_block);
958
959         max_pay_load  = ((dev_ctrl_data >> DEVICE_CTRL_MAX_RREQ_SZ_SHIFT)) &
960                         DEVICE_CTRL_MAX_RREQ_SZ_MASK;
961         hw->dmar_block = min_t(u32, max_pay_load, hw->dmar_block);
962
963         if (hw->nic_type != athr_l2e_revB)
964                 AT_WRITE_REGW(hw, REG_TXQ_CTRL + 2,
965                               atl1e_pay_load_size[hw->dmar_block]);
966         /* enable TXQ */
967         AT_WRITE_REGW(hw, REG_TXQ_CTRL,
968                         (((u16)hw->tpd_burst & TXQ_CTRL_NUM_TPD_BURST_MASK)
969                          << TXQ_CTRL_NUM_TPD_BURST_SHIFT)
970                         | TXQ_CTRL_ENH_MODE | TXQ_CTRL_EN);
971 }
972
973 static inline void atl1e_configure_rx(struct atl1e_adapter *adapter)
974 {
975         struct atl1e_hw *hw = (struct atl1e_hw *)&adapter->hw;
976         u32 rxf_len  = 0;
977         u32 rxf_low  = 0;
978         u32 rxf_high = 0;
979         u32 rxf_thresh_data = 0;
980         u32 rxq_ctrl_data = 0;
981
982         if (hw->nic_type != athr_l2e_revB) {
983                 AT_WRITE_REGW(hw, REG_RXQ_JMBOSZ_RRDTIM,
984                               (u16)((hw->rx_jumbo_th & RXQ_JMBOSZ_TH_MASK) <<
985                               RXQ_JMBOSZ_TH_SHIFT |
986                               (1 & RXQ_JMBO_LKAH_MASK) <<
987                               RXQ_JMBO_LKAH_SHIFT));
988
989                 rxf_len  = AT_READ_REG(hw, REG_SRAM_RXF_LEN);
990                 rxf_high = rxf_len * 4 / 5;
991                 rxf_low  = rxf_len / 5;
992                 rxf_thresh_data = ((rxf_high  & RXQ_RXF_PAUSE_TH_HI_MASK)
993                                   << RXQ_RXF_PAUSE_TH_HI_SHIFT) |
994                                   ((rxf_low & RXQ_RXF_PAUSE_TH_LO_MASK)
995                                   << RXQ_RXF_PAUSE_TH_LO_SHIFT);
996
997                 AT_WRITE_REG(hw, REG_RXQ_RXF_PAUSE_THRESH, rxf_thresh_data);
998         }
999
1000         /* RRS */
1001         AT_WRITE_REG(hw, REG_IDT_TABLE, hw->indirect_tab);
1002         AT_WRITE_REG(hw, REG_BASE_CPU_NUMBER, hw->base_cpu);
1003
1004         if (hw->rrs_type & atl1e_rrs_ipv4)
1005                 rxq_ctrl_data |= RXQ_CTRL_HASH_TYPE_IPV4;
1006
1007         if (hw->rrs_type & atl1e_rrs_ipv4_tcp)
1008                 rxq_ctrl_data |= RXQ_CTRL_HASH_TYPE_IPV4_TCP;
1009
1010         if (hw->rrs_type & atl1e_rrs_ipv6)
1011                 rxq_ctrl_data |= RXQ_CTRL_HASH_TYPE_IPV6;
1012
1013         if (hw->rrs_type & atl1e_rrs_ipv6_tcp)
1014                 rxq_ctrl_data |= RXQ_CTRL_HASH_TYPE_IPV6_TCP;
1015
1016         if (hw->rrs_type != atl1e_rrs_disable)
1017                 rxq_ctrl_data |=
1018                         (RXQ_CTRL_HASH_ENABLE | RXQ_CTRL_RSS_MODE_MQUESINT);
1019
1020         rxq_ctrl_data |= RXQ_CTRL_IPV6_XSUM_VERIFY_EN | RXQ_CTRL_PBA_ALIGN_32 |
1021                          RXQ_CTRL_CUT_THRU_EN | RXQ_CTRL_EN;
1022
1023         AT_WRITE_REG(hw, REG_RXQ_CTRL, rxq_ctrl_data);
1024 }
1025
1026 static inline void atl1e_configure_dma(struct atl1e_adapter *adapter)
1027 {
1028         struct atl1e_hw *hw = &adapter->hw;
1029         u32 dma_ctrl_data = 0;
1030
1031         dma_ctrl_data = DMA_CTRL_RXCMB_EN;
1032         dma_ctrl_data |= (((u32)hw->dmar_block) & DMA_CTRL_DMAR_BURST_LEN_MASK)
1033                 << DMA_CTRL_DMAR_BURST_LEN_SHIFT;
1034         dma_ctrl_data |= (((u32)hw->dmaw_block) & DMA_CTRL_DMAW_BURST_LEN_MASK)
1035                 << DMA_CTRL_DMAW_BURST_LEN_SHIFT;
1036         dma_ctrl_data |= DMA_CTRL_DMAR_REQ_PRI | DMA_CTRL_DMAR_OUT_ORDER;
1037         dma_ctrl_data |= (((u32)hw->dmar_dly_cnt) & DMA_CTRL_DMAR_DLY_CNT_MASK)
1038                 << DMA_CTRL_DMAR_DLY_CNT_SHIFT;
1039         dma_ctrl_data |= (((u32)hw->dmaw_dly_cnt) & DMA_CTRL_DMAW_DLY_CNT_MASK)
1040                 << DMA_CTRL_DMAW_DLY_CNT_SHIFT;
1041
1042         AT_WRITE_REG(hw, REG_DMA_CTRL, dma_ctrl_data);
1043 }
1044
1045 static void atl1e_setup_mac_ctrl(struct atl1e_adapter *adapter)
1046 {
1047         u32 value;
1048         struct atl1e_hw *hw = &adapter->hw;
1049         struct net_device *netdev = adapter->netdev;
1050
1051         /* Config MAC CTRL Register */
1052         value = MAC_CTRL_TX_EN |
1053                 MAC_CTRL_RX_EN ;
1054
1055         if (FULL_DUPLEX == adapter->link_duplex)
1056                 value |= MAC_CTRL_DUPLX;
1057
1058         value |= ((u32)((SPEED_1000 == adapter->link_speed) ?
1059                           MAC_CTRL_SPEED_1000 : MAC_CTRL_SPEED_10_100) <<
1060                           MAC_CTRL_SPEED_SHIFT);
1061         value |= (MAC_CTRL_TX_FLOW | MAC_CTRL_RX_FLOW);
1062
1063         value |= (MAC_CTRL_ADD_CRC | MAC_CTRL_PAD);
1064         value |= (((u32)adapter->hw.preamble_len &
1065                   MAC_CTRL_PRMLEN_MASK) << MAC_CTRL_PRMLEN_SHIFT);
1066
1067         __atl1e_vlan_mode(netdev->features, &value);
1068
1069         value |= MAC_CTRL_BC_EN;
1070         if (netdev->flags & IFF_PROMISC)
1071                 value |= MAC_CTRL_PROMIS_EN;
1072         if (netdev->flags & IFF_ALLMULTI)
1073                 value |= MAC_CTRL_MC_ALL_EN;
1074
1075         AT_WRITE_REG(hw, REG_MAC_CTRL, value);
1076 }
1077
1078 /*
1079  * atl1e_configure - Configure Transmit&Receive Unit after Reset
1080  * @adapter: board private structure
1081  *
1082  * Configure the Tx /Rx unit of the MAC after a reset.
1083  */
1084 static int atl1e_configure(struct atl1e_adapter *adapter)
1085 {
1086         struct atl1e_hw *hw = &adapter->hw;
1087
1088         u32 intr_status_data = 0;
1089
1090         /* clear interrupt status */
1091         AT_WRITE_REG(hw, REG_ISR, ~0);
1092
1093         /* 1. set MAC Address */
1094         atl1e_hw_set_mac_addr(hw);
1095
1096         /* 2. Init the Multicast HASH table done by set_muti */
1097
1098         /* 3. Clear any WOL status */
1099         AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
1100
1101         /* 4. Descripter Ring BaseMem/Length/Read ptr/Write ptr
1102          *    TPD Ring/SMB/RXF0 Page CMBs, they use the same
1103          *    High 32bits memory */
1104         atl1e_configure_des_ring(adapter);
1105
1106         /* 5. set Interrupt Moderator Timer */
1107         AT_WRITE_REGW(hw, REG_IRQ_MODU_TIMER_INIT, hw->imt);
1108         AT_WRITE_REGW(hw, REG_IRQ_MODU_TIMER2_INIT, hw->imt);
1109         AT_WRITE_REG(hw, REG_MASTER_CTRL, MASTER_CTRL_LED_MODE |
1110                         MASTER_CTRL_ITIMER_EN | MASTER_CTRL_ITIMER2_EN);
1111
1112         /* 6. rx/tx threshold to trig interrupt */
1113         AT_WRITE_REGW(hw, REG_TRIG_RRD_THRESH, hw->rrd_thresh);
1114         AT_WRITE_REGW(hw, REG_TRIG_TPD_THRESH, hw->tpd_thresh);
1115         AT_WRITE_REGW(hw, REG_TRIG_RXTIMER, hw->rx_count_down);
1116         AT_WRITE_REGW(hw, REG_TRIG_TXTIMER, hw->tx_count_down);
1117
1118         /* 7. set Interrupt Clear Timer */
1119         AT_WRITE_REGW(hw, REG_CMBDISDMA_TIMER, hw->ict);
1120
1121         /* 8. set MTU */
1122         AT_WRITE_REG(hw, REG_MTU, hw->max_frame_size + ETH_HLEN +
1123                         VLAN_HLEN + ETH_FCS_LEN);
1124
1125         /* 9. config TXQ early tx threshold */
1126         atl1e_configure_tx(adapter);
1127
1128         /* 10. config RXQ */
1129         atl1e_configure_rx(adapter);
1130
1131         /* 11. config  DMA Engine */
1132         atl1e_configure_dma(adapter);
1133
1134         /* 12. smb timer to trig interrupt */
1135         AT_WRITE_REG(hw, REG_SMB_STAT_TIMER, hw->smb_timer);
1136
1137         intr_status_data = AT_READ_REG(hw, REG_ISR);
1138         if (unlikely((intr_status_data & ISR_PHY_LINKDOWN) != 0)) {
1139                 netdev_err(adapter->netdev,
1140                            "atl1e_configure failed, PCIE phy link down\n");
1141                 return -1;
1142         }
1143
1144         AT_WRITE_REG(hw, REG_ISR, 0x7fffffff);
1145         return 0;
1146 }
1147
1148 /*
1149  * atl1e_get_stats - Get System Network Statistics
1150  * @netdev: network interface device structure
1151  *
1152  * Returns the address of the device statistics structure.
1153  * The statistics are actually updated from the timer callback.
1154  */
1155 static struct net_device_stats *atl1e_get_stats(struct net_device *netdev)
1156 {
1157         struct atl1e_adapter *adapter = netdev_priv(netdev);
1158         struct atl1e_hw_stats  *hw_stats = &adapter->hw_stats;
1159         struct net_device_stats *net_stats = &netdev->stats;
1160
1161         net_stats->rx_packets = hw_stats->rx_ok;
1162         net_stats->tx_packets = hw_stats->tx_ok;
1163         net_stats->rx_bytes   = hw_stats->rx_byte_cnt;
1164         net_stats->tx_bytes   = hw_stats->tx_byte_cnt;
1165         net_stats->multicast  = hw_stats->rx_mcast;
1166         net_stats->collisions = hw_stats->tx_1_col +
1167                                 hw_stats->tx_2_col * 2 +
1168                                 hw_stats->tx_late_col + hw_stats->tx_abort_col;
1169
1170         net_stats->rx_errors  = hw_stats->rx_frag + hw_stats->rx_fcs_err +
1171                                 hw_stats->rx_len_err + hw_stats->rx_sz_ov +
1172                                 hw_stats->rx_rrd_ov + hw_stats->rx_align_err;
1173         net_stats->rx_fifo_errors   = hw_stats->rx_rxf_ov;
1174         net_stats->rx_length_errors = hw_stats->rx_len_err;
1175         net_stats->rx_crc_errors    = hw_stats->rx_fcs_err;
1176         net_stats->rx_frame_errors  = hw_stats->rx_align_err;
1177         net_stats->rx_over_errors   = hw_stats->rx_rrd_ov + hw_stats->rx_rxf_ov;
1178
1179         net_stats->rx_missed_errors = hw_stats->rx_rrd_ov + hw_stats->rx_rxf_ov;
1180
1181         net_stats->tx_errors = hw_stats->tx_late_col + hw_stats->tx_abort_col +
1182                                hw_stats->tx_underrun + hw_stats->tx_trunc;
1183         net_stats->tx_fifo_errors    = hw_stats->tx_underrun;
1184         net_stats->tx_aborted_errors = hw_stats->tx_abort_col;
1185         net_stats->tx_window_errors  = hw_stats->tx_late_col;
1186
1187         return net_stats;
1188 }
1189
1190 static void atl1e_update_hw_stats(struct atl1e_adapter *adapter)
1191 {
1192         u16 hw_reg_addr = 0;
1193         unsigned long *stats_item = NULL;
1194
1195         /* update rx status */
1196         hw_reg_addr = REG_MAC_RX_STATUS_BIN;
1197         stats_item  = &adapter->hw_stats.rx_ok;
1198         while (hw_reg_addr <= REG_MAC_RX_STATUS_END) {
1199                 *stats_item += AT_READ_REG(&adapter->hw, hw_reg_addr);
1200                 stats_item++;
1201                 hw_reg_addr += 4;
1202         }
1203         /* update tx status */
1204         hw_reg_addr = REG_MAC_TX_STATUS_BIN;
1205         stats_item  = &adapter->hw_stats.tx_ok;
1206         while (hw_reg_addr <= REG_MAC_TX_STATUS_END) {
1207                 *stats_item += AT_READ_REG(&adapter->hw, hw_reg_addr);
1208                 stats_item++;
1209                 hw_reg_addr += 4;
1210         }
1211 }
1212
1213 static inline void atl1e_clear_phy_int(struct atl1e_adapter *adapter)
1214 {
1215         u16 phy_data;
1216
1217         spin_lock(&adapter->mdio_lock);
1218         atl1e_read_phy_reg(&adapter->hw, MII_INT_STATUS, &phy_data);
1219         spin_unlock(&adapter->mdio_lock);
1220 }
1221
1222 static bool atl1e_clean_tx_irq(struct atl1e_adapter *adapter)
1223 {
1224         struct atl1e_tx_ring *tx_ring = (struct atl1e_tx_ring *)
1225                                         &adapter->tx_ring;
1226         struct atl1e_tx_buffer *tx_buffer = NULL;
1227         u16 hw_next_to_clean = AT_READ_REGW(&adapter->hw, REG_TPD_CONS_IDX);
1228         u16 next_to_clean = atomic_read(&tx_ring->next_to_clean);
1229
1230         while (next_to_clean != hw_next_to_clean) {
1231                 tx_buffer = &tx_ring->tx_buffer[next_to_clean];
1232                 if (tx_buffer->dma) {
1233                         if (tx_buffer->flags & ATL1E_TX_PCIMAP_SINGLE)
1234                                 pci_unmap_single(adapter->pdev, tx_buffer->dma,
1235                                         tx_buffer->length, PCI_DMA_TODEVICE);
1236                         else if (tx_buffer->flags & ATL1E_TX_PCIMAP_PAGE)
1237                                 pci_unmap_page(adapter->pdev, tx_buffer->dma,
1238                                         tx_buffer->length, PCI_DMA_TODEVICE);
1239                         tx_buffer->dma = 0;
1240                 }
1241
1242                 if (tx_buffer->skb) {
1243                         dev_kfree_skb_irq(tx_buffer->skb);
1244                         tx_buffer->skb = NULL;
1245                 }
1246
1247                 if (++next_to_clean == tx_ring->count)
1248                         next_to_clean = 0;
1249         }
1250
1251         atomic_set(&tx_ring->next_to_clean, next_to_clean);
1252
1253         if (netif_queue_stopped(adapter->netdev) &&
1254                         netif_carrier_ok(adapter->netdev)) {
1255                 netif_wake_queue(adapter->netdev);
1256         }
1257
1258         return true;
1259 }
1260
1261 /*
1262  * atl1e_intr - Interrupt Handler
1263  * @irq: interrupt number
1264  * @data: pointer to a network interface device structure
1265  * @pt_regs: CPU registers structure
1266  */
1267 static irqreturn_t atl1e_intr(int irq, void *data)
1268 {
1269         struct net_device *netdev  = data;
1270         struct atl1e_adapter *adapter = netdev_priv(netdev);
1271         struct atl1e_hw *hw = &adapter->hw;
1272         int max_ints = AT_MAX_INT_WORK;
1273         int handled = IRQ_NONE;
1274         u32 status;
1275
1276         do {
1277                 status = AT_READ_REG(hw, REG_ISR);
1278                 if ((status & IMR_NORMAL_MASK) == 0 ||
1279                                 (status & ISR_DIS_INT) != 0) {
1280                         if (max_ints != AT_MAX_INT_WORK)
1281                                 handled = IRQ_HANDLED;
1282                         break;
1283                 }
1284                 /* link event */
1285                 if (status & ISR_GPHY)
1286                         atl1e_clear_phy_int(adapter);
1287                 /* Ack ISR */
1288                 AT_WRITE_REG(hw, REG_ISR, status | ISR_DIS_INT);
1289
1290                 handled = IRQ_HANDLED;
1291                 /* check if PCIE PHY Link down */
1292                 if (status & ISR_PHY_LINKDOWN) {
1293                         netdev_err(adapter->netdev,
1294                                    "pcie phy linkdown %x\n", status);
1295                         if (netif_running(adapter->netdev)) {
1296                                 /* reset MAC */
1297                                 atl1e_irq_reset(adapter);
1298                                 schedule_work(&adapter->reset_task);
1299                                 break;
1300                         }
1301                 }
1302
1303                 /* check if DMA read/write error */
1304                 if (status & (ISR_DMAR_TO_RST | ISR_DMAW_TO_RST)) {
1305                         netdev_err(adapter->netdev,
1306                                    "PCIE DMA RW error (status = 0x%x)\n",
1307                                    status);
1308                         atl1e_irq_reset(adapter);
1309                         schedule_work(&adapter->reset_task);
1310                         break;
1311                 }
1312
1313                 if (status & ISR_SMB)
1314                         atl1e_update_hw_stats(adapter);
1315
1316                 /* link event */
1317                 if (status & (ISR_GPHY | ISR_MANUAL)) {
1318                         netdev->stats.tx_carrier_errors++;
1319                         atl1e_link_chg_event(adapter);
1320                         break;
1321                 }
1322
1323                 /* transmit event */
1324                 if (status & ISR_TX_EVENT)
1325                         atl1e_clean_tx_irq(adapter);
1326
1327                 if (status & ISR_RX_EVENT) {
1328                         /*
1329                          * disable rx interrupts, without
1330                          * the synchronize_irq bit
1331                          */
1332                         AT_WRITE_REG(hw, REG_IMR,
1333                                      IMR_NORMAL_MASK & ~ISR_RX_EVENT);
1334                         AT_WRITE_FLUSH(hw);
1335                         if (likely(napi_schedule_prep(
1336                                    &adapter->napi)))
1337                                 __napi_schedule(&adapter->napi);
1338                 }
1339         } while (--max_ints > 0);
1340         /* re-enable Interrupt*/
1341         AT_WRITE_REG(&adapter->hw, REG_ISR, 0);
1342
1343         return handled;
1344 }
1345
1346 static inline void atl1e_rx_checksum(struct atl1e_adapter *adapter,
1347                   struct sk_buff *skb, struct atl1e_recv_ret_status *prrs)
1348 {
1349         u8 *packet = (u8 *)(prrs + 1);
1350         struct iphdr *iph;
1351         u16 head_len = ETH_HLEN;
1352         u16 pkt_flags;
1353         u16 err_flags;
1354
1355         skb_checksum_none_assert(skb);
1356         pkt_flags = prrs->pkt_flag;
1357         err_flags = prrs->err_flag;
1358         if (((pkt_flags & RRS_IS_IPV4) || (pkt_flags & RRS_IS_IPV6)) &&
1359                 ((pkt_flags & RRS_IS_TCP) || (pkt_flags & RRS_IS_UDP))) {
1360                 if (pkt_flags & RRS_IS_IPV4) {
1361                         if (pkt_flags & RRS_IS_802_3)
1362                                 head_len += 8;
1363                         iph = (struct iphdr *) (packet + head_len);
1364                         if (iph->frag_off != 0 && !(pkt_flags & RRS_IS_IP_DF))
1365                                 goto hw_xsum;
1366                 }
1367                 if (!(err_flags & (RRS_ERR_IP_CSUM | RRS_ERR_L4_CSUM))) {
1368                         skb->ip_summed = CHECKSUM_UNNECESSARY;
1369                         return;
1370                 }
1371         }
1372
1373 hw_xsum :
1374         return;
1375 }
1376
1377 static struct atl1e_rx_page *atl1e_get_rx_page(struct atl1e_adapter *adapter,
1378                                                u8 que)
1379 {
1380         struct atl1e_rx_page_desc *rx_page_desc =
1381                 (struct atl1e_rx_page_desc *) adapter->rx_ring.rx_page_desc;
1382         u8 rx_using = rx_page_desc[que].rx_using;
1383
1384         return (struct atl1e_rx_page *)&(rx_page_desc[que].rx_page[rx_using]);
1385 }
1386
1387 static void atl1e_clean_rx_irq(struct atl1e_adapter *adapter, u8 que,
1388                    int *work_done, int work_to_do)
1389 {
1390         struct net_device *netdev  = adapter->netdev;
1391         struct atl1e_rx_ring *rx_ring = (struct atl1e_rx_ring *)
1392                                          &adapter->rx_ring;
1393         struct atl1e_rx_page_desc *rx_page_desc =
1394                 (struct atl1e_rx_page_desc *) rx_ring->rx_page_desc;
1395         struct sk_buff *skb = NULL;
1396         struct atl1e_rx_page *rx_page = atl1e_get_rx_page(adapter, que);
1397         u32 packet_size, write_offset;
1398         struct atl1e_recv_ret_status *prrs;
1399
1400         write_offset = *(rx_page->write_offset_addr);
1401         if (likely(rx_page->read_offset < write_offset)) {
1402                 do {
1403                         if (*work_done >= work_to_do)
1404                                 break;
1405                         (*work_done)++;
1406                         /* get new packet's  rrs */
1407                         prrs = (struct atl1e_recv_ret_status *) (rx_page->addr +
1408                                                  rx_page->read_offset);
1409                         /* check sequence number */
1410                         if (prrs->seq_num != rx_page_desc[que].rx_nxseq) {
1411                                 netdev_err(netdev,
1412                                            "rx sequence number error (rx=%d) (expect=%d)\n",
1413                                            prrs->seq_num,
1414                                            rx_page_desc[que].rx_nxseq);
1415                                 rx_page_desc[que].rx_nxseq++;
1416                                 /* just for debug use */
1417                                 AT_WRITE_REG(&adapter->hw, REG_DEBUG_DATA0,
1418                                              (((u32)prrs->seq_num) << 16) |
1419                                              rx_page_desc[que].rx_nxseq);
1420                                 goto fatal_err;
1421                         }
1422                         rx_page_desc[que].rx_nxseq++;
1423
1424                         /* error packet */
1425                         if (prrs->pkt_flag & RRS_IS_ERR_FRAME) {
1426                                 if (prrs->err_flag & (RRS_ERR_BAD_CRC |
1427                                         RRS_ERR_DRIBBLE | RRS_ERR_CODE |
1428                                         RRS_ERR_TRUNC)) {
1429                                 /* hardware error, discard this packet*/
1430                                         netdev_err(netdev,
1431                                                    "rx packet desc error %x\n",
1432                                                    *((u32 *)prrs + 1));
1433                                         goto skip_pkt;
1434                                 }
1435                         }
1436
1437                         packet_size = ((prrs->word1 >> RRS_PKT_SIZE_SHIFT) &
1438                                         RRS_PKT_SIZE_MASK) - 4; /* CRC */
1439                         skb = netdev_alloc_skb_ip_align(netdev, packet_size);
1440                         if (skb == NULL) {
1441                                 netdev_warn(netdev,
1442                                             "Memory squeeze, deferring packet\n");
1443                                 goto skip_pkt;
1444                         }
1445                         memcpy(skb->data, (u8 *)(prrs + 1), packet_size);
1446                         skb_put(skb, packet_size);
1447                         skb->protocol = eth_type_trans(skb, netdev);
1448                         atl1e_rx_checksum(adapter, skb, prrs);
1449
1450                         if (prrs->pkt_flag & RRS_IS_VLAN_TAG) {
1451                                 u16 vlan_tag = (prrs->vtag >> 4) |
1452                                                ((prrs->vtag & 7) << 13) |
1453                                                ((prrs->vtag & 8) << 9);
1454                                 netdev_dbg(netdev,
1455                                            "RXD VLAN TAG<RRD>=0x%04x\n",
1456                                            prrs->vtag);
1457                                 __vlan_hwaccel_put_tag(skb, vlan_tag);
1458                         }
1459                         netif_receive_skb(skb);
1460
1461 skip_pkt:
1462         /* skip current packet whether it's ok or not. */
1463                         rx_page->read_offset +=
1464                                 (((u32)((prrs->word1 >> RRS_PKT_SIZE_SHIFT) &
1465                                 RRS_PKT_SIZE_MASK) +
1466                                 sizeof(struct atl1e_recv_ret_status) + 31) &
1467                                                 0xFFFFFFE0);
1468
1469                         if (rx_page->read_offset >= rx_ring->page_size) {
1470                                 /* mark this page clean */
1471                                 u16 reg_addr;
1472                                 u8  rx_using;
1473
1474                                 rx_page->read_offset =
1475                                         *(rx_page->write_offset_addr) = 0;
1476                                 rx_using = rx_page_desc[que].rx_using;
1477                                 reg_addr =
1478                                         atl1e_rx_page_vld_regs[que][rx_using];
1479                                 AT_WRITE_REGB(&adapter->hw, reg_addr, 1);
1480                                 rx_page_desc[que].rx_using ^= 1;
1481                                 rx_page = atl1e_get_rx_page(adapter, que);
1482                         }
1483                         write_offset = *(rx_page->write_offset_addr);
1484                 } while (rx_page->read_offset < write_offset);
1485         }
1486
1487         return;
1488
1489 fatal_err:
1490         if (!test_bit(__AT_DOWN, &adapter->flags))
1491                 schedule_work(&adapter->reset_task);
1492 }
1493
1494 /*
1495  * atl1e_clean - NAPI Rx polling callback
1496  * @adapter: board private structure
1497  */
1498 static int atl1e_clean(struct napi_struct *napi, int budget)
1499 {
1500         struct atl1e_adapter *adapter =
1501                         container_of(napi, struct atl1e_adapter, napi);
1502         u32 imr_data;
1503         int work_done = 0;
1504
1505         /* Keep link state information with original netdev */
1506         if (!netif_carrier_ok(adapter->netdev))
1507                 goto quit_polling;
1508
1509         atl1e_clean_rx_irq(adapter, 0, &work_done, budget);
1510
1511         /* If no Tx and not enough Rx work done, exit the polling mode */
1512         if (work_done < budget) {
1513 quit_polling:
1514                 napi_complete(napi);
1515                 imr_data = AT_READ_REG(&adapter->hw, REG_IMR);
1516                 AT_WRITE_REG(&adapter->hw, REG_IMR, imr_data | ISR_RX_EVENT);
1517                 /* test debug */
1518                 if (test_bit(__AT_DOWN, &adapter->flags)) {
1519                         atomic_dec(&adapter->irq_sem);
1520                         netdev_err(adapter->netdev,
1521                                    "atl1e_clean is called when AT_DOWN\n");
1522                 }
1523                 /* reenable RX intr */
1524                 /*atl1e_irq_enable(adapter); */
1525
1526         }
1527         return work_done;
1528 }
1529
1530 #ifdef CONFIG_NET_POLL_CONTROLLER
1531
1532 /*
1533  * Polling 'interrupt' - used by things like netconsole to send skbs
1534  * without having to re-enable interrupts. It's not called while
1535  * the interrupt routine is executing.
1536  */
1537 static void atl1e_netpoll(struct net_device *netdev)
1538 {
1539         struct atl1e_adapter *adapter = netdev_priv(netdev);
1540
1541         disable_irq(adapter->pdev->irq);
1542         atl1e_intr(adapter->pdev->irq, netdev);
1543         enable_irq(adapter->pdev->irq);
1544 }
1545 #endif
1546
1547 static inline u16 atl1e_tpd_avail(struct atl1e_adapter *adapter)
1548 {
1549         struct atl1e_tx_ring *tx_ring = &adapter->tx_ring;
1550         u16 next_to_use = 0;
1551         u16 next_to_clean = 0;
1552
1553         next_to_clean = atomic_read(&tx_ring->next_to_clean);
1554         next_to_use   = tx_ring->next_to_use;
1555
1556         return (u16)(next_to_clean > next_to_use) ?
1557                 (next_to_clean - next_to_use - 1) :
1558                 (tx_ring->count + next_to_clean - next_to_use - 1);
1559 }
1560
1561 /*
1562  * get next usable tpd
1563  * Note: should call atl1e_tdp_avail to make sure
1564  * there is enough tpd to use
1565  */
1566 static struct atl1e_tpd_desc *atl1e_get_tpd(struct atl1e_adapter *adapter)
1567 {
1568         struct atl1e_tx_ring *tx_ring = &adapter->tx_ring;
1569         u16 next_to_use = 0;
1570
1571         next_to_use = tx_ring->next_to_use;
1572         if (++tx_ring->next_to_use == tx_ring->count)
1573                 tx_ring->next_to_use = 0;
1574
1575         memset(&tx_ring->desc[next_to_use], 0, sizeof(struct atl1e_tpd_desc));
1576         return (struct atl1e_tpd_desc *)&tx_ring->desc[next_to_use];
1577 }
1578
1579 static struct atl1e_tx_buffer *
1580 atl1e_get_tx_buffer(struct atl1e_adapter *adapter, struct atl1e_tpd_desc *tpd)
1581 {
1582         struct atl1e_tx_ring *tx_ring = &adapter->tx_ring;
1583
1584         return &tx_ring->tx_buffer[tpd - tx_ring->desc];
1585 }
1586
1587 /* Calculate the transmit packet descript needed*/
1588 static u16 atl1e_cal_tdp_req(const struct sk_buff *skb)
1589 {
1590         int i = 0;
1591         u16 tpd_req = 1;
1592         u16 fg_size = 0;
1593         u16 proto_hdr_len = 0;
1594
1595         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1596                 fg_size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1597                 tpd_req += ((fg_size + MAX_TX_BUF_LEN - 1) >> MAX_TX_BUF_SHIFT);
1598         }
1599
1600         if (skb_is_gso(skb)) {
1601                 if (skb->protocol == htons(ETH_P_IP) ||
1602                    (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6)) {
1603                         proto_hdr_len = skb_transport_offset(skb) +
1604                                         tcp_hdrlen(skb);
1605                         if (proto_hdr_len < skb_headlen(skb)) {
1606                                 tpd_req += ((skb_headlen(skb) - proto_hdr_len +
1607                                            MAX_TX_BUF_LEN - 1) >>
1608                                            MAX_TX_BUF_SHIFT);
1609                         }
1610                 }
1611
1612         }
1613         return tpd_req;
1614 }
1615
1616 static int atl1e_tso_csum(struct atl1e_adapter *adapter,
1617                        struct sk_buff *skb, struct atl1e_tpd_desc *tpd)
1618 {
1619         u8 hdr_len;
1620         u32 real_len;
1621         unsigned short offload_type;
1622         int err;
1623
1624         if (skb_is_gso(skb)) {
1625                 if (skb_header_cloned(skb)) {
1626                         err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1627                         if (unlikely(err))
1628                                 return -1;
1629                 }
1630                 offload_type = skb_shinfo(skb)->gso_type;
1631
1632                 if (offload_type & SKB_GSO_TCPV4) {
1633                         real_len = (((unsigned char *)ip_hdr(skb) - skb->data)
1634                                         + ntohs(ip_hdr(skb)->tot_len));
1635
1636                         if (real_len < skb->len)
1637                                 pskb_trim(skb, real_len);
1638
1639                         hdr_len = (skb_transport_offset(skb) + tcp_hdrlen(skb));
1640                         if (unlikely(skb->len == hdr_len)) {
1641                                 /* only xsum need */
1642                                 netdev_warn(adapter->netdev,
1643                                             "IPV4 tso with zero data??\n");
1644                                 goto check_sum;
1645                         } else {
1646                                 ip_hdr(skb)->check = 0;
1647                                 ip_hdr(skb)->tot_len = 0;
1648                                 tcp_hdr(skb)->check = ~csum_tcpudp_magic(
1649                                                         ip_hdr(skb)->saddr,
1650                                                         ip_hdr(skb)->daddr,
1651                                                         0, IPPROTO_TCP, 0);
1652                                 tpd->word3 |= (ip_hdr(skb)->ihl &
1653                                         TDP_V4_IPHL_MASK) <<
1654                                         TPD_V4_IPHL_SHIFT;
1655                                 tpd->word3 |= ((tcp_hdrlen(skb) >> 2) &
1656                                         TPD_TCPHDRLEN_MASK) <<
1657                                         TPD_TCPHDRLEN_SHIFT;
1658                                 tpd->word3 |= ((skb_shinfo(skb)->gso_size) &
1659                                         TPD_MSS_MASK) << TPD_MSS_SHIFT;
1660                                 tpd->word3 |= 1 << TPD_SEGMENT_EN_SHIFT;
1661                         }
1662                         return 0;
1663                 }
1664         }
1665
1666 check_sum:
1667         if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
1668                 u8 css, cso;
1669
1670                 cso = skb_checksum_start_offset(skb);
1671                 if (unlikely(cso & 0x1)) {
1672                         netdev_err(adapter->netdev,
1673                                    "payload offset should not ant event number\n");
1674                         return -1;
1675                 } else {
1676                         css = cso + skb->csum_offset;
1677                         tpd->word3 |= (cso & TPD_PLOADOFFSET_MASK) <<
1678                                         TPD_PLOADOFFSET_SHIFT;
1679                         tpd->word3 |= (css & TPD_CCSUMOFFSET_MASK) <<
1680                                         TPD_CCSUMOFFSET_SHIFT;
1681                         tpd->word3 |= 1 << TPD_CC_SEGMENT_EN_SHIFT;
1682                 }
1683         }
1684
1685         return 0;
1686 }
1687
1688 static void atl1e_tx_map(struct atl1e_adapter *adapter,
1689                       struct sk_buff *skb, struct atl1e_tpd_desc *tpd)
1690 {
1691         struct atl1e_tpd_desc *use_tpd = NULL;
1692         struct atl1e_tx_buffer *tx_buffer = NULL;
1693         u16 buf_len = skb_headlen(skb);
1694         u16 map_len = 0;
1695         u16 mapped_len = 0;
1696         u16 hdr_len = 0;
1697         u16 nr_frags;
1698         u16 f;
1699         int segment;
1700
1701         nr_frags = skb_shinfo(skb)->nr_frags;
1702         segment = (tpd->word3 >> TPD_SEGMENT_EN_SHIFT) & TPD_SEGMENT_EN_MASK;
1703         if (segment) {
1704                 /* TSO */
1705                 map_len = hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1706                 use_tpd = tpd;
1707
1708                 tx_buffer = atl1e_get_tx_buffer(adapter, use_tpd);
1709                 tx_buffer->length = map_len;
1710                 tx_buffer->dma = pci_map_single(adapter->pdev,
1711                                         skb->data, hdr_len, PCI_DMA_TODEVICE);
1712                 ATL1E_SET_PCIMAP_TYPE(tx_buffer, ATL1E_TX_PCIMAP_SINGLE);
1713                 mapped_len += map_len;
1714                 use_tpd->buffer_addr = cpu_to_le64(tx_buffer->dma);
1715                 use_tpd->word2 = (use_tpd->word2 & (~TPD_BUFLEN_MASK)) |
1716                         ((cpu_to_le32(tx_buffer->length) &
1717                         TPD_BUFLEN_MASK) << TPD_BUFLEN_SHIFT);
1718         }
1719
1720         while (mapped_len < buf_len) {
1721                 /* mapped_len == 0, means we should use the first tpd,
1722                    which is given by caller  */
1723                 if (mapped_len == 0) {
1724                         use_tpd = tpd;
1725                 } else {
1726                         use_tpd = atl1e_get_tpd(adapter);
1727                         memcpy(use_tpd, tpd, sizeof(struct atl1e_tpd_desc));
1728                 }
1729                 tx_buffer = atl1e_get_tx_buffer(adapter, use_tpd);
1730                 tx_buffer->skb = NULL;
1731
1732                 tx_buffer->length = map_len =
1733                         ((buf_len - mapped_len) >= MAX_TX_BUF_LEN) ?
1734                         MAX_TX_BUF_LEN : (buf_len - mapped_len);
1735                 tx_buffer->dma =
1736                         pci_map_single(adapter->pdev, skb->data + mapped_len,
1737                                         map_len, PCI_DMA_TODEVICE);
1738                 ATL1E_SET_PCIMAP_TYPE(tx_buffer, ATL1E_TX_PCIMAP_SINGLE);
1739                 mapped_len  += map_len;
1740                 use_tpd->buffer_addr = cpu_to_le64(tx_buffer->dma);
1741                 use_tpd->word2 = (use_tpd->word2 & (~TPD_BUFLEN_MASK)) |
1742                         ((cpu_to_le32(tx_buffer->length) &
1743                         TPD_BUFLEN_MASK) << TPD_BUFLEN_SHIFT);
1744         }
1745
1746         for (f = 0; f < nr_frags; f++) {
1747                 const struct skb_frag_struct *frag;
1748                 u16 i;
1749                 u16 seg_num;
1750
1751                 frag = &skb_shinfo(skb)->frags[f];
1752                 buf_len = skb_frag_size(frag);
1753
1754                 seg_num = (buf_len + MAX_TX_BUF_LEN - 1) / MAX_TX_BUF_LEN;
1755                 for (i = 0; i < seg_num; i++) {
1756                         use_tpd = atl1e_get_tpd(adapter);
1757                         memcpy(use_tpd, tpd, sizeof(struct atl1e_tpd_desc));
1758
1759                         tx_buffer = atl1e_get_tx_buffer(adapter, use_tpd);
1760                         BUG_ON(tx_buffer->skb);
1761
1762                         tx_buffer->skb = NULL;
1763                         tx_buffer->length =
1764                                 (buf_len > MAX_TX_BUF_LEN) ?
1765                                 MAX_TX_BUF_LEN : buf_len;
1766                         buf_len -= tx_buffer->length;
1767
1768                         tx_buffer->dma = skb_frag_dma_map(&adapter->pdev->dev,
1769                                                           frag,
1770                                                           (i * MAX_TX_BUF_LEN),
1771                                                           tx_buffer->length,
1772                                                           DMA_TO_DEVICE);
1773                         ATL1E_SET_PCIMAP_TYPE(tx_buffer, ATL1E_TX_PCIMAP_PAGE);
1774                         use_tpd->buffer_addr = cpu_to_le64(tx_buffer->dma);
1775                         use_tpd->word2 = (use_tpd->word2 & (~TPD_BUFLEN_MASK)) |
1776                                         ((cpu_to_le32(tx_buffer->length) &
1777                                         TPD_BUFLEN_MASK) << TPD_BUFLEN_SHIFT);
1778                 }
1779         }
1780
1781         if ((tpd->word3 >> TPD_SEGMENT_EN_SHIFT) & TPD_SEGMENT_EN_MASK)
1782                 /* note this one is a tcp header */
1783                 tpd->word3 |= 1 << TPD_HDRFLAG_SHIFT;
1784         /* The last tpd */
1785
1786         use_tpd->word3 |= 1 << TPD_EOP_SHIFT;
1787         /* The last buffer info contain the skb address,
1788            so it will be free after unmap */
1789         tx_buffer->skb = skb;
1790 }
1791
1792 static void atl1e_tx_queue(struct atl1e_adapter *adapter, u16 count,
1793                            struct atl1e_tpd_desc *tpd)
1794 {
1795         struct atl1e_tx_ring *tx_ring = &adapter->tx_ring;
1796         /* Force memory writes to complete before letting h/w
1797          * know there are new descriptors to fetch.  (Only
1798          * applicable for weak-ordered memory model archs,
1799          * such as IA-64). */
1800         wmb();
1801         AT_WRITE_REG(&adapter->hw, REG_MB_TPD_PROD_IDX, tx_ring->next_to_use);
1802 }
1803
1804 static netdev_tx_t atl1e_xmit_frame(struct sk_buff *skb,
1805                                           struct net_device *netdev)
1806 {
1807         struct atl1e_adapter *adapter = netdev_priv(netdev);
1808         unsigned long flags;
1809         u16 tpd_req = 1;
1810         struct atl1e_tpd_desc *tpd;
1811
1812         if (test_bit(__AT_DOWN, &adapter->flags)) {
1813                 dev_kfree_skb_any(skb);
1814                 return NETDEV_TX_OK;
1815         }
1816
1817         if (unlikely(skb->len <= 0)) {
1818                 dev_kfree_skb_any(skb);
1819                 return NETDEV_TX_OK;
1820         }
1821         tpd_req = atl1e_cal_tdp_req(skb);
1822         if (!spin_trylock_irqsave(&adapter->tx_lock, flags))
1823                 return NETDEV_TX_LOCKED;
1824
1825         if (atl1e_tpd_avail(adapter) < tpd_req) {
1826                 /* no enough descriptor, just stop queue */
1827                 netif_stop_queue(netdev);
1828                 spin_unlock_irqrestore(&adapter->tx_lock, flags);
1829                 return NETDEV_TX_BUSY;
1830         }
1831
1832         tpd = atl1e_get_tpd(adapter);
1833
1834         if (vlan_tx_tag_present(skb)) {
1835                 u16 vlan_tag = vlan_tx_tag_get(skb);
1836                 u16 atl1e_vlan_tag;
1837
1838                 tpd->word3 |= 1 << TPD_INS_VL_TAG_SHIFT;
1839                 AT_VLAN_TAG_TO_TPD_TAG(vlan_tag, atl1e_vlan_tag);
1840                 tpd->word2 |= (atl1e_vlan_tag & TPD_VLANTAG_MASK) <<
1841                                 TPD_VLAN_SHIFT;
1842         }
1843
1844         if (skb->protocol == htons(ETH_P_8021Q))
1845                 tpd->word3 |= 1 << TPD_VL_TAGGED_SHIFT;
1846
1847         if (skb_network_offset(skb) != ETH_HLEN)
1848                 tpd->word3 |= 1 << TPD_ETHTYPE_SHIFT; /* 802.3 frame */
1849
1850         /* do TSO and check sum */
1851         if (atl1e_tso_csum(adapter, skb, tpd) != 0) {
1852                 spin_unlock_irqrestore(&adapter->tx_lock, flags);
1853                 dev_kfree_skb_any(skb);
1854                 return NETDEV_TX_OK;
1855         }
1856
1857         atl1e_tx_map(adapter, skb, tpd);
1858         atl1e_tx_queue(adapter, tpd_req, tpd);
1859
1860         netdev->trans_start = jiffies; /* NETIF_F_LLTX driver :( */
1861         spin_unlock_irqrestore(&adapter->tx_lock, flags);
1862         return NETDEV_TX_OK;
1863 }
1864
1865 static void atl1e_free_irq(struct atl1e_adapter *adapter)
1866 {
1867         struct net_device *netdev = adapter->netdev;
1868
1869         free_irq(adapter->pdev->irq, netdev);
1870 }
1871
1872 static int atl1e_request_irq(struct atl1e_adapter *adapter)
1873 {
1874         struct pci_dev    *pdev   = adapter->pdev;
1875         struct net_device *netdev = adapter->netdev;
1876         int err = 0;
1877
1878         err = request_irq(pdev->irq, atl1e_intr, IRQF_SHARED,
1879                           netdev->name, netdev);
1880         if (err) {
1881                 netdev_dbg(adapter->netdev,
1882                            "Unable to allocate interrupt Error: %d\n", err);
1883                 return err;
1884         }
1885         netdev_dbg(adapter->netdev, "atl1e_request_irq OK\n");
1886         return err;
1887 }
1888
1889 int atl1e_up(struct atl1e_adapter *adapter)
1890 {
1891         struct net_device *netdev = adapter->netdev;
1892         int err = 0;
1893         u32 val;
1894
1895         /* hardware has been reset, we need to reload some things */
1896         err = atl1e_init_hw(&adapter->hw);
1897         if (err) {
1898                 err = -EIO;
1899                 return err;
1900         }
1901         atl1e_init_ring_ptrs(adapter);
1902         atl1e_set_multi(netdev);
1903         atl1e_restore_vlan(adapter);
1904
1905         if (atl1e_configure(adapter)) {
1906                 err = -EIO;
1907                 goto err_up;
1908         }
1909
1910         clear_bit(__AT_DOWN, &adapter->flags);
1911         napi_enable(&adapter->napi);
1912         atl1e_irq_enable(adapter);
1913         val = AT_READ_REG(&adapter->hw, REG_MASTER_CTRL);
1914         AT_WRITE_REG(&adapter->hw, REG_MASTER_CTRL,
1915                       val | MASTER_CTRL_MANUAL_INT);
1916
1917 err_up:
1918         return err;
1919 }
1920
1921 void atl1e_down(struct atl1e_adapter *adapter)
1922 {
1923         struct net_device *netdev = adapter->netdev;
1924
1925         /* signal that we're down so the interrupt handler does not
1926          * reschedule our watchdog timer */
1927         set_bit(__AT_DOWN, &adapter->flags);
1928
1929         netif_stop_queue(netdev);
1930
1931         /* reset MAC to disable all RX/TX */
1932         atl1e_reset_hw(&adapter->hw);
1933         msleep(1);
1934
1935         napi_disable(&adapter->napi);
1936         atl1e_del_timer(adapter);
1937         atl1e_irq_disable(adapter);
1938
1939         netif_carrier_off(netdev);
1940         adapter->link_speed = SPEED_0;
1941         adapter->link_duplex = -1;
1942         atl1e_clean_tx_ring(adapter);
1943         atl1e_clean_rx_ring(adapter);
1944 }
1945
1946 /*
1947  * atl1e_open - Called when a network interface is made active
1948  * @netdev: network interface device structure
1949  *
1950  * Returns 0 on success, negative value on failure
1951  *
1952  * The open entry point is called when a network interface is made
1953  * active by the system (IFF_UP).  At this point all resources needed
1954  * for transmit and receive operations are allocated, the interrupt
1955  * handler is registered with the OS, the watchdog timer is started,
1956  * and the stack is notified that the interface is ready.
1957  */
1958 static int atl1e_open(struct net_device *netdev)
1959 {
1960         struct atl1e_adapter *adapter = netdev_priv(netdev);
1961         int err;
1962
1963         /* disallow open during test */
1964         if (test_bit(__AT_TESTING, &adapter->flags))
1965                 return -EBUSY;
1966
1967         /* allocate rx/tx dma buffer & descriptors */
1968         atl1e_init_ring_resources(adapter);
1969         err = atl1e_setup_ring_resources(adapter);
1970         if (unlikely(err))
1971                 return err;
1972
1973         err = atl1e_request_irq(adapter);
1974         if (unlikely(err))
1975                 goto err_req_irq;
1976
1977         err = atl1e_up(adapter);
1978         if (unlikely(err))
1979                 goto err_up;
1980
1981         return 0;
1982
1983 err_up:
1984         atl1e_free_irq(adapter);
1985 err_req_irq:
1986         atl1e_free_ring_resources(adapter);
1987         atl1e_reset_hw(&adapter->hw);
1988
1989         return err;
1990 }
1991
1992 /*
1993  * atl1e_close - Disables a network interface
1994  * @netdev: network interface device structure
1995  *
1996  * Returns 0, this is not allowed to fail
1997  *
1998  * The close entry point is called when an interface is de-activated
1999  * by the OS.  The hardware is still under the drivers control, but
2000  * needs to be disabled.  A global MAC reset is issued to stop the
2001  * hardware, and all transmit and receive resources are freed.
2002  */
2003 static int atl1e_close(struct net_device *netdev)
2004 {
2005         struct atl1e_adapter *adapter = netdev_priv(netdev);
2006
2007         WARN_ON(test_bit(__AT_RESETTING, &adapter->flags));
2008         atl1e_down(adapter);
2009         atl1e_free_irq(adapter);
2010         atl1e_free_ring_resources(adapter);
2011
2012         return 0;
2013 }
2014
2015 static int atl1e_suspend(struct pci_dev *pdev, pm_message_t state)
2016 {
2017         struct net_device *netdev = pci_get_drvdata(pdev);
2018         struct atl1e_adapter *adapter = netdev_priv(netdev);
2019         struct atl1e_hw *hw = &adapter->hw;
2020         u32 ctrl = 0;
2021         u32 mac_ctrl_data = 0;
2022         u32 wol_ctrl_data = 0;
2023         u16 mii_advertise_data = 0;
2024         u16 mii_bmsr_data = 0;
2025         u16 mii_intr_status_data = 0;
2026         u32 wufc = adapter->wol;
2027         u32 i;
2028 #ifdef CONFIG_PM
2029         int retval = 0;
2030 #endif
2031
2032         if (netif_running(netdev)) {
2033                 WARN_ON(test_bit(__AT_RESETTING, &adapter->flags));
2034                 atl1e_down(adapter);
2035         }
2036         netif_device_detach(netdev);
2037
2038 #ifdef CONFIG_PM
2039         retval = pci_save_state(pdev);
2040         if (retval)
2041                 return retval;
2042 #endif
2043
2044         if (wufc) {
2045                 /* get link status */
2046                 atl1e_read_phy_reg(hw, MII_BMSR, (u16 *)&mii_bmsr_data);
2047                 atl1e_read_phy_reg(hw, MII_BMSR, (u16 *)&mii_bmsr_data);
2048
2049                 mii_advertise_data = ADVERTISE_10HALF;
2050
2051                 if ((atl1e_write_phy_reg(hw, MII_CTRL1000, 0) != 0) ||
2052                     (atl1e_write_phy_reg(hw,
2053                            MII_ADVERTISE, mii_advertise_data) != 0) ||
2054                     (atl1e_phy_commit(hw)) != 0) {
2055                         netdev_dbg(adapter->netdev, "set phy register failed\n");
2056                         goto wol_dis;
2057                 }
2058
2059                 hw->phy_configured = false; /* re-init PHY when resume */
2060
2061                 /* turn on magic packet wol */
2062                 if (wufc & AT_WUFC_MAG)
2063                         wol_ctrl_data |= WOL_MAGIC_EN | WOL_MAGIC_PME_EN;
2064
2065                 if (wufc & AT_WUFC_LNKC) {
2066                 /* if orignal link status is link, just wait for retrive link */
2067                         if (mii_bmsr_data & BMSR_LSTATUS) {
2068                                 for (i = 0; i < AT_SUSPEND_LINK_TIMEOUT; i++) {
2069                                         msleep(100);
2070                                         atl1e_read_phy_reg(hw, MII_BMSR,
2071                                                         (u16 *)&mii_bmsr_data);
2072                                         if (mii_bmsr_data & BMSR_LSTATUS)
2073                                                 break;
2074                                 }
2075
2076                                 if ((mii_bmsr_data & BMSR_LSTATUS) == 0)
2077                                         netdev_dbg(adapter->netdev,
2078                                                    "Link may change when suspend\n");
2079                         }
2080                         wol_ctrl_data |=  WOL_LINK_CHG_EN | WOL_LINK_CHG_PME_EN;
2081                         /* only link up can wake up */
2082                         if (atl1e_write_phy_reg(hw, MII_INT_CTRL, 0x400) != 0) {
2083                                 netdev_dbg(adapter->netdev,
2084                                            "read write phy register failed\n");
2085                                 goto wol_dis;
2086                         }
2087                 }
2088                 /* clear phy interrupt */
2089                 atl1e_read_phy_reg(hw, MII_INT_STATUS, &mii_intr_status_data);
2090                 /* Config MAC Ctrl register */
2091                 mac_ctrl_data = MAC_CTRL_RX_EN;
2092                 /* set to 10/100M halt duplex */
2093                 mac_ctrl_data |= MAC_CTRL_SPEED_10_100 << MAC_CTRL_SPEED_SHIFT;
2094                 mac_ctrl_data |= (((u32)adapter->hw.preamble_len &
2095                                  MAC_CTRL_PRMLEN_MASK) <<
2096                                  MAC_CTRL_PRMLEN_SHIFT);
2097
2098                 __atl1e_vlan_mode(netdev->features, &mac_ctrl_data);
2099
2100                 /* magic packet maybe Broadcast&multicast&Unicast frame */
2101                 if (wufc & AT_WUFC_MAG)
2102                         mac_ctrl_data |= MAC_CTRL_BC_EN;
2103
2104                 netdev_dbg(adapter->netdev, "suspend MAC=0x%x\n",
2105                            mac_ctrl_data);
2106
2107                 AT_WRITE_REG(hw, REG_WOL_CTRL, wol_ctrl_data);
2108                 AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
2109                 /* pcie patch */
2110                 ctrl = AT_READ_REG(hw, REG_PCIE_PHYMISC);
2111                 ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
2112                 AT_WRITE_REG(hw, REG_PCIE_PHYMISC, ctrl);
2113                 pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
2114                 goto suspend_exit;
2115         }
2116 wol_dis:
2117
2118         /* WOL disabled */
2119         AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
2120
2121         /* pcie patch */
2122         ctrl = AT_READ_REG(hw, REG_PCIE_PHYMISC);
2123         ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
2124         AT_WRITE_REG(hw, REG_PCIE_PHYMISC, ctrl);
2125
2126         atl1e_force_ps(hw);
2127         hw->phy_configured = false; /* re-init PHY when resume */
2128
2129         pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
2130
2131 suspend_exit:
2132
2133         if (netif_running(netdev))
2134                 atl1e_free_irq(adapter);
2135
2136         pci_disable_device(pdev);
2137
2138         pci_set_power_state(pdev, pci_choose_state(pdev, state));
2139
2140         return 0;
2141 }
2142
2143 #ifdef CONFIG_PM
2144 static int atl1e_resume(struct pci_dev *pdev)
2145 {
2146         struct net_device *netdev = pci_get_drvdata(pdev);
2147         struct atl1e_adapter *adapter = netdev_priv(netdev);
2148         u32 err;
2149
2150         pci_set_power_state(pdev, PCI_D0);
2151         pci_restore_state(pdev);
2152
2153         err = pci_enable_device(pdev);
2154         if (err) {
2155                 netdev_err(adapter->netdev,
2156                            "Cannot enable PCI device from suspend\n");
2157                 return err;
2158         }
2159
2160         pci_set_master(pdev);
2161
2162         AT_READ_REG(&adapter->hw, REG_WOL_CTRL); /* clear WOL status */
2163
2164         pci_enable_wake(pdev, PCI_D3hot, 0);
2165         pci_enable_wake(pdev, PCI_D3cold, 0);
2166
2167         AT_WRITE_REG(&adapter->hw, REG_WOL_CTRL, 0);
2168
2169         if (netif_running(netdev)) {
2170                 err = atl1e_request_irq(adapter);
2171                 if (err)
2172                         return err;
2173         }
2174
2175         atl1e_reset_hw(&adapter->hw);
2176
2177         if (netif_running(netdev))
2178                 atl1e_up(adapter);
2179
2180         netif_device_attach(netdev);
2181
2182         return 0;
2183 }
2184 #endif
2185
2186 static void atl1e_shutdown(struct pci_dev *pdev)
2187 {
2188         atl1e_suspend(pdev, PMSG_SUSPEND);
2189 }
2190
2191 static const struct net_device_ops atl1e_netdev_ops = {
2192         .ndo_open               = atl1e_open,
2193         .ndo_stop               = atl1e_close,
2194         .ndo_start_xmit         = atl1e_xmit_frame,
2195         .ndo_get_stats          = atl1e_get_stats,
2196         .ndo_set_rx_mode        = atl1e_set_multi,
2197         .ndo_validate_addr      = eth_validate_addr,
2198         .ndo_set_mac_address    = atl1e_set_mac_addr,
2199         .ndo_fix_features       = atl1e_fix_features,
2200         .ndo_set_features       = atl1e_set_features,
2201         .ndo_change_mtu         = atl1e_change_mtu,
2202         .ndo_do_ioctl           = atl1e_ioctl,
2203         .ndo_tx_timeout         = atl1e_tx_timeout,
2204 #ifdef CONFIG_NET_POLL_CONTROLLER
2205         .ndo_poll_controller    = atl1e_netpoll,
2206 #endif
2207
2208 };
2209
2210 static int atl1e_init_netdev(struct net_device *netdev, struct pci_dev *pdev)
2211 {
2212         SET_NETDEV_DEV(netdev, &pdev->dev);
2213         pci_set_drvdata(pdev, netdev);
2214
2215         netdev->irq  = pdev->irq;
2216         netdev->netdev_ops = &atl1e_netdev_ops;
2217
2218         netdev->watchdog_timeo = AT_TX_WATCHDOG;
2219         atl1e_set_ethtool_ops(netdev);
2220
2221         netdev->hw_features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_TSO |
2222                               NETIF_F_HW_VLAN_RX;
2223         netdev->features = netdev->hw_features | NETIF_F_LLTX |
2224                            NETIF_F_HW_VLAN_TX;
2225
2226         return 0;
2227 }
2228
2229 /*
2230  * atl1e_probe - Device Initialization Routine
2231  * @pdev: PCI device information struct
2232  * @ent: entry in atl1e_pci_tbl
2233  *
2234  * Returns 0 on success, negative on failure
2235  *
2236  * atl1e_probe initializes an adapter identified by a pci_dev structure.
2237  * The OS initialization, configuring of the adapter private structure,
2238  * and a hardware reset occur.
2239  */
2240 static int __devinit atl1e_probe(struct pci_dev *pdev,
2241                                  const struct pci_device_id *ent)
2242 {
2243         struct net_device *netdev;
2244         struct atl1e_adapter *adapter = NULL;
2245         static int cards_found;
2246
2247         int err = 0;
2248
2249         err = pci_enable_device(pdev);
2250         if (err) {
2251                 dev_err(&pdev->dev, "cannot enable PCI device\n");
2252                 return err;
2253         }
2254
2255         /*
2256          * The atl1e chip can DMA to 64-bit addresses, but it uses a single
2257          * shared register for the high 32 bits, so only a single, aligned,
2258          * 4 GB physical address range can be used at a time.
2259          *
2260          * Supporting 64-bit DMA on this hardware is more trouble than it's
2261          * worth.  It is far easier to limit to 32-bit DMA than update
2262          * various kernel subsystems to support the mechanics required by a
2263          * fixed-high-32-bit system.
2264          */
2265         if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
2266             (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)) {
2267                 dev_err(&pdev->dev, "No usable DMA configuration,aborting\n");
2268                 goto err_dma;
2269         }
2270
2271         err = pci_request_regions(pdev, atl1e_driver_name);
2272         if (err) {
2273                 dev_err(&pdev->dev, "cannot obtain PCI resources\n");
2274                 goto err_pci_reg;
2275         }
2276
2277         pci_set_master(pdev);
2278
2279         netdev = alloc_etherdev(sizeof(struct atl1e_adapter));
2280         if (netdev == NULL) {
2281                 err = -ENOMEM;
2282                 dev_err(&pdev->dev, "etherdev alloc failed\n");
2283                 goto err_alloc_etherdev;
2284         }
2285
2286         err = atl1e_init_netdev(netdev, pdev);
2287         if (err) {
2288                 netdev_err(netdev, "init netdevice failed\n");
2289                 goto err_init_netdev;
2290         }
2291         adapter = netdev_priv(netdev);
2292         adapter->bd_number = cards_found;
2293         adapter->netdev = netdev;
2294         adapter->pdev = pdev;
2295         adapter->hw.adapter = adapter;
2296         adapter->hw.hw_addr = pci_iomap(pdev, BAR_0, 0);
2297         if (!adapter->hw.hw_addr) {
2298                 err = -EIO;
2299                 netdev_err(netdev, "cannot map device registers\n");
2300                 goto err_ioremap;
2301         }
2302         netdev->base_addr = (unsigned long)adapter->hw.hw_addr;
2303
2304         /* init mii data */
2305         adapter->mii.dev = netdev;
2306         adapter->mii.mdio_read  = atl1e_mdio_read;
2307         adapter->mii.mdio_write = atl1e_mdio_write;
2308         adapter->mii.phy_id_mask = 0x1f;
2309         adapter->mii.reg_num_mask = MDIO_REG_ADDR_MASK;
2310
2311         netif_napi_add(netdev, &adapter->napi, atl1e_clean, 64);
2312
2313         init_timer(&adapter->phy_config_timer);
2314         adapter->phy_config_timer.function = atl1e_phy_config;
2315         adapter->phy_config_timer.data = (unsigned long) adapter;
2316
2317         /* get user settings */
2318         atl1e_check_options(adapter);
2319         /*
2320          * Mark all PCI regions associated with PCI device
2321          * pdev as being reserved by owner atl1e_driver_name
2322          * Enables bus-mastering on the device and calls
2323          * pcibios_set_master to do the needed arch specific settings
2324          */
2325         atl1e_setup_pcicmd(pdev);
2326         /* setup the private structure */
2327         err = atl1e_sw_init(adapter);
2328         if (err) {
2329                 netdev_err(netdev, "net device private data init failed\n");
2330                 goto err_sw_init;
2331         }
2332
2333         /* Init GPHY as early as possible due to power saving issue  */
2334         atl1e_phy_init(&adapter->hw);
2335         /* reset the controller to
2336          * put the device in a known good starting state */
2337         err = atl1e_reset_hw(&adapter->hw);
2338         if (err) {
2339                 err = -EIO;
2340                 goto err_reset;
2341         }
2342
2343         if (atl1e_read_mac_addr(&adapter->hw) != 0) {
2344                 err = -EIO;
2345                 netdev_err(netdev, "get mac address failed\n");
2346                 goto err_eeprom;
2347         }
2348
2349         memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
2350         memcpy(netdev->perm_addr, adapter->hw.mac_addr, netdev->addr_len);
2351         netdev_dbg(netdev, "mac address : %pM\n", adapter->hw.mac_addr);
2352
2353         INIT_WORK(&adapter->reset_task, atl1e_reset_task);
2354         INIT_WORK(&adapter->link_chg_task, atl1e_link_chg_task);
2355         netif_set_gso_max_size(netdev, MAX_TSO_SEG_SIZE);
2356         err = register_netdev(netdev);
2357         if (err) {
2358                 netdev_err(netdev, "register netdevice failed\n");
2359                 goto err_register;
2360         }
2361
2362         /* assume we have no link for now */
2363         netif_stop_queue(netdev);
2364         netif_carrier_off(netdev);
2365
2366         cards_found++;
2367
2368         return 0;
2369
2370 err_reset:
2371 err_register:
2372 err_sw_init:
2373 err_eeprom:
2374         iounmap(adapter->hw.hw_addr);
2375 err_init_netdev:
2376 err_ioremap:
2377         free_netdev(netdev);
2378 err_alloc_etherdev:
2379         pci_release_regions(pdev);
2380 err_pci_reg:
2381 err_dma:
2382         pci_disable_device(pdev);
2383         return err;
2384 }
2385
2386 /*
2387  * atl1e_remove - Device Removal Routine
2388  * @pdev: PCI device information struct
2389  *
2390  * atl1e_remove is called by the PCI subsystem to alert the driver
2391  * that it should release a PCI device.  The could be caused by a
2392  * Hot-Plug event, or because the driver is going to be removed from
2393  * memory.
2394  */
2395 static void __devexit atl1e_remove(struct pci_dev *pdev)
2396 {
2397         struct net_device *netdev = pci_get_drvdata(pdev);
2398         struct atl1e_adapter *adapter = netdev_priv(netdev);
2399
2400         /*
2401          * flush_scheduled work may reschedule our watchdog task, so
2402          * explicitly disable watchdog tasks from being rescheduled
2403          */
2404         set_bit(__AT_DOWN, &adapter->flags);
2405
2406         atl1e_del_timer(adapter);
2407         atl1e_cancel_work(adapter);
2408
2409         unregister_netdev(netdev);
2410         atl1e_free_ring_resources(adapter);
2411         atl1e_force_ps(&adapter->hw);
2412         iounmap(adapter->hw.hw_addr);
2413         pci_release_regions(pdev);
2414         free_netdev(netdev);
2415         pci_disable_device(pdev);
2416 }
2417
2418 /*
2419  * atl1e_io_error_detected - called when PCI error is detected
2420  * @pdev: Pointer to PCI device
2421  * @state: The current pci connection state
2422  *
2423  * This function is called after a PCI bus error affecting
2424  * this device has been detected.
2425  */
2426 static pci_ers_result_t
2427 atl1e_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2428 {
2429         struct net_device *netdev = pci_get_drvdata(pdev);
2430         struct atl1e_adapter *adapter = netdev_priv(netdev);
2431
2432         netif_device_detach(netdev);
2433
2434         if (state == pci_channel_io_perm_failure)
2435                 return PCI_ERS_RESULT_DISCONNECT;
2436
2437         if (netif_running(netdev))
2438                 atl1e_down(adapter);
2439
2440         pci_disable_device(pdev);
2441
2442         /* Request a slot slot reset. */
2443         return PCI_ERS_RESULT_NEED_RESET;
2444 }
2445
2446 /*
2447  * atl1e_io_slot_reset - called after the pci bus has been reset.
2448  * @pdev: Pointer to PCI device
2449  *
2450  * Restart the card from scratch, as if from a cold-boot. Implementation
2451  * resembles the first-half of the e1000_resume routine.
2452  */
2453 static pci_ers_result_t atl1e_io_slot_reset(struct pci_dev *pdev)
2454 {
2455         struct net_device *netdev = pci_get_drvdata(pdev);
2456         struct atl1e_adapter *adapter = netdev_priv(netdev);
2457
2458         if (pci_enable_device(pdev)) {
2459                 netdev_err(adapter->netdev,
2460                            "Cannot re-enable PCI device after reset\n");
2461                 return PCI_ERS_RESULT_DISCONNECT;
2462         }
2463         pci_set_master(pdev);
2464
2465         pci_enable_wake(pdev, PCI_D3hot, 0);
2466         pci_enable_wake(pdev, PCI_D3cold, 0);
2467
2468         atl1e_reset_hw(&adapter->hw);
2469
2470         return PCI_ERS_RESULT_RECOVERED;
2471 }
2472
2473 /*
2474  * atl1e_io_resume - called when traffic can start flowing again.
2475  * @pdev: Pointer to PCI device
2476  *
2477  * This callback is called when the error recovery driver tells us that
2478  * its OK to resume normal operation. Implementation resembles the
2479  * second-half of the atl1e_resume routine.
2480  */
2481 static void atl1e_io_resume(struct pci_dev *pdev)
2482 {
2483         struct net_device *netdev = pci_get_drvdata(pdev);
2484         struct atl1e_adapter *adapter = netdev_priv(netdev);
2485
2486         if (netif_running(netdev)) {
2487                 if (atl1e_up(adapter)) {
2488                         netdev_err(adapter->netdev,
2489                                    "can't bring device back up after reset\n");
2490                         return;
2491                 }
2492         }
2493
2494         netif_device_attach(netdev);
2495 }
2496
2497 static struct pci_error_handlers atl1e_err_handler = {
2498         .error_detected = atl1e_io_error_detected,
2499         .slot_reset = atl1e_io_slot_reset,
2500         .resume = atl1e_io_resume,
2501 };
2502
2503 static struct pci_driver atl1e_driver = {
2504         .name     = atl1e_driver_name,
2505         .id_table = atl1e_pci_tbl,
2506         .probe    = atl1e_probe,
2507         .remove   = __devexit_p(atl1e_remove),
2508         /* Power Management Hooks */
2509 #ifdef CONFIG_PM
2510         .suspend  = atl1e_suspend,
2511         .resume   = atl1e_resume,
2512 #endif
2513         .shutdown = atl1e_shutdown,
2514         .err_handler = &atl1e_err_handler
2515 };
2516
2517 /*
2518  * atl1e_init_module - Driver Registration Routine
2519  *
2520  * atl1e_init_module is the first routine called when the driver is
2521  * loaded. All it does is register with the PCI subsystem.
2522  */
2523 static int __init atl1e_init_module(void)
2524 {
2525         return pci_register_driver(&atl1e_driver);
2526 }
2527
2528 /*
2529  * atl1e_exit_module - Driver Exit Cleanup Routine
2530  *
2531  * atl1e_exit_module is called just before the driver is removed
2532  * from memory.
2533  */
2534 static void __exit atl1e_exit_module(void)
2535 {
2536         pci_unregister_driver(&atl1e_driver);
2537 }
2538
2539 module_init(atl1e_init_module);
2540 module_exit(atl1e_exit_module);