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